#include<stdio.h>
void main() {
int g = 83;
int h = (g++, ++g);
printf(“%d”, h);
}
**g++** will increment **g** after **;**
My answer: h = 84
Correct answer: h = 85
I am a beginner that's why I am confused.
#include<stdio.h>
void main() {
int g = 83;
int h = (g++, ++g);
printf(“%d”, h);
}
**g++** will increment **g** after **;**
My answer: h = 84
Correct answer: h = 85
I am a beginner that's why I am confused.
On
g++ will increment g after ;
No, it will increment g before ++g. , is a sequence operator. See the example here.
On
This is an example of the comma operator in C, not to be confused with commas used in argument lists.
The comma operator first evaluates its left operand for side effects (throwing away the resulting value), and then evaluates its right operand. All side effects in that left operand will be sequenced-before all side effects and reads in the right operand.
So g++ will increment g, and then ++g wil increment g again and give the value after this second increment.
We first evaluate the left operand
g++sogis now 84 but otherwise ignore the result. Then we evaluate the right operand++gsogis now85.Here is the relevant sections of the specification: