an interview question


What's the output of the following program? Assume the program is running on a 32bit CPU.

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>

int main()
{
int i=3;
int j;
j=sizeof(++i + ++i);
printf("i= %d j= %d \n", i, j);

}
The size of an integer is implementation defined
I'm sure the "32-bit CPU" bit of information was actually meant to say that int is 4 bytes in this case.
Then it's i=3 j=4.
And I'm fairly certain this is a class assignment.
You can't know if sizeof(int) == sizeof(short) or if sizeof(int) == sizeof(long)
The thing to understand is that sizeof() only cares about the TYPE of the variable and NOT its 'value'.

So you need to know how many bytes an int is on a 32 bit computer (I'm not sure that's a given btw).

@OP
how about?

procure pencil and notepad.
compile program.
run program.
copy output into notepad.

ISO/IEC 14882:2003 §5.3.3:
sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined.
ISO/IEC 9899:1999 §6.5.3.4:
[sizeof]
3. When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. [...]
4. The value of the result is implementation-defined [...]
Even if a given compiler gives a certain output you can't say that that is the output of the program
It is also asking about the value of i, which can be anything, because the consequences of the statement are undefined. More on this here:
http://www.cplusplus.com/forum/general/23499/
Duoas wrote:
It is also asking about the value of i, which can be anything, because the consequences of the statement are undefined.

Since the expression in sizeof is not evaluated, the value of i is defined in this case.
With this:

1
2
3
int i=3;
int j;
sizeof(++i + ++i);


Even though the expression (++i + ++i) has an undefined value, I think we can guarantee that i will be incremented exactly twice. So I think that i can be said to have the value 5 after the expression of undefined value completes.

EDIT: Actually I think that's what Athar just said.
Last edited on
Hmm, I've learned something.

[edit] No, that's not what he said. He said that the expression is "not evaluated".
[edit] Also, if it were evaluated, we cannot guarantee anything about i's value after the expression is evaluated, because said expression is undefined.
Last edited on
Athar wrote:
Since the expression in sizeof is not evaluated, the value of i is defined in this case.

Oh, no I got you now. The expression in sizeof() is literally not processed. Very interesting.

EDIT: I agree that the expression is undefined, but that is because we don't know when i will be incremented. But surely we do know that i will be incremented twice? (at least if it was not within a sizeof() directive).
Last edited on

The question assumes that the "int" has 4 bytes.
So you need to know how many bytes an int is on a 32 bit computer (I'm not sure that's a given btw).

Out of curiosity, has anoyone actually come across a computer where an int hasn't been 4 bytes? Or know of one where it is not?
Topic archived. No new replies allowed.