[nycbug-talk] MSNBC on the decline of technology jobs

Bob Ippolito bob
Wed Jun 22 18:06:29 EDT 2005


On Jun 22, 2005, at 3:00 PM, Marc Spitzer wrote:

> On 6/22/05, alex at pilosoft.com <alex at pilosoft.com> wrote:
>
>> On Wed, 22 Jun 2005, Francisco Reyes wrote:
>>
>>
>> If you want me to give examples in software development, my favorite
>> interview question is "c=0; c=c++;" what is the value of c?  The  
>> right
>> answer is 'undefined', it can be 0, 1, or 500. Now, its all about how
>>
>
> actually I would say 1, compilers may differ but according to what I
> remember of C, been years, it should be 1.  Here is why:
>
> 1: c=0
> 2: c=c ( c=0)
> 3 : c++ ( c = 0 + 1)
>
> that is how it should work, gcc 3.3.3 agrees with me.  The 500 thing
> is not plausable, 0 and  1 are though.

I've never seen a compiler that would do anything but 1 here.

> here is a more fun one:
> c=0; c =+ ++c;
>
> here I would vote for 2, gcc says 1
> 1: c=0
> 2: ++c ( c= 0+1)
> 3 c= c+c (c = 1+1)

GCC says 1 because you don't remember C syntax correctly.  =+ isn't  
in-place addition, it is plain old assignment with a unary plus  
operator (effectively a no-op).  If you had done += then it would've  
been 2.

What GCC is doing here is this:

1. c=0
2. ++c (c = 0 + 1)
3. c = c (c = 1)

>> someone thinks about resolving this: Break the statement into  
>> operations,
>> and try to figure whether assignment or post-increment will happen  
>> first.
>>
>
> by definition post incrament happens after assignment, again compilers
> may vary but that is how it should work

Yeah, if the answer is not 1, then the compiler has a bug.

-bob





More information about the talk mailing list