megazord
megazord

Reputation: 3210

What does the void statement in C do?

What does putting void; on a line do in C? The compiler is warning about it but i dont understand. What is the point in being able to put void on a line like this?

#include <stdio.h>

int main() {
        void;
        printf("word dude");
        return 1;
}

eh

$ gcc -pedantic -ansi -Wall -Wextra eh.c -o eh
eh.c: In function 'main':
eh.c:4:2: warning: useless type name in empty declaration
$ ./eh
word dude

People seem to be getting confused what I'm asking: What does this line mean, does it do anything? why is it valid?

void;

Removed void cast as it's causing unnecessary discussion.

Upvotes: 4

Views: 4703

Answers (6)

Ralph Allan Rice
Ralph Allan Rice

Reputation: 402

This question got me interested because my first thought was K&R. I went back to my old K&R book (Appendix A p.192) on found blurb about declarations (transcripted):

8. Declarations

Declarations are used to specify the interpretation which C gives to each identifier; they do not necessarily reserve storage associated with the identifier. Declarations have
the form
    declaration:
        decl-specifier declarator-listopt;

The declarators in the declarator-list contain the identifiers being declared. The decl-specifiers consist of a sequence of type and storage class specifiers.
    decl-specifiers:
        type-specifier decl-specifiersopt
        sc-specifier decl-specifiersopt

This leads me to believe that delarator lists are optional (meaning it can be empty).

To add to this confusion on following page, it lists the set of legal type-specifier values and void is not one of them.

In my narrow interpretation that this may be still legal (but obsolete) C.

Upvotes: 2

Levon
Levon

Reputation: 143102

void;

There's no point in doing that, it doesn't do anything.

Did you get this out of a book? Must be a mistake.

Somewhat unrelated, a return value if 0 is usually used to indicate the program terminated normally, any other value indicates a problem (by convention)

Update:

Looks like you added another line:

(void)printf("word dude");

this - unlike the previous void; - tells the compiler explictly to ignore the return value of the printf() function (the number of characters printed) and prevent warning messages a picky/pedantic setting on the compiler or other tools, like lint, to stop from complaining.

You don't see this for frequently used functions generally, though you may for others.

Upvotes: 1

carlosfigueira
carlosfigueira

Reputation: 87258

It looks like the old style of declaring arguments to a function. That's equivalent to int main(void). Some compilers still accept it, some don't. For example, this code compiles in two compilers I tried:

void main(i, j) {
    int;
    long;

    i = 12;
    j = 123456;
    printf("%d %d\n", i, j);
}

The answer to Untyped arguments in a C function declaration has a more precise definition for this case.

In any case, this is not recommended, so can we just stick with the "normal" way of declaring functions, please? :)

Upvotes: 0

octopusgrabbus
octopusgrabbus

Reputation: 10695

void is used as a data type in C, and come to think of it, I've never heard of an assembly language equivalent of NOP in C, so having void by itself on a line is probably an error.

Upvotes: 0

Zagorax
Zagorax

Reputation: 11890

It prints the same error if you change void with int or any other type.

void is a type and it expects something after that type. It has no sense at all as writing only int without a variable (or function) name after it.

Upvotes: 0

ouah
ouah

Reputation: 145899

      void;                       // 1
      (void)printf("word dude");  // 2

The first statement is an invalid statement: this is not C.

In the second statement the (void) cast makes your intent clear to discard the return value. Another common use is to make silent some static analysis tools like Lint.

Upvotes: 2

Related Questions