+ 1

What is the output and why?

#include <stdio.h> void main() {     int x = 97;     int y = sizeof(x++);     printf("x is %d", x); }

22nd Sep 2020, 5:45 PM
Gaurav Rawat
Gaurav Rawat - avatar
3 Answers
+ 2
output is x is 97 Because output value of x after increment is still 97 as compared to the expected value which is 98 because of sizeof operator don't need to evaluate the expression in side it,it only gives sizeof the data type So x value is remain same
22nd Sep 2020, 6:21 PM
Bhargava Ram
Bhargava Ram - avatar
+ 1
The C compiler evaluates the sizeof operand during compile time and just replaces the term with the appropriate constant. So there is no code at run time that executes the post increment. I have read that run-time evaluation of sizeof is a relatively new feature that was added to support variable length arrays (VLAs). However, I would suppose that the new standard did not update run-time evaluation of arbitrary expressions in order to maintain compatibility for programs written under past standards.
22nd Sep 2020, 8:48 PM
Brian
Brian - avatar