I have a C code ,which uses simple comma operators
main()
{
int a= 1,2,3;
printf("%d",a);
}
Now when i compile got an error while same program with little modification runs fine
main()
{
int a;
a= 1,2,3;
printf("%d",a);
}
Why is it so?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the first case, the error is raised because the compiler is not able to differentiate if you pretend to declare several variables or assign several values.
Did you mean
int a; a = 1, 2, 3, orint a = 1, int 2, int 3? Compiler cannot tell from context (even if 2 or 3 are not legal variable names).This ambiguity does not exist in the second case, hence no error (but warnings issued anyway).
PS: it’s
int main()notvoid main().