avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] OT Generic C question


From: Trampas
Subject: RE: [avr-gcc-list] OT Generic C question
Date: Tue, 20 Sep 2005 07:45:17 -0400

First off this was code written by a newbie to C and embedded development as
such it would have been better to do i++ or i=i+1. But he did it as i=i++; 

The code was actually something like this:

char name[20];
char c;
int i;

i=0;
c=getchar();
while(c!=13)
{
    name[i]=c;
    c=getchar();
    i=i++;
}
name[i]=0;
printf("%d, %s\n\r",i,name);

Of course this code has overflow issues and such, but after the return
character was seen the printf statement printed that i==0. Changing from
i=i++; to i++; worked. 

Again my objective was to determine if this was a bug with compiler or not.
>From my understanding of the ++ operator this is indeed a bug. Sure it is
bad coding but still a bug.

Regards,
Trampas 
  
 

-----Original Message-----
From: David Brown [mailto:address@hidden 
Sent: Tuesday, September 20, 2005 7:48 AM
To: Trampas; address@hidden
Subject: Re: [avr-gcc-list] OT Generic C question

----- Original Message -----
From: "Trampas" <address@hidden>
> I was helping a friend debug some code, he is new to C,  using the Keil
> version of GCC for ARM. Anyway I found the following:
>
> int i;
>
> i=0;
> i=i++;
> //i was still zero that
>
> That is i=i++ never incremented i, now I would have thought the line would
> be the same as:
>
> i=i;
> i=i+1;
>
> So you guys are the smartest people I know when it comes to C so I thought
I
> would ask you guys if this is a compiler bug or is my understanding of C
> just been shaken.
>
> Regards,
> Trampas
>
>

I'd agree with you that i should be 1 after "i = i++", despite the sillyness
of the statement.  However, how did you check that i was still 0?  If the
compiler had no reason to actually carry out the incrementation, and you
used a debugger to view "i", then it's quite likely that it remained at 0
due to legitimate compiler optomisations.








reply via email to

[Prev in Thread] Current Thread [Next in Thread]