Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Unexpected answer, compiler bug?

Reply
Thread Tools

Unexpected answer, compiler bug?

 
 
Port Pirie
Guest
Posts: n/a
 
      12-25-2007
Hi,

I ran the program below. It should print out 2 but actually it prints
out 1. What's going on? Should I report a bug against the compiler?

Thanks.


void main()
{
int a=1;
a=a++;
printf("%d", a);
}
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      12-25-2007
Port Pirie wrote:

> Hi,
>
> I ran the program below. It should print out 2 but actually it prints
> out 1. What's going on? Should I report a bug against the compiler?
>
> Thanks.
>
>
> void main()
> {
> int a=1;
> a=a++;
> printf("%d", a);
> }


You have invoked undefined behaviour thrice. Firstly void main is not
the proper prototype. Proper forms are int main(void) or int main(int,
char**). Secondly you access an object to modify it's value twice
within a sequence point. Thirdly you are calling a variadic function
with no prototype in scope.

Please read the FAQ at the following link:

<http://www.c-faq.com/>
<http://www.clc-wiki.net/>

 
Reply With Quote
 
 
 
 
Chris Torek
Guest
Posts: n/a
 
      12-25-2007
In article <fkqn1v$cgq$(E-Mail Removed)> Port Pirie <(E-Mail Removed)> wrote:
>I ran the program below. It should print out 2 but actually it prints
>out 1. What's going on? Should I report a bug against the compiler?
>
>void main()
>{
> int a=1;
> a=a++;
> printf("%d", a);
>}


You have asked a variant of Frequently Asked Questions 3.1, 3.2, and 3.3.
See <http://c-faq.com/expr/index.html>. See also questions 11.12a through
11.15 at <http://c-faq.com/ansi/index.html>.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      12-25-2007
"santosh" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fkqnl6$pn7$(E-Mail Removed)...
> Port Pirie wrote:
>
>> Hi,
>>
>> I ran the program below. It should print out 2 but actually it prints
>> out 1. What's going on? Should I report a bug against the compiler?
>>
>> Thanks.
>>
>>
>> void main()
>> {
>> int a=1;
>> a=a++;
>> printf("%d", a);
>> }

>
> You have invoked undefined behaviour thrice.

Four times actually

> Firstly void main is not
> the proper prototype. Proper forms are int main(void) or int main(int,
> char**). Secondly you access an object to modify it's value twice
> within a sequence point. Thirdly you are calling a variadic function
> with no prototype in scope.

and fourth you don't end your output with a \n

bye, Jojo


 
Reply With Quote
 
NicoleaSW@gmail.com
Guest
Posts: n/a
 
      12-25-2007
On Dec 25, 12:46 pm, Port Pirie <(E-Mail Removed)> wrote:
> Hi,
>
> I ran the program below. It should print out 2 but actually it prints
> out 1. What's going on? Should I report a bug against the compiler?
>
> Thanks.
>
> void main()
> {
> int a=1;
> a=a++;
> printf("%d", a);
>
> }


The key point to your answer is the line:
a=a++;

it evaluates a to 1 in right side, assigns 1 to a in left side, then
increments a used in right side. Use prefix increment instead, like
this:

a = ++a;

Of course, it's advisable you take into account all other advises
about prototype of main() and enting output with \n.
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      12-25-2007
On Tue, 25 Dec 2007 14:07:58 +0100, Joachim Schmitz wrote:
> "santosh" <(E-Mail Removed)> schrieb im Newsbeitrag
>> [...]
>> You have invoked undefined behaviour thrice.

> Four times actually
> [...]
> and fourth you don't end your output with a \n


Whether that is required is implementation-defined. When it's not
required, and your program don't provide one, your program is slightly
less portable, but still has defined behaviour.
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      12-25-2007
On Tue, 25 Dec 2007 05:23:40 -0800, NicoleaSW wrote:
> The key point to your answer is the line: a=a++;
>
> it evaluates a to 1 in right side, assigns 1 to a in left side, then
> increments a used in right side. Use prefix increment instead, like
> this:
>
> a = ++a;


This is exactly as broken as the original code. If you want to increment
a, write a++, or ++a, or a += 1, or a = a + 1, or any other alternative
that you can think of that updates a _once_. Even if it were valid C,
there is no point in saying you want to update a, and then store the
result in a. Updating a already stores the result in a. Trying to store
it again makes no sense.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-25-2007
Port Pirie wrote:
> Hi,
>
> I ran the program below. It should print out 2 but actually it prints
> out 1. What's going on? Should I report a bug against the compiler?


Your code (below) contains at least three errors (four under the old C89
standard). It cannot reasonably be expected to have any particular
behavior. However, if you wanted to guess what the behavior might be of
this incredibly error-laden code, consider this code block:

{
int a = 1;
int c;
c = a++; /* a++ has the value 1, that value is assigned to c */
}

Does that give you a hint?
If you do not understand why a++ has the value 1, open any elementary C
text and read the very first page in which the post-increment operator
++ is introduced.

> void main()
> {
> int a=1;
> a=a++;
> printf("%d", a);
> }

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-25-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> The key point to your answer is the line:
> a=a++;
>
> it evaluates a to 1 in right side, assigns 1 to a in left side, then
> increments a used in right side.


You might think so, but you would be horribly wrong.
This line has completely undefinied behavior.

> Use prefix increment instead, like
> this:
>
> a = ++a;


You might think so, but you would be horribly wrong.
This line has completely undefinied behavior.

> Of course, it's advisable you take into account all other advises
> about prototype of main() and enting output with \n.


Of course, it's advisable for you to learn about sequence points before
posting "advice".
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      12-25-2007
(E-Mail Removed) wrote:

> On Dec 25, 12:46 pm, Port Pirie <(E-Mail Removed)> wrote:
>> Hi,
>>
>> I ran the program below. It should print out 2 but actually it prints
>> out 1. What's going on? Should I report a bug against the compiler?
>>
>> Thanks.
>>
>> void main()
>> {
>> int a=1;
>> a=a++;
>> printf("%d", a);
>>
>> }

>
> The key point to your answer is the line:
> a=a++;
>
> it evaluates a to 1 in right side, assigns 1 to a in left side, then
> increments a used in right side.


Wherever you learned that from, reconsider its educational value;
it's wrong. It's a /permitted/ implementation, but only because
the expression `a = a++` has /no/ defined behaviour -- anything
and everything is permitted.

There are specific words in the Standard that, amongst other things,
make undefined the effects of writing to the same location multiple
times without an intervening "sequence point". Don't Do That.

--
Oh Noes! I Has Meltdownz! Hedgehog
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unexpected compiler behavior relating to size_t and boost - VisualStudio 2005. Unknownmat C++ 9 07-15-2008 07:38 AM
Compiler Error Message: CS0007: Unexpected common language... FrigginMook ASP .Net 3 12-29-2007 10:15 AM
Compiler Error Message: The compiler failed with error code -1073741819 Ram ASP .Net 0 09-13-2005 09:52 AM
Can we use <compiler> tag to avoid RunTime Compiler error? Jack Wright ASP .Net 5 01-19-2004 04:36 PM
Compiler Error Message: The compiler failed with error code 128. Yan ASP .Net 0 07-21-2003 10:49 PM



Advertisments