The void type in C

  • A+
Category:Languages

The void type in C seems to be strange from various different situations. Sometimes it behaves like a normal object type, such as int or char, and sometimes it just means nothing (as it should).

Look at my snippet. First of all, it seems strange that you can declare a void object, meaning you just declare nothing.

Then I created an int variable and casted its result to void, discarding it:

If an expression of any other type is evaluated as a void expression, its value or designator is discarded. (ISO/IEC 9899:201x, 6.3.2.2 void)

I tried to call my function with a void cast, but my compiler gave me (Clang 10.0):

error: too many arguments to function call, expected 0, have 1

So the void in a prototype means nothing, and not the type void.

But then, I created a pointer to void, dereferenced it, and assigning the “result” to my int variable. I got the “incompatible type” error. That means the void type does exist here.

extern void a; // Why is this authorised ???  void foo(void); // This function takes no argument. Not the 'void' type.  int main(void) {     int a = 42;     void *p;      // Expression result casted to 'void' which discards it (per the C standard).     (void)a;      // Casting to 'void' should make the argument inexistant too...     foo((void)a);      // Assigning to 'int' from incompatible type 'void': so the 'void' type does exists...     a = *p;      // Am I not passing the 'void' type ?     foo(*p);      return 0; } 

Is void an actual type, or a keyword to means nothing ? Because sometimes it behaves like the instruction “nothing is allowed here”, and sometimes like an actual type.

EDIT: This questions is NOT a duplicate. It is a purely about the semantics of the void type. I do not want any explanation about how to use void, pointers to void or any other things. I want an answer per the C standard.

 


From C Standard#6.2.5p19

19 The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.

This indicate that the void type exists.

Doubt 1:

void foo(void); // This function takes no argument. Not the 'void' type. 

Here, void is representing the empty set that means function foo() parameter list is empty and is neither taking argument and nor returning any value.

Doubt 2:

// Casting to 'void' should make the argument inexistant too... foo((void)a); 

No, it will not because void is also an object type though it is incomplete.

Doubt 3:

// Assigning to 'int' from incompatible type 'void': so the 'void' type does exists... a = *p; 

Yes, it does and hence the compiler reporting error.

Doubt 4:

// Am I not passing the 'void' type ? foo(*p); 

The void in parameter list indicates that function is not taking any argument.
Check this from C Standard#5.1.2.2.1p1 [emphasis mine]:

1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

    int main(void) { /* ... */ } 

A void in parameter list means function with no parameters.

Doubt 5:

extern void a; // Why is this authorised ??? 

Because void is a valid type and it is just a declaration. No storage allocate to a.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: