r/C_Programming • u/iper_linuxiano • Jul 10 '26
Question C programming style
Hi all, in my opinion choosing a valid C programming style may help people to maintain code. I know that freedom is a plus, especially for code creators, but it may become a real nightmare for code maintainers.
In my work experience I always tried to keep a uniform code style even if I work by myself, but, after six months I create a software, if I don't use a code style I lose so much time to correct my own code that sometimes I create it newly from scratch.
My question is: are there any places where programmers share their code styles, or some advices (especially variable or typedef names) based upon their real work experience?
Thanks in advance to anyone who will answer! Cheers!
16
u/Practical-Sleep4259 Jul 10 '26
Yes I believe major companies like Google and Microsoft maintain company wide styles.
All major companies that employ guidelines.
3
u/mikeblas Jul 12 '26
Here's one: https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/
7
u/Evil-Twin-Skippy Jul 10 '26
I've always lived by the typesetting guidance that K&R put forth in "The C Programming Language"
Yes, it's mainly designed to look good on a printed page. But it does work really well across giant codebases:
Myself, I write in a lot of mixed Tcl, C, and Sqlite, so I've modified it slightly:
My style also reflects that when fitting code into 2 column research papers, 2 spaces instead of 4 saves horizontal space.
5
u/SmokeMuch7356 Jul 10 '26
As far as formatting goes, after 15 years1 of free-wheeling it in
vimI'm now working through VSCode and everything gets formatted on save per somebody else's requirements. I don't hate it - it's close to the style I've used for the past couple of eons - but every now and again it does something that annoys me.It has saved us ungodly amounts of heartburn on diffs and merges so I'm happy to put up with it. And it has sped up the review process since we're not wasting time trying to figure out what scope we're in because Bruce used 5 spaces while Abbas used 4-space tabs and Sourav used 8-space tabs and we're looking at it in my environment where all indentations are 2 spaces (AS GOD INTENDED) and it's an unintelligible mess.
Style is a bit trickier - that's not just formatting, that's also things like naming conventions, inline documentation, what you put behind an abstraction layer vs. what you don't, etc. I know there are guides and conventions some organizations use to enforce common styles, but we don't use them where I work.
For example, I consider Hungarian notation an abomination and do not use it. I do not use typedefs unless I'm willing to write a full API for that type and hide its implementation completely (a la
FILE).I factor my code a bit more aggressively these days; makes unit testing easier.
4
u/Vincenzo__ Jul 11 '26
Kernel coding style
Everything else is ugly
Don't @ me
3
u/Dangerous_Region1682 Jul 11 '26
If you really want, there once was a UNIX command called cb, which was a C Beautifier.
It might live on somewhere in the current world.
We used to use versions of it as part of our Makefiles.
Of course we were largely adopting K&R C ANSI Edition as our Bible, so we largely followed that as our style.
Younger folks may not agree of course, especially regarding pointer declarations.
Sometimes what helps you create or adopt a style is by keeping your code simple and readable. Things tend to get into trouble style wise when you start using clever or esoteric code.
If you write code that’s complicated to read, or poorly commented or documented enough you can’t read your own code, I think your problems would lie with more than code style.
3
u/chrisekh Jul 11 '26
keep same style what was in file. If new file then same style than in mostly library. If new library then select some reasonable. Projects usually have many libraries with many styles so coder just must to adapt many styles.
3
u/richtw1 Jul 11 '26
Everyone here is talking about formatting style or naming convention. I think that's largely unimportant. What's more important in my opinion are the actual coding standards you hold yourself to.
Mine are:
Emphasis on value semantics; return multiple values by value in a struct. Pass small structs by value.
Treat slices as a first class concept (essentially a {pointer, length} pair). Prefer indices over pointers. Avoids problems with reallocation invalidating pointers.
Arena allocation over raw malloc/free. This helps you consolidate objects with the same lifetime in one group, easier to reason about and less prone to bugs.
No standard library. I don't use <string.h>, I have my own string types based around a slice representation, with some good supporting functions.
I routinely write expressive and safe code with these (and other) standards in mind.
2
u/dvhh Jul 11 '26
for most of it I use clang format when possible to unify between dev, otherwise regarding variable, I pretty much try to stick to target_verb, boolean are usually simple binary adjectives like enabled (struct they are in give the context). struct member name are usually quite generic, which is contextualized by the variable name. It is for me a way to be concise without resorting to implementation specific abrevations.
2
u/sciencekm Jul 11 '26
Linux kernel coding style for me: https://www.kernel.org/doc/html/v4.10/process/coding-style.html
When I have to import code, I'm old school, and I just use gnu indent with my custom rules.
2
u/Substantial_Job_2068 Jul 11 '26
I think deciding a style or structure and sticking to it is problematic as it hinders you from exploring new ways to do things. Also as you improve that style and structure will probably change too.
For a large codebase with many people it makes sense to use linting and agreed patterns to keep things under control, but I would never do that for my own projects as I want to try new and better ways to do things.
2
u/Fujinn981 Jul 11 '26
I absolutely try to keep a uniform code style myself, although if I'm working in some one elses codebase, I will adhere to their style instead rather I like it or not, as to keep things nice and uniform.
1
u/Hawk13424 Jul 10 '26
My work project uses Allman, lower-case camel for variables, upper-case camel for functions. Upper-case snake for macros and defines. _t for typedefs. 4 spaces, no tabs. Fully parenthesised expressions.
3
u/[deleted] Jul 10 '26
[removed] — view removed comment