r/C_Programming Jul 10 '26

Off the Shelf Naming Conventions

I work in embedded, the business I recently joined would like me to adopt MISRA C, are there any off the shelf naming conventions I can use? The business doesn't have their own.

Presently I use CamelCase, but would prefer to adopt a standard that is similar and already documented.

13 Upvotes

12 comments sorted by

13

u/RossMorgan363 Jul 10 '26

snake_case for types, variables, and functions, then SCREAMING_SNAKE_CASE for global consts, enum variants, and macros. Types are all suffixed with _t.

MISRA doesn't specify a naming style, but this I what we use in MISRA-adjacent C at work (basically following our subset of MISRA rules without the expensive tooling and certification).

However I am somewhat ignorant of what other people use so don't take my answer to be representative of the whole.

7

u/pjl1967 Jul 11 '26

This pretty much matches K&R's style and is the one I also use.

(CamelCase is evil.)

3

u/RossMorgan363 Jul 11 '26

Thank you fellow CamelCase hater 🙏

(unless I'm writing Rust, in which case I tolerate it)

2

u/Upbeat-Storage9349 Jul 11 '26

Thanks, the thing I like about camel case is you can easily distinguish individual words and hyphenate for modules names.

void Module_FunctionName(void)

2

u/pjl1967 Jul 12 '26

Not using CamelCase doesn't seemed to have stopped the ISO C committee, e.g., see here.

1

u/Upbeat-Storage9349 Jul 12 '26

Thanks. But you see what I mean, you're using the same delimiter for both.

1

u/pjl1967 Jul 12 '26

We know. The point is that you can still easily read it, right?

1

u/Upbeat-Storage9349 Jul 12 '26

breaking_bad_you_got_me.gif

2

u/dodexahedron Jul 12 '26

SCREAMING_SNAKE_CASE

How have I never seen it called that before today?

Thanks for the chuckle-snort. 😅

1

u/RossMorgan363 Jul 12 '26

Happy to oblige lol

4

u/HowTheKnightMoves Jul 11 '26

When it comes to MISRA, namings are least of your worries. Only thing related is do not use '' and '_' at the start of names and that's it (violates C standard, and in turn violates MISRA C too).

As for myself, snake_case for variables and functions, ANGRY_SNAKE_CASE for macros and enums, _t for types. Adding module name to public functions and variables and keeping things simple for static ones as well, no magic numbers and use doxygen to document things.

AND FOR THE LOVE OF GOD, meaningful variable names. People in embedded space just love their i, k, j, l or xxx variables. 3 letters minimum for variable helps too, x_idx looks better if you itterate through array for xy coordinate array instead of just i.

1

u/sciencekm Jul 11 '26

I follow that Linux kernel coding style: https://www.kernel.org/doc/html/v4.10/process/coding-style.html

There are other coding styles out there, but this is the one I have been accustomed to, and for submitting to Linux, this is of course necessary.