Examveda
Examveda

What will be the output of this program on an implementation where "int" occupies 2 bytes?
#include <stdio.h>
void main()
{
      int i = 3;
      int j;
      j = sizeof(++i + ++i);
      printf("i=%d j=%d", i, j);
}

A. i=4 j=2

B. i=3 j=2

C. i=5 j=2

D. the behavior is undefined

Answer: Option B

Solution(By Examveda Team)

Evaluating ++i + ++i would produce undefined behavior, but the operand of sizeof is not evaluated, so i remains 3 throughout the program. The type of the expression (int) is reduced at compile time, and the size of this type (2) is assigned to j.


This Question Belongs to C Program >> Operators And Expressions

Join The Discussion

Comments ( 5 )

  1. Shubham Shinde
    Shubham Shinde :
    1 year ago

    in 32 bit operating system
    i=3 j=2
    in 64 bit operating system
    i=3 j=4

  2. Mahin's Education
    Mahin's Education :
    3 years ago

    I run this program on my codeblocks 13.12. It showed i=3 j=4. Why j=4 ?

  3. MD.Shafiqur Rahman
    MD.Shafiqur Rahman :
    4 years ago

    i = 3
    j = sizeof(10) //sizeof(int)
    so i = 3 j = 2

  4. Oma Choudhary
    Oma Choudhary :
    6 years ago

    i=3 b=4

  5. Mehwish Shabbir
    Mehwish Shabbir :
    7 years ago

    Why expression (int) is reduced at compile time?

Related Questions on Operators and Expressions