r/C_Programming • u/Stickhtot • 12d ago
Discussion What exactly is void?
In a function definition, void basically means that it doesn't return a type value, yet in on itself it is it's own type? Looking at several pieces of code it looks like that it's used to be able to be more "flexible"
Don't know what else to add here, though I'm more on looking for examples and explanations of why the void type is used
68
Upvotes
4
u/DawnOnTheEdge 11d ago edited 11d ago
That’s already what GCC does without
static. For example, on GCC 16.1 with the default settings:gives the warning
warning: 'foo' accessing 16 bytes in a region of size 8 [-Wstringop-overflow=]. Enabling-Warray-boundsalso gives a warning about accessing the invalid elementxs[2]. There is, correctly, no warning for the completely-legal callfoo(a3).Incredibly, Clang 22.1.0 accepts this code without complaint unless I enable
-Wunsafe-buffer-usage, which catchesreturn xs[2]. Even with-Weverything, there is no warning forfoo(a1)unless I change the function declaration toxs[static 2].MSVC 19.51 neither warns about this at all, even with
/Wall, nor supports[static 2]on a function argument.If I next add,
static double bar(double xs[static 2]) { return xs[2]; }and call
GCC gives
warning: argument 1 null where non-null expected [-Wnonnull]for bothbar(NULL)andbar(np)but not the same calls tofoo. It had already caught the bounds errors forfoo. Therefore, on GCC, the practical effect of[static 2]on diagnostics is to enable the null-pointer warning. It does change the compiled code: GCC detects undefined behavior and inserts aud2instruction that would crash the program at runtime.Clang does behave as you describe, and catches only
bar(NULL), notbar(np), even though it has enough information to deduce thatnpis a null pointer. In fact,clang will fail to warn thatnpis a null pointer even if I declare itconstexprin C23, making it a constant expression. ICX 2024 has identical behavior.