freelanceprogrammers.org Forum Index » C
help reg precedence
Joined: 25 Jul 2003
Posts: 4
help reg precedence
HI all,
can one expln order of evaluation here on gcc , turboc
main()
{
int i=5;
printf("%d
",i-- * i++);
}
2. diff b/w these 2 in terms of accessibility.
const char *p;andchar * const p;
regards,
ashwini
SMS using the Yahoo! Messenger;
Download latest version.
Joined: 19 Jul 2003
Posts: 29
help reg precedence
> can one expln order of evaluation here on gcc ,
> turboc
> main()
> {
> int i=5;
> printf("%d
",i-- * i++);
> }
25 on GCC, quite expectedly. I don`t think compilers
can vary so much on such general issues. This one, is
in fact no issue at all. C-precedences rule.
I have used Turbo C++ 3.0 as its latest version, but
in quite a long time now. It`s bad at compiling C++
code, but should produce the same result here, that is
--> 25 since this is purely C.
>
> 2. diff b/w these 2 in terms of accessibility.
>
> const char *p;
> and
> char * const p;
What was that again? Just change in the order of
writing?
Regards,
Shantanu
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
Joined: 25 Jul 2003
Posts: 48
help reg precedence
Reply/comments embedded...
> -----Original Message-----
> From: Shantanu Kumar [mailto:shantanu_k06@...]
> Sent: Friday, July 25, 2003 09:45
> To: Programmers-Town@yahoogroups.com
> Subject: Re: *(&Programmers-Town&)* help reg precedence
>
> > can one expln order of evaluation here on gcc ,
> > turboc
> > main()
> > {
> > int i=5;
> > printf("%d
",i-- * i++);
> > }
>
> 25 on GCC, quite expectedly. I don`t think compilers
> can vary so much on such general issues. This one, is
> in fact no issue at all. C-precedences rule.
>
> I have used Turbo C++ 3.0 as its latest version, but
> in quite a long time now. It`s bad at compiling C++
> code, but should produce the same result here, that is
> --> 25 since this is purely C.
I think not. This is another undefined behavior. Precedence is not the
issue here. See:
< http://www.eskimo.com/~scs/C-faq/q3.4.html >
The compiler is free to evaluate either side of the multiplication
operator as it seems fit before applying the multiplication. Further,
if any sub-expression is undefined, the entire statement is undefined.
>
> >
> > 2. diff b/w these 2 in terms of accessibility.
> >
> > const char *p;
> > and
> > char * const p;
>
> What was that again? Just change in the order of
> writing?
Changing the order in this case meant totally different thing.
For the first case:
const char *p
| \_______/
| ^
| |
+-------+
Here, `const` apply to the `char *`. This means the stuff the pointer
pointed to is constant.
For the second line:
char * const p
| \___/
| ^
| |
+-----+
Here, `const` apply to `p`, the pointer. This means the pointer itself
is constant. The stuff `p` points to is not.
HTH
Shyan
Joined: 26 Jul 2003
Posts: 4
help reg precedence
--- Shantanu Kumar <shantanu_k06@...> wrote:
> > can one expln order of evaluation here on gcc
> ,
> > turboc
> > main()
> > {
> > int i=5;
> > printf("%d
",i-- * i++);
> > }
>
> 25 on GCC, quite expectedly. I don`t think compilers
> can vary so much on such general issues. This one,
> is
> in fact no issue at all. C-precedences rule.
as far as i know this is undefined plz refer following
link it deals with something called sequnce points
http://www.eskimo.com/~scs/C-faq/q3.2.html
>
> I have used Turbo C++ 3.0 as its latest version, but
> in quite a long time now. It`s bad at compiling C++
> code, but should produce the same result here, that
> is
> --> 25 since this is purely C.
>
> >
> > 2. diff b/w these 2 in terms of accessibility.
> >
> > const char *p;
This is pointer to constant char,
value in character to which p points can`t be
changed be pointer can be made point some other char
> > and
> > char * const p;
>
This is constant pointer to character
once pointer is pointed (when it is defined ) it can`t
be changed but value of character can be changed
> What was that again? Just change in the order of
> writing?
>
> Regards,
> Shantanu
>
Regards
Rajaram
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
Joined: 19 Jul 2003
Posts: 29
help reg precedence
--- Shyan Lam <lam@...> wrote:
> Reply/comments embedded...
>
> > -----Original Message-----
> > From: Shantanu Kumar
> [mailto:shantanu_k06@...]
> > Sent: Friday, July 25, 2003 09:45
> > To: Programmers-Town@yahoogroups.com
> > Subject: Re: *(&Programmers-Town&)* help reg
> precedence
> >
> > > can one expln order of evaluation here on
> gcc ,
> > > turboc
> > > main()
> > > {
> > > int i=5;
> > > printf("%d
",i-- * i++);
> > > }
> >
> > 25 on GCC, quite expectedly. I don`t think
> compilers
> > can vary so much on such general issues. This one,
> is
> > in fact no issue at all. C-precedences rule.
> >
> > I have used Turbo C++ 3.0 as its latest version,
> but
> > in quite a long time now. It`s bad at compiling
> C++
> > code, but should produce the same result here,
> that is
> > --> 25 since this is purely C.
>
> I think not. This is another undefined behavior.
> Precedence is not the
> issue here. See:
> < http://www.eskimo.com/~scs/C-faq/q3.4.html >
>
> The compiler is free to evaluate either side of the
> multiplication
> operator as it seems fit before applying the
> multiplication. Further,
> if any sub-expression is undefined, the entire
> statement is undefined.
Since both i-- and i++ use "post" (decrement &
increment respectively) operators, I think it`s almost
certain that they (-- & ++ operators) will be
individually excuted only after the
expression/sub-expression (in which they are
contained) done. In this example, it will probably
degenerate as
1. (i * i)
2. either of i--, i++
3. vice versa of 2 above
That`s what I wanted to point by saying precendences
-- "post"-increment/decrement operators. Please
correct me if I am wrong. Does any compiler really
produce some other value? I should be surprised if
they do. :-)
Regards,
Shantanu
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
Joined: 26 Jul 2003
Posts: 20
help reg precedence
> > > main()
> > > {
> > > int i=5;
> > > printf("%d
",i-- * i++);
> > > }
Its best not to use these kind of non-standard statements.
Cheers,
Joe
--
Software is like sex, its better when it is free.
- Linus Torvalds
Visit:http://www.joesteeve.tk/
PS:> I hate HTML mail
Joined: 25 Jul 2003
Posts: 48
help reg precedence
See embedded comments...
> -----Original Message-----
> From: Shantanu Kumar [mailto:shantanu_k06@...]
> Sent: Saturday, July 26, 2003 05:27
> To: Programmers-Town@yahoogroups.com
> Subject: RE: *(&Programmers-Town&)* help reg precedence
>
> Since both i-- and i++ use "post" (decrement &
> increment respectively) operators, I think it`s almost
> certain that they (-- & ++ operators) will be
> individually excuted only after the
> expression/sub-expression (in which they are
> contained) done. In this example, it will probably
> degenerate as
>
> 1. (i * i)
This is not guaranteed. When one of the operand is being evaluated, you
cannot be sure at which moment the side effect (either decrement or
increment) will take place. Be it before or after the evaluation of the
other operand.
> 2. either of i--, i++
The fact that you use the word "either" illustrates the "undefined"
behavior.
> 3. vice versa of 2 above
>
> That`s what I wanted to point by saying precendences
> -- "post"-increment/decrement operators. Please
> correct me if I am wrong. Does any compiler really
> produce some other value? I should be surprised if
> they do. :-)
The side effects only get settled down after the sequence point. If you
reference the variable more than once before the sequence point, what
can you said about its value?
To illustrate it, consider the following expression:
x = a-- * b++;
The compiler can generate at least the following interpretation:
(1) x = a * b
a = a-1
b = b+1
(2) x = a * b
b = b+1
a = a-1
(3) tmp = b
b = b+1
x = a * tmp
a = a-1
(4) tmp = a
a = a-1
x = tmp * b
b = b+1
(5) tmpa = a
tmpb = b
a = a-1
b = b+1
x = tmpa * tmpb;
If both `a` and `b` are 10, at the end, `x` will be 100, both `a` and
`b` will be 9 and 11 respectively.
Now replace `a` and `b` by `i`, if `i` is 10, we`ll have at least three
possible results for `x`: 90, 100 and 110.
Hence "undefined" behavior!
HTH
Shyan
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







