Home > Net >  Error "expression result unused" when using comma operator
Error "expression result unused" when using comma operator

Time:12-22

I am trying to understand the comma Operator in C and I have encountered this compilation error. Can somebody help me?

#include <stdio.h>

int main(void)
{
    int a = (1,2);
    printf("%d", a);
}

I am using a GCC compiler. I expect the variable "a" value to equal 2 and print it out as output by the printf. But the following warning is generated.

Output:

test.c:5:11: warning: expression result unused [-Wunused-value]
        int a = (1,2);
                 ^
1 warning generated. 

  

CodePudding user response:

Your compiler is configured to treat warnings as being fatal. Normally it is not a good idea to disable that but for this experimental case you can disable it with -Wno-error.

$ gcc -Wno-error test.c
test.c:9:14: warning: expression result unused [-Wunused-value]
    int a = (1,2);
             ^
1 warning generated.
$ ./a.out
2
  •  Tags:  
  • c
  • Related