CrazyEngineers
  • Mayank
    Mayank

    MemberSep 27, 2006

    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
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • crazy anoop

    MemberSep 27, 2006

    j=11
    coz
    i is incremented and then took size
    Are you sure? This action cannot be undone.
    Cancel
  • Mayank

    MemberSep 27, 2006

    Sorry Buddy,

    But that is way away from the actual answer.😔

    Regards,
    Mayank Shukla.
    Are you sure? This action cannot be undone.
    Cancel
  • sahana

    MemberSep 28, 2006

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorSep 28, 2006

    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-
    Are you sure? This action cannot be undone.
    Cancel
  • Mayank

    MemberSep 29, 2006

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • sahana

    MemberSep 29, 2006

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Mayank

    MemberOct 5, 2006

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • sahana

    MemberOct 5, 2006

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Mayank

    MemberOct 5, 2006

    Hi,

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

    Mayank
    Are you sure? This action cannot be undone.
    Cancel
  • Mayank

    MemberOct 9, 2006

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • ksrbhanu

    MemberNov 7, 2006

    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 😛
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberDec 27, 2009

    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
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register