Mayank's C Challenger - I

Hi All,

Here is a small 'C' question for all of you. Find the output:
#include
int main()
{
int i=10;
int j=sizeof(++i);
printf("i = %d\n",i);
printf("j = %d\n",j);
return 0;
}

Actually finding the output is the easiest thing, just run it on any compiler. But, the point is to find out the discripency with the printed value of variable 'i'.
If i=10 then WHY????
If i=11 then WHY????

I will be comming up with other C/C++ questions too, so everyone keep answering and keep enjoying.


Thanks and Regards,
Mayank Shukla.

Replies

  • crazy anoop
    crazy anoop
    j=11
    coz
    i is incremented and then took size
  • Mayank
    Mayank
    Sorry Buddy,

    But that is way away from the actual answer.πŸ˜”

    Regards,
    Mayank Shukla.
  • sahana
    sahana
    i will be 10.this ofcource obvious.
    j will be 2.
    the size occupied by an integer is 2 bytes(this can differ for different compilers).this means that i occupies 2 bytes.incrementing the value will not affect the size occupied by it.so j will be 2.
  • Kaustubh Katdare
    Kaustubh Katdare
    sahana
    i will be 10.this ofcource obvious.
    j will be 2.
    the size occupied by an integer is 2 bytes(this can differ for different compilers).this means that i occupies 2 bytes.incrementing the value will not affect the size occupied by it.so j will be 2.
    I'm not a C Expert, but I will use my C-memories of the past :sshhh: . I guess Sahana got a right answer.

    Mayank, confirm please!

    -The Big K-
  • Mayank
    Mayank
    Hey Sahana,

    You are correct. 😎

    But still I didnt got what I was looking for. You explanation for the value of J is correct. Just double check it when you say the value of 'j' will be Compiler specific or OS specific 😁. The latter is correct.

    Anyways, I dont know why you feel value of "i" is so obviously "10". What happened to it when we said "i++" in the code. That should have idealy increased its value by 1. Why is that not happening?

    Lets discuss on this.


    Thanks and Regards,
    Mayank Shukla.
  • sahana
    sahana
    oops i m sorry πŸ˜• the value of i will be 11.i forgot abt ++i
    but i guess it will be complier specific.how do u say that it is os specific.does the operating sytem has anything to do with allocation of memory incase if integer variables. i dont understand that.please xplain me.
  • Mayank
    Mayank
    Hey All,

    There is a problem friend. 'i' is not going to be '11', even that '++i' is present.
    Crazies........, come on and find the correct answer with an explanation. I am glad the at least people are thinking on it πŸ˜‰.
    All the best to all.

    One more thing, amount of memory assigned to an integer or any other variable for that matter is definitely dependent on OS and not the compiler. You use 16 bit OS, the value will be 2 or else if you use a 32 bit OS it will give the value as 4.

    Thanks and Regards,
    Mayank Shukla.
  • sahana
    sahana
    hey mayank.

    i am actually using a 32 bit os.my compiler is turboc.the value returned by it for size of an integer is 2.
    okay if i execute the same code in visual c++, it will return 4.so it definetly depends on the complier.
  • Mayank
    Mayank
    Hi,

    Ok. I need to check it again, thxs for that input.

    Mayank
  • Mayank
    Mayank
    Finally Here is the answer.......

    i=10

    The reason why 'i' is 10 : The sizeof operator just check the type of the expression passed to it. That expression is never executed. So in our case "++i" is never executed, just its type is taken, which makes its value as '10' and not '11'.

    Discussion about the value of 'j' is already done:smile:.


    Thanks and Regards,
    Mayank Shukla.
  • ksrbhanu
    ksrbhanu
    Good point Mayank, sizeof operator just checks the type of the expr given to it. it will not execute the expression.Thanks for correct explanation and keep posting. By the way this is bhanu πŸ˜›
  • Manish Goyal
    Manish Goyal
    Mayank
    Finally Here is the answer.......

    i=10

    The reason why 'i' is 10 : The sizeof operator just check the type of the expression passed to it. That expression is never executed. So in our case "++i" is never executed, just its type is taken, which makes its value as '10' and not '11'.

    Discussion about the value of 'j' is already done:smile:.


    Thanks and Regards,
    Mayank Shukla.
    Good info....I have never heard about this before

You are reading an archived discussion.

Related Posts

Hi all Engineers! I have a question for those interested in Physics. Can we have artifical Gravity? Meaning, can we create gravity under test conditions? With best regards, ROCKER
Three Crazy Engineers celebrate their Birthday today !😁 HaPpy Birthday Swati, veereshshugar & acrazyengineer:myparty: May your dreams come true & you have all the fun! πŸŽ‰ Party On! -The Big...
i love programming
spamming ....? me....? yak.... i am quiting this forum..i didnt know how i spammed.... r u crazy.... gud bye... sorry for interfereing in YOUR FORUMπŸ˜”πŸ˜‘
can u help me to get some excellent open source game engines?