Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

just want detail information

0 views
Skip to first unread message

ashu

unread,
Apr 22, 2007, 6:16:59 AM4/22/07
to
main()
{
int a = 1;
int c,d;
c = ++a + ++a + ++a;
printf("\n%d",a);
printf("\n%d ", c);
getch();
}

The above given code gives the value of "a" and "c" as 4 and 12 resp.

Can anyone tell me why it is so...

pete

unread,
Apr 22, 2007, 6:27:37 AM4/22/07
to

Mark L Pappin

unread,
Apr 22, 2007, 6:35:06 AM4/22/07
to
ashu <ashishm...@gmail.com> writes:

> c = ++a + ++a + ++a;

> The above given code gives the value of "a" and "c" as 4 and 12 resp.


>
> Can anyone tell me why it is so...

Because you were unlucky.

If you were lucky, the compiler would have refused to compile this
source.

If you were using the compiler I have not yet written, it would have
deleted the source file and displayed the relevant part of the
standard (that explicitly describes this as Undefined Behaviour) in
high-contrast lettering until you passed a Turing test it administered
to ensure you understood the point.

mlp

ashu

unread,
Apr 22, 2007, 6:51:03 AM4/22/07
to
On Apr 22, 2:35 am, Mark L Pappin <m...@acm.org> wrote:

actually i want to abt one specific line
" c=++a + ++a + ++a;
how compiler calculate it and y? will it be different on different
compilers.
i think it should taken as
c= 2+3+4.

Ian Collins

unread,
Apr 22, 2007, 7:02:16 AM4/22/07
to
ashu wrote:
> On Apr 22, 2:35 am, Mark L Pappin <m...@acm.org> wrote:
>
>>ashu <ashishmoury...@gmail.com> writes:
>>
>>>c = ++a + ++a + ++a;
>>>The above given code gives the value of "a" and "c" as 4 and 12 resp.
>>
>>>Can anyone tell me why it is so...
>>
>>Because you were unlucky.
>>
>>If you were lucky, the compiler would have refused to compile this
>>source.
>>
>>If you were using the compiler I have not yet written, it would have
>>deleted the source file and displayed the relevant part of the
>>standard (that explicitly describes this as Undefined Behaviour) in
>>high-contrast lettering until you passed a Turing test it administered
>>to ensure you understood the point.
>>
>
> actually i want to abt one specific line
> " c=++a + ++a + ++a;
> how compiler calculate it and y? will it be different on different
> compilers.
> i think it should taken as
> c= 2+3+4.
>
Please don't use silly txtspk abbreviations on Usenet.

The line in question invokes undefined behaviour.

--
Ian Collins.

CBFalconer

unread,
Apr 22, 2007, 9:06:10 AM4/22/07
to
ashu wrote:
>
error 1, no #include <anything>

> main()

error 2, no type for main, no specification of parameters.

> {
> int a = 1;
> int c,d;
> c = ++a + ++a + ++a;

error 3, undefined behaviour.

> printf("\n%d",a);
> printf("\n%d ", c);

error 4, no final '\n'

> getch();

error 5, unknown function in std. C
error 6, failure to return a value.

> }
>
> The above given code gives the value of "a" and "c" as 4 and 12
> resp. Can anyone tell me why it is so...


--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline.net

--
Posted via a free Usenet account from http://www.teranews.com

pete

unread,
Apr 22, 2007, 10:20:39 AM4/22/07
to

The compiler can do whatever it wants with that one specific line.

The variable (a), is read 3 times and modified 3 times
without an intervening sequence point.
Even though it seems as though the maximum value can be 12
if the initial value of (a) is 1
(a) is incremented 3 times to a value of 4
and then (4 + 4 + 4 == 12)
and that the minimum value can be 6
(++a) == 2, and then (2 + 2 + 2 == 6)
and then (a) is incremented 3 times to become 4
the case is that the C standard committee decided that
code like that is garbage and not worth the trouble of defining.

Read the document at the below URL:

http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/C-faq/q3.8.html

--
pete

zero

unread,
Apr 22, 2007, 11:06:57 AM4/22/07
to
Mark L Pappin <m...@acm.org> wrote in news:m3mz10k...@Claudio.Messina:

>
> If you were using the compiler I have not yet written, it would have
> deleted the source file and displayed the relevant part of the
> standard (that explicitly describes this as Undefined Behaviour) in
> high-contrast lettering until you passed a Turing test it administered
> to ensure you understood the point.
>

Now that sounds like a compiler I'd be interested in :-)

Zero

zero

unread,
Apr 22, 2007, 11:10:10 AM4/22/07
to
pete <pfi...@mindspring.com> wrote in
news:462B6F...@mindspring.com:

> ashu wrote:
>>
>> actually i want to abt one specific line
>> " c=++a + ++a + ++a;
>> how compiler calculate it and y?
>
> The compiler can do whatever it wants with that one specific line.
>

actually the compiler can whatever it want with the whole program. Using
undefined behaviour anywhere in a program makes the whole thing undefined.

Zero

Army1987

unread,
Apr 22, 2007, 12:06:23 PM4/22/07
to
"CBFalconer" <cbfal...@yahoo.com> ha scritto nel messaggio
news:462B5DC2...@yahoo.com...
> ashu wrote:

>> main()

> error 2, no type for main, no specification of parameters.

In C89 it's not an error, just bas style.


Keith Thompson

unread,
Apr 22, 2007, 4:22:45 PM4/22/07
to

Pete, did you post this URL to call attention to the apparent
copyright violation?

The *real* comp.lang.c FAQ is at <http://www.c-faq.com/>.
docs.mandragor.org appears to have an unauthorized copy of an earlier
version of it. The wording for question 3.2 differs from that in the
real FAQ.

Amazingly enough, mandragor.com even reproduces (and old version of)
Steve Summit's copyright message:

This collection of hypertext pages is Copyright 1995 by Steve
Summit. Content from the book "C Programming FAQs: Frequently
Asked Questions" (Addison-Wesley, 1995, ISBN 0-201-84519-9) is
made available here by permission of the author and the publisher
as a service to the community. It is intended to complement the
use of the published text and is protected by international
copyright laws. The content is made available here and may be
accessed freely for personal use but may not be published or
retransmitted without written permission.

I'll send Steve an e-mail message about this.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson

unread,
Apr 22, 2007, 4:50:04 PM4/22/07
to
pete <pfi...@mindspring.com> writes:
[...]

No, visit the *real* FAQ at <http://www.c-faq.com> and read question
3.8. mandragor.org has a copy of an old version of the FAQ, probably
in violation of its copyright. I've notified the site's maintainer
and Steve Summit.

pete

unread,
Apr 23, 2007, 4:14:27 AM4/23/07
to
Keith Thompson wrote:

>
> pete <pfi...@mindspring.com> writes:
>
http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/C-faq/q3.2.html
>
> Pete, did you post this URL to call attention to the apparent
> copyright violation?

No.
I couldn't remember the URL so I googled "clc faq", and I got:
http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/
which says that it is "Steve Summit's home page"

>
> The *real* comp.lang.c FAQ is at <http://www.c-faq.com/>.
> docs.mandragor.org appears to have an unauthorized copy of an earlier
> version of it. The wording for question 3.2 differs from that in the
> real FAQ.
>
> Amazingly enough, mandragor.com even reproduces (and old version of)
> Steve Summit's copyright message:
>
> This collection of hypertext pages is Copyright 1995 by Steve
> Summit. Content from the book "C Programming FAQs: Frequently
> Asked Questions" (Addison-Wesley, 1995, ISBN 0-201-84519-9) is
> made available here by permission of the author and the publisher
> as a service to the community. It is intended to complement the
> use of the published text and is protected by international
> copyright laws. The content is made available here and may be
> accessed freely for personal use but may not be published or
> retransmitted without written permission.
>
> I'll send Steve an e-mail message about this.

--
pete

Mark L Pappin

unread,
Apr 23, 2007, 4:55:56 AM4/23/07
to
zero <ze...@this.hi> writes:

> Mark L Pappin <m...@acm.org> wrote:

>> If you were using the compiler I have not yet written, it would
>> have deleted the source file and displayed the relevant part of the
>> standard (that explicitly describes this as Undefined Behaviour) in
>> high-contrast lettering until you passed a Turing test it
>> administered to ensure you understood the point.

> Now that sounds like a compiler I'd be interested in :-)

And that's only the default, before you increase the severity of the
diagnostics.

It should be available for the DeathStation 9000 in mid-October 1993,
with other architectures following customer demand.

mlp

Flash Gordon

unread,
Apr 23, 2007, 6:31:57 AM4/23/07
to
pete wrote, On 23/04/07 09:14:

> Keith Thompson wrote:
>> pete <pfi...@mindspring.com> writes:
> >
> http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/C-faq/q3.2.html
>> Pete, did you post this URL to call attention to the apparent
>> copyright violation?
>
> No.
> I couldn't remember the URL so I googled "clc faq", and I got:
> http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/
> which says that it is "Steve Summit's home page"

They seem to have archived a version of his site, wrapped it in their
own page, and failed to acknowledge what they have done.

Steve's REAL home page is http://www.eskimo.com/~scs/ which has a
pointer to the correct location of the FAQ.

<snip>

>> I'll send Steve an e-mail message about this.

Good. I would say this was a clear cut case of copyright violation. The
same probably applies to a lot of the stuff on mandragor.org
--
Flash Gordon

Keith Thompson

unread,
Apr 23, 2007, 5:04:03 PM4/23/07
to
pete <pfi...@mindspring.com> writes:
> Keith Thompson wrote:
>>
>> pete <pfi...@mindspring.com> writes:
> >
> http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/C-faq/q3.2.html
>>
>> Pete, did you post this URL to call attention to the apparent
>> copyright violation?
>
> No.
> I couldn't remember the URL so I googled "clc faq", and I got:
> http://docs.mandragor.org/files/Programming_languages/C/clc_faq_en/
> which says that it is "Steve Summit's home page"

Interesting. A Google search for "clc faq" (without the quotation
marks) doesn't turn up the actual FAQ anywhere in the top 100 hits.

But http://www.c-faq.com is the *first* hit in a search for "c faq" or
for "comp.lang.c faq". If you need to find the real FAQ, use one of
those search terms (or set a bookmark).

>> The *real* comp.lang.c FAQ is at <http://www.c-faq.com/>.
>> docs.mandragor.org appears to have an unauthorized copy of an earlier
>> version of it. The wording for question 3.2 differs from that in the
>> real FAQ.
>>
>> Amazingly enough, mandragor.com even reproduces (and old version of)
>> Steve Summit's copyright message:
>>
>> This collection of hypertext pages is Copyright 1995 by Steve
>> Summit. Content from the book "C Programming FAQs: Frequently
>> Asked Questions" (Addison-Wesley, 1995, ISBN 0-201-84519-9) is
>> made available here by permission of the author and the publisher
>> as a service to the community. It is intended to complement the
>> use of the published text and is protected by international
>> copyright laws. The content is made available here and may be
>> accessed freely for personal use but may not be published or
>> retransmitted without written permission.
>>
>> I'll send Steve an e-mail message about this.

I tried to contact Steve and the owner of the web site; the latter
bounced with a "mailbox is full" message. The site's main page says:

We're spending a lot of time in selecting the documentation
available here, especially because of the copyrights. If despite
our attention, some file we shouldn't redistribute are on this
website, please mail us quickly.

so hopefully this will be straightened out amicably.

Army1987

unread,
Apr 24, 2007, 1:30:43 PM4/24/07
to
"Mark L Pappin" <m...@acm.org> ha scritto nel messaggio
news:m3irbnj...@Claudio.Messina...

> zero <ze...@this.hi> writes:
>
>> Mark L Pappin <m...@acm.org> wrote:
>
>>> If you were using the compiler I have not yet written, it would
>>> have deleted the source file and displayed the relevant part of the
>>> standard (that explicitly describes this as Undefined Behaviour) in
>>> high-contrast lettering until you passed a Turing test it
>>> administered to ensure you understood the point.
>
>> Now that sounds like a compiler I'd be interested in :-)
>
> And that's only the default, before you increase the severity of the
> diagnostics.

What will it do when you increase it? :-)


0 new messages