Chad Austin ([info]aegisknight) wrote,
@ 2003-11-05 12:43:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Good Interview Question
if (-1 < 10U) {
    foo();
} else {
    bar();
}

Which function is called? (pssst. no cheating with the help of your compiler.)




(Post a new comment)


[info]sorcy7
2003-11-05 11:15 am UTC (link)
I've never seen that 'U' near a number before :-\ . I'll try to guess, maybe foo() ? :-/ ( just guessing )

(Reply to this) (Thread)


[info]aegisknight
2003-11-05 11:17 am UTC (link)
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) {
....

(Reply to this) (Parent)(Thread)


[info]aegisknight
2003-11-05 11:18 am UTC (link)
Er... I meant they're signed by default.

(Reply to this) (Parent)


[info]lordgalbalan
2003-11-05 12:58 pm UTC (link)
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++.

(Reply to this) (Thread)


[info]aegisknight
2003-11-05 01:01 pm UTC (link)
10U is an integer literal of 'unsigned int' type and value 10, in both C and C++.

(Reply to this) (Parent)(Thread)


[info]lordgalbalan
2003-11-06 02:46 pm UTC (link)
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?

(Reply to this) (Parent)(Thread)


[info]aegisknight
2003-11-06 04:34 pm UTC (link)
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. :)

(Reply to this) (Parent)


[info]bssnchica
2003-11-05 01:31 pm UTC (link)
Hah. You got me.

You and your computer mumbo-jumbo... :-p

(Reply to this) (Thread)


[info]aegisknight
2003-11-05 06:32 pm UTC (link)
Is your icon from Ebichu? o_O

(Reply to this) (Parent)(Thread)


[info]bssnchica
2003-11-06 02:15 pm UTC (link)
Mayyyybe :)

(Reply to this) (Parent)(Thread)


[info]aegisknight
2003-11-06 04:35 pm UTC (link)
Where did you get it? And why? o_O Sister + Ebichu = Disturbing

(Reply to this) (Parent)(Thread)


[info]bssnchica
2003-11-06 06:11 pm UTC (link)
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. ^_^

(Reply to this) (Parent)


[info]bssnchica
2003-11-06 06:59 pm UTC (link)
And how do YOU know about Ebichu, may I ask? o_O

(Reply to this) (Parent)(Thread)


[info]aegisknight
2003-11-06 09:27 pm UTC (link)
Mike and I have been watching it for the last year or so. We're up to episode 16. It's hilarious.

(Reply to this) (Parent)


[info]thespeedbump
2003-11-06 09:31 pm UTC (link)
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!

(Reply to this) (Parent)


[info]thespeedbump
2003-11-05 05:16 pm UTC (link)
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();)

(Reply to this) (Thread)


[info]aegisknight
2003-11-05 06:24 pm UTC (link)
Neither left nor right operands get any special treatment when it comes to type promotion. ((-1 < 10U) is equivalent to (10U > -1))

(Reply to this) (Parent)


[info]tomt64
2003-11-05 07:43 pm UTC (link)
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

(Reply to this)


[info]flik9x
2003-11-06 01:36 am UTC (link)
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... *

(Reply to this)

dunno
[info]dleary
2003-11-06 10:05 am UTC (link)
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.

(Reply to this)


Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…