A = 3.2.1; gives an error in gcc

I tried the following code in gcc:

#include<stdio.h>
int main()
{
    int a=3,2,1;//////////////////////ERROR!//////////////////////////
    printf("%d", a);
    return 0;
}

I expected it to compile successfully as:

  • a series of integer expressions separated by commas will be evaluated from left to right, and the value of the rightmost expression becomes the value of the full expression, separated by a comma.

Then the value of the integer variable a should be 1 right? Or is it 3?

And why am I getting this error when trying to run this program?

error: expected identifier or '(' before a numeric constant

+4
source share
2 answers

If you separate the initialization from the declaration, you can get what you expected:

int a;
a = 3, 2, 1;     // a == 3, see note bellow

or

int a;
a = (3, 2, 1);   // a == 1, the rightmost operand of 3, 2, 1

( , , 2 1)

. .

,

a = 3, 2, 1

3 a = 3, 2 1, ,

a = 3, 2

2 ( ) (, , - , a = 3 3), , a = 3 , .. . 3 a. ( AnT .)

+3

.

, :

int a=(3,2,1);
+12

Source: https://habr.com/ru/post/1659201/


All Articles