1. Home >
  2. Apps >
  3. Groups >

Mayank's C Challenger - I

Question asked by Mayank in #Coffee Room on Sep 27, 2006
Mayank
Mayank 路 Sep 27, 2006
Rank D2 - MASTER
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. Posted in: #Coffee Room
crazy anoop
crazy anoop 路 Sep 27, 2006
Rank D2 - MASTER
j=11
coz
i is incremented and then took size
Mayank
Mayank 路 Sep 28, 2006
Rank D2 - MASTER
Sorry Buddy,

But that is way away from the actual answer.馃様

Regards,
Mayank Shukla.
sahana
sahana 路 Sep 28, 2006
Rank D2 - MASTER
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 路 Sep 28, 2006
Rank A1 - PRO
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 路 Sep 29, 2006
Rank D2 - MASTER
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 路 Sep 29, 2006
Rank D2 - MASTER
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 路 Oct 5, 2006
Rank D2 - MASTER
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 路 Oct 5, 2006
Rank D2 - MASTER
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 路 Oct 6, 2006
Rank D2 - MASTER
Hi,

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

Mayank
Mayank
Mayank 路 Oct 9, 2006
Rank D2 - MASTER
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 路 Nov 7, 2006
Rank E1 - BEGINNER
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 路 Dec 27, 2009
Rank A2 - PRO
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 must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up