If we use pre increment operator in a statement, then increment takes place first and then the statement gets executed. But if we use post increment operator in a statement the statement gets executed with it's older value and after this statement execution increment takes place
Ex: a=10;
b=a++; //b=10,a= 11
a=10;
b=++a; //b=11,a= 11
NOTE: You may have a doubt that If you use b=( a++)+(++a)+(++a) or any other statement which involves multiple pre or post increment or decrement operations on the same variable then what would be output?
The answer is undefined i.e. it is a compiler dependent. The compiler won't give an error but different compiler gives different outputs and no c++ rule define their execution.
Ex: a=10;
b=a++; //b=10,a= 11
a=10;
b=++a; //b=11,a= 11
NOTE: You may have a doubt that If you use b=( a++)+(++a)+(++a) or any other statement which involves multiple pre or post increment or decrement operations on the same variable then what would be output?
The answer is undefined i.e. it is a compiler dependent. The compiler won't give an error but different compiler gives different outputs and no c++ rule define their execution.
No comments:
Post a Comment