r/cprogramming • u/Key_River7180 • Apr 10 '26
tip - do not name variables like libc functions
Today I spent three hours debugging why ncurses jumped to address 0x55d9f367b4c0 out of the wild and crashing when calling stat...
I had a variable named stat holding the status of the program, and it was doing some really shitty and incorrect pointer arit on stat or something.
so yeah, just a quick tip :)
71
Upvotes
12
u/jeffbell Apr 11 '26
There was that obfuscated C winner that had an array named main. It contained machine code that would still run on two different processor architectures.
23
u/Plane_Dust2555 Apr 10 '26
There are some tips in glibc documentation as well:
_tsuffixes;_prefixes;Those are reserved to the library...
I tend to declare my types with
_T(uppercase T), instead. And use some camel style, like:doSometyhing,numItem, etc... but, of course, you can usesnakestyle as well:do_somethingornum_item, and you are essentially free from colisions... Not always (for example:EXIT_SUCCESSandEXIT_FAILURE).Always remember that C/C++ are case sensitive.
6
u/zhivago Apr 11 '26
Yes, namespaces is one of the areas where C++ really improved on C.
2
u/robobob68 Apr 11 '26
If your global scoped variables are local to a single file, make them static and then they will not collide with ones from a library. She goes for functions. Make them static whenever they aren't used by other files.
29
u/particlemanwavegirl Apr 11 '26
abbreviate less. there's literally no reason to drop two letters off a variable name. the decrease in readability is not remotely worth it.
15
u/YakPilot Apr 10 '26
Professional pro tip: You are always free to declare your arrays as
malloc[]orprintf[]1
u/WildCard65 Apr 11 '26
Gotta love how symbol resolution works on ELF based systems.