Problem: Output of this C Code

main()
{
int a = 1;
int c,d;
c = ++a + ++a + ++a;
printf("\n%d",a);
printf("\n%d ", c);
getch();
}

The above C code after the execution gives the values of 'a' and 'c' as '4' and '12' resp.

Can anyone explain, why it is so...

Replies

  • maheshkrishnan
    maheshkrishnan
    Chaudary...

    The result what you told (ie: c= 12 ) will be diffrent for diffrent compilers,
    But at the ,most of the compilers the "a" in your programe will be "4" only due to the value of a which increments and then assign.
    This type of codes are called, "UNDEFINED BEHAVIOUR" which will makes problem in programing.
    Take care of this if you are using a 32 bit compiler for this programe.you will get the out put of "c" as "10" (Amazing isnt)
    So this is an Undefined behaviour code.

    Thanks,
    Mahesh Krishnan
  • chaudharyssv
    chaudharyssv
    Hi Mahesh,

    Thanks for your response.
    Can you tell me more about the Undefined Behaviour? Does UB means that there is no such way in 'C' to evaluate such kind of expressions?

    Waiting for your reply.

    Regards,

    Satendra Chaudhary
  • Sujatha
    Sujatha
    Hi Mahesh,

    Whatever u have explained is correct. I have executed the same program in LINUX and i ot the answer as '4 ' and '10 'for 'a' and 'c' respectively.Yes its behaviour is compiler dependent.

    Hi Chowdary!!!!!!!!

    I think ur from AP. Its nice to see in this forum. Looking forward to share more knowledge.



    Cheers
    Sujatha
  • MaRo
    MaRo
    It's not weird if answers 4 & 12.

    Because each ++a increment & then assign the new value to 'a', i.e ++a makes a=2 assign this value to 'a' then the second ++a increment starting from a=2 NOT a=1 & so on with the third ++a, all ++a's increment in the same time with the new value of a. So, when a=1, ++a + ++a + ++a = 2 + 2 + 2. & so on when a=3 the three ++a increment parallel to 4 make it 4 + 4 + 4=12.
    Of course a=4.
    Resulting a,c equals 4 & 12 respectively.

    It's weird when c=10 😒

    Sometimes I get c=10 & c=12 😕
  • maheshkrishnan
    maheshkrishnan
    Sujatha
    Hi Mahesh,

    Whatever u have explained is correct. I have executed the same program in LINUX and i ot the answer as '4 ' and '10 'for 'a' and 'c' respectively.Yes its behaviour is compiler dependent.

    Hi Chowdary!!!!!!!!

    I think ur from AP. Its nice to see in this forum. Looking forward to share more knowledge.



    Cheers
    Sujatha


    Hi Choudhary , Sujatha.

    Acutally Undefined behaviour can be seen not only in C, also in all compiler dependent languages.A normal example is that if you wrote a progrm. in Turbo C which you are taking an input (int i), the maximum value will be 35K' but if you run the same programme in Linux or in 32 bit compiler you can take an maximum value of 65K .
    This is what at the time of real time programming you wants to take care,
    Please check the link and get more on this.
    Undefined behavior - Wikipedia

    Thanks,
    Mahesh Krishnan
  • dayanandavt
    dayanandavt
    Hi all,

    Entire process will be depends on the compiler, If the compiler is optimised then it will print c=10 otherwise it may lead to the parallel execution like c=12.

    for eg,
    int j = 0;
    for(i=0;i<100;i++)
    j++;

    print(j)


    [​IMG] Optimised compiler - wont loop up to 100 times instead it learns, 100times i need to increment the j by 1. then finally print "J" without iterating the loop even a single time.

    [​IMG] Ordinary compiler - It will loop 100 times accordingly then print "J".

    Regards
    daya

You are reading an archived discussion.

Related Posts

Hi Folks, I am final year computer engineering student. Could anyone suggest me the latest hot topics for the seminar related to latest computer technology. Regards, Satendra
CEans, Please help me choose a backup utility. Requirements are as below - Should be Free 😁 Should support Windows Vista Should allow me to choose individual folders to backup...
hi, i want to input 8 bits to EPP port. I have tried using 74HC244, 74LS244, 74LS245 as buffers but then also i am observing problem of overloading. If I...
A lot has been said but nothing much has been done about this topic. I know this is not one of those interesting topics on which anyone and everyone has...
Hyderabad has the prime importance in the educational field among the cities in southern India. This city has two Central Universities, six State Universities, and two Deemed universities. The Osmania...