5

I recently encountered this code, and I'm confused, what is it for?

What does it mean in the C programming language, to type cast a variable to void data type? What does this accomplish?

If an expression such as a type cast, or an addition is performed, and the result is not immediately assigned to some variable, then that result gets lost. Why should you ever want to do that? It seems like a useless expression.

The code sample:

int main(int argc, char *argv[])
{
    /* void unused vars */
    (void) argc;
    (void) argv;

    // more code here

    return 0;
}

It seems the author was not using these variables, but why cast them to void data type? You might as well cast them to int data type.

Galaxy
  • 1,856
  • 1
  • 14
  • 39

2 Answers2

6

This is to avoid unused variable warnings in some compilers.

Also there is a MISRA rule which states that all function parameters must be used. This method is a workaround for this rule.

Rishikesh Raje
  • 8,410
  • 2
  • 14
  • 30
3

This can be used to suppress unused variable(s) compilation warning.

Sumit Trehan
  • 3,727
  • 3
  • 21
  • 38