I've never seen that 'U' near a number before :-\ . I'll try to guess, maybe foo() ? :-/ ( just guessing )
aegisknight
on Nov 5, 2003
The trailing U makes the integer literal unsigned. By default they're unsigned. You could replace the code with:
int negone = -1;
unsigned ten = 10;
if (negone < ten) {
....
aegisknight
on Nov 5, 2003
Er... I meant they're signed by default.
lordgalbalan
on Nov 5, 2003
I'd think that 1OU is an invalid variable name, so I'm gonna say neither.
At least in C; I'm not sure about C++.
aegisknight
on Nov 5, 2003
10U is an integer literal of 'unsigned int' type and value 10, in both C and C++.
bssnchica
on Nov 5, 2003
Hah. You got me.
You and your computer mumbo-jumbo... :-p
thespeedbump
on Nov 5, 2003
Well, if nobody else is brave enough to put their neck on the guillotine, I'll give it a shot. I'm due for a shave anyway.
The left operand is lexically evaluated first, and 10U is within the range of signed integers anyway. I shall guess at the obvious outcome. (foo();)
aegisknight
on Nov 5, 2003
Neither left nor right operands get any special treatment when it comes to type promotion. ((-1 < 10U) is equivalent to (10U > -1))
aegisknight
on Nov 5, 2003
Is your icon from Ebichu? o_O
tomt64
on Nov 5, 2003
I know for a fact this will say signed/unsigned mismatch with -Wall in GCC, so I dunno. I'll say foo(), because -1 is less than 10
flik9x
on Nov 6, 2003
If -1 is converted to unsigned to comply with the type of 10U, bar will be called because unsigned -1 is something like 360564
* Wonders if this depends on compilers too... *
dleary
on Nov 6, 2003
I don't really like interview questions like this, if they are a sort of "which is correct" interview question. If you can explain things, then that usually gets across that you know what you're talking about.
If -1 is "promoted" to unsigned, then it will be much larger than 10, so bar() would be called. If 10U is promoted to signed, then the "normal" thing will happen and foo is called.
It just depends on which type gets promoted, which is (I think?) an arbitrary decision in the C spec (maybe there is a reason behind it?), and I don't know off the top of my head which way it goes.
Regardless, it's bad style, and most compilers will warn you about it.
bssnchica
on Nov 6, 2003
Mayyyybe :)
lordgalbalan
on Nov 6, 2003
Oh OK. Thanks, I've really learned something! I didn't know that C allowed for dynamic conversion between unsigned and signed integer types, and I've read many, many books on the subject. But I've not read K&R C proper, and that's probably what I missed. Or, is this a modern ANSI C extension?
Did you just come up with this as a way to teach us something, or was this question really asked of you at a job interview?
You'd be a really effective mentor, you know?
aegisknight
on Nov 6, 2003
K&R C was never standardized afaik, so we can effectively ignore it. It's worthless anyway. :P The ANSI C that we all use was standardized in 1989, so it's not that modern...
I posted the entry because I was fixing a lot of warnings in OpenSG, and signed/unsigned comparisons happened to be a lot of them. I had just talked to my roommate about interview questions too.
I'm going to be posting an entry later clarifying the issue...
My mentor just got a LiveJournal. :)
aegisknight
on Nov 6, 2003
Where did you get it? And why? o_O Sister + Ebichu = Disturbing
bssnchica
on Nov 6, 2003
I got it from a friend of a friend... I thought it was adorable.
Don't worry, I've never seen Ebichu, but I know what it's about... however, taken out of context, the icon is precious. ^_^
bssnchica
on Nov 6, 2003
And how do YOU know about Ebichu, may I ask? o_O
aegisknight
on Nov 6, 2003
Mike and I have been watching it for the last year or so. We're up to episode 16. It's hilarious.
thespeedbump
on Nov 6, 2003
The best part of Ebichu is that it starts out as something a 9 year old would watch. Then Ebichu gets punched into a wall by her owner and explodes into a spectacular explosion of blood as she slams into the wall with a satisfyingly wet splatting sound.
After that, the chick who punched Ebichu jumps into the sack with her boyfriend for awhile.
I've never seen that 'U' near a number before :-\ . I'll try to guess, maybe foo() ? :-/ ( just guessing )
The trailing U makes the integer literal unsigned. By default they're unsigned. You could replace the code with:
int negone = -1; unsigned ten = 10; if (negone < ten) { ....
Er... I meant they're signed by default.
I'd think that 1OU is an invalid variable name, so I'm gonna say neither.
At least in C; I'm not sure about C++.
10U is an integer literal of 'unsigned int' type and value 10, in both C and C++.
Hah. You got me.
You and your computer mumbo-jumbo... :-p
Well, if nobody else is brave enough to put their neck on the guillotine, I'll give it a shot. I'm due for a shave anyway.
The left operand is lexically evaluated first, and 10U is within the range of signed integers anyway. I shall guess at the obvious outcome. (foo();)
Neither left nor right operands get any special treatment when it comes to type promotion. ((-1 < 10U) is equivalent to (10U > -1))
Is your icon from Ebichu? o_O
I know for a fact this will say signed/unsigned mismatch with -Wall in GCC, so I dunno. I'll say foo(), because -1 is less than 10
If -1 is converted to unsigned to comply with the type of 10U, bar will be called because unsigned -1 is something like 360564 * Wonders if this depends on compilers too... *
I don't really like interview questions like this, if they are a sort of "which is correct" interview question. If you can explain things, then that usually gets across that you know what you're talking about.
If -1 is "promoted" to unsigned, then it will be much larger than 10, so bar() would be called. If 10U is promoted to signed, then the "normal" thing will happen and foo is called.
It just depends on which type gets promoted, which is (I think?) an arbitrary decision in the C spec (maybe there is a reason behind it?), and I don't know off the top of my head which way it goes.
Regardless, it's bad style, and most compilers will warn you about it.
Mayyyybe :)
Oh OK. Thanks, I've really learned something! I didn't know that C allowed for dynamic conversion between unsigned and signed integer types, and I've read many, many books on the subject. But I've not read K&R C proper, and that's probably what I missed. Or, is this a modern ANSI C extension?
Did you just come up with this as a way to teach us something, or was this question really asked of you at a job interview?
You'd be a really effective mentor, you know?
K&R C was never standardized afaik, so we can effectively ignore it. It's worthless anyway. :P The ANSI C that we all use was standardized in 1989, so it's not that modern...
I posted the entry because I was fixing a lot of warnings in OpenSG, and signed/unsigned comparisons happened to be a lot of them. I had just talked to my roommate about interview questions too.
I'm going to be posting an entry later clarifying the issue...
My mentor just got a LiveJournal. :)
Where did you get it? And why? o_O Sister + Ebichu = Disturbing
I got it from a friend of a friend... I thought it was adorable.
Don't worry, I've never seen Ebichu, but I know what it's about... however, taken out of context, the icon is precious. ^_^
And how do YOU know about Ebichu, may I ask? o_O
Mike and I have been watching it for the last year or so. We're up to episode 16. It's hilarious.
The best part of Ebichu is that it starts out as something a 9 year old would watch. Then Ebichu gets punched into a wall by her owner and explodes into a spectacular explosion of blood as she slams into the wall with a satisfyingly wet splatting sound.
After that, the chick who punched Ebichu jumps into the sack with her boyfriend for awhile.
Wholesome!