-4

My friends and I are having an argument about this little piece of code:

#include <stdio.h>
#include <stdio.h>

int foo (int k) 
{
    int i, n;

    for (i = i ? 0 : i, n ^= n; i < sizeof(k) * 8;)
        n += k >> i++ & ~-2;
    return n;
}

I suspect it won't get compiled because i is uninitialized, but my friends think it will. What do you think?

styx
  • 1,852
  • 1
  • 11
  • 22
  • 4
    Did you try compiling it yourself? – chrisaycock Dec 18 '13 at 01:55
  • i did on VC IT dosent compile but on GCC and ideone.com it does – styx Dec 18 '13 at 01:57
  • 3
    It's a draw then. Nobody wins but we all lost a little something... – Lee Taylor Dec 18 '13 at 01:59
  • Not related to your question but `for (i=i?0:i` what are you trying to do here? – PakkuDon Dec 18 '13 at 01:59
  • 3
    You and your friends must be very popular. – Fiddling Bits Dec 18 '13 at 01:59
  • PakkuDan: its a question i got as part of a job interview thats all – styx Dec 18 '13 at 02:05
  • 1
    The initial assignments to 'i' and 'n' in the 'for' loop will always produce zero, even though they weren't initialized. I don't see what purpose was served by asking you about this rubbish in the interview, unless either they're worried that you will understand it and are therefore capable of producing it, or they already have this kind of garbage in their codebase. In either case I think you dodged a bullet. – user207421 Dec 18 '13 at 02:12
  • A lot of edits. This question has had some major scrubbing! – Fiddling Bits Dec 18 '13 at 02:15
  • Smells like homework. Why is it so difficult for you to plug it into a compiler? – Yick Leung Dec 18 '13 at 02:56
  • @EJP: The initial assignments to `i` and `n` are Undefined Behavior becaue they use the variable before initialization. Maybe the interview was trying to check whether the OP grasped the subtleties of the C and UB. – rodrigo Dec 18 '13 at 09:33
  • @rodrigo Of course it's UB, but the result is still zero in both cases. Check the code. If you wish to debate this further, first find a value of *i* or *n* for which that isn't so. We don't have any idea what the interviewer was looking for, and personally I don't care. – user207421 Dec 18 '13 at 10:32

2 Answers2

5

That code seems perfectly valid, from a syntax point of view. So it should be compilable.

But if you try to run it... it has so many undefined behaviors that I stopped counting.

The i is not undefined, it is uninitialized. It is perfectly defined in the local variable definition. C is not Java, variables don't have to be definitely initialized to be used.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • 'i' and 'n' will be zero when the body of the loop first executes, regardless of their initial values. – user207421 Dec 18 '13 at 02:15
  • @EJP: No, `n ^= n` would result in 0 for any specific value of `n`, but if it is uninitialized that is UB, and you have no guaranties whatsoever. – rodrigo Dec 18 '13 at 09:31
  • @rodrigo *n ^= n* is zero for any value of n, period, whether 'specific' or otherwise, whatever that means. – user207421 Dec 18 '13 at 10:35
  • @EJP: I wish things were as simple, but they are not. UB has priority over any logic you may think of. Plenty of [discussions in SO about this](http://stackoverflow.com/questions/15268799/uninitialized-variable-in-c/15268869#15268869). – rodrigo Dec 18 '13 at 11:12
1

It should be okay since you did declare i as an int variable. I compiled your code through my compiler (GCC), it looks like your code can be compiled. I ran it by substituted some integers into your foo function and it seems to be returning integers fine. For example, foo(5) will yield 2.

Yick Leung
  • 1,048
  • 1
  • 9
  • 16