Mayank's C Challenger - I

Mayank

Mayank

@mayank-MV7Gjv Oct 25, 2024

Hi All,

Here is a small 'C' question for all of you. Find the output:
#include<stdio.h>
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • crazy anoop

    crazy anoop

    @crazy-anoop-DSplp9 Sep 27, 2006

    j=11
    coz
    i is incremented and then took size

  • Mayank

    Mayank

    @mayank-MV7Gjv Sep 27, 2006

    Sorry Buddy,

    But that is way away from the actual answer.😔

    Regards,
    Mayank Shukla.

  • sahana

    sahana

    @sahana-S4nL10 Sep 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.

  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk Sep 28, 2006

    sahanai 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

    @mayank-MV7Gjv Sep 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.

  • sahana

    sahana

    @sahana-S4nL10 Sep 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.

  • Mayank

    Mayank

    @mayank-MV7Gjv Oct 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.

  • sahana

    sahana

    @sahana-S4nL10 Oct 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.

  • Mayank

    Mayank

    @mayank-MV7Gjv Oct 5, 2006

    Hi,

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

    Mayank

  • Mayank

    Mayank

    @mayank-MV7Gjv Oct 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.

  • ksrbhanu

    ksrbhanu

    @ksrbhanu-7vVvZL Nov 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 😛

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Dec 27, 2009

    MayankFinally 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