just want detail information

A

ashu

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...
 
M

Mark L Pappin

ashu said:
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
 
A

ashu

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

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.
 
I

Ian Collins

ashu said:
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.
 
C

CBFalconer

error 1 said:

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'

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...
 
P

pete

ashu said:
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.

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
 
Z

zero

Mark L Pappin said:
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
 
Z

zero

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
 
K

Keith Thompson

pete said:

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.
 
M

Mark L Pappin

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
 
F

Flash Gordon

pete wrote, On 23/04/07 09:14:
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.


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
 
K

Keith Thompson

pete said:
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).

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top