on-internet
← Community

Comma Operator in for-loop in C

Stack Overflow ·

For the following loop, int i; for(i= 0, printf("hello\n"); i<10; i++) {printf("%d",i)}; the output is hello 0123456789 Can anyone explain this? My reasoning: In C, the comma operator works by evaluating the first expression, discarding its value, then evaluating the second expression and returning that as the result. For example: x = (exp1, exp2); This executes exp1, ignores its value, then evaluates exp2 and assigns that result to x. However, in a declaration like: int x1, x2, ... , xn; the comma is not the comma operator — it’s a declarator separator. The grammar of C treats commas in decla

Comments

Sign in or sign up to comment and upvote.

No comments yet.