Search the web
Sign In
New User? Sign Up
c-prog · C/C++ Programmer's Mailing List
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Show off your group to the world. Share a photo of your group with us.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
how to solve it   Message List  
Reply | Forward Message #70971 of 71513 |
Re: [c-prog] how to solve it

On Tue, Jul 14, 2009 at 5:21 AM, anurag JAIN <informanuragjain@...> wrote:
>
> Hi,
>
> Initially the value of a is 5.
>
> c=a/++a; means
> c= 5/6

No it doesn't. It's permissible for the compiler to _also_ interpret it as:
c = 6/6

> ; so C=0;

... so that's wrong.

Neither method is wrong, and because both are valid, the construct
'a/++a' shouldn't be used in C because it is not well defined.

> d=++a/a++; means
> d= 7/7;

No it doesn't. Another permissible interpretation is

d = 7/6

--
PJH

http://shabbleland.myminicity.com/com
http://www.chavgangs.com/register.php?referer=9375



Wed Jul 15, 2009 5:12 am

shabble
Offline Offline
Send Email Send Email

Forward
Message #70971 of 71513 |
Expand Messages Author Sort by Date

int a=5,c,d; c=a/++a; d=++a/a++; plz tell me how to evaluate these expression...
manish_m95
Offline Send Email
Jul 13, 2009
3:59 pm

... You don't. They're undefined. See http://c-faq.com/expr/evalorder2.html In both cases, you can never know which out of the denominator or numerator is...
Paul Herring
shabble
Offline Send Email
Jul 13, 2009
4:09 pm

Hi,   Initially the value of a is 5.   c=a/++a; means c= 5/6 ; so C=0;   d=++a/a++; means d= 7/7; so d=1;   Finally Value of a=8; d=1; c=0;     ...
anurag JAIN
informanurag...
Offline Send Email
Jul 15, 2009
4:59 am

... No it doesn't. It's permissible for the compiler to _also_ interpret it as: c = 6/6 ... ... so that's wrong. Neither method is wrong, and because both are...
Paul Herring
shabble
Offline Send Email
Jul 15, 2009
5:13 am

How it could be? is it possible after initializing a=5 ,6 could divided by 6 ,It could possible after dividing a by ++a,a will be six ...
Nayera Khan
nayera.khan
Offline Send Email
Jul 15, 2009
11:22 am

... "It is also possible the core of the earth is made of cheese. We just won't know until we get there." It is also possible that your hard drive will be...
Thomas Hruska
shininglightpro
Offline Send Email
Jul 15, 2009
11:58 am

... Given int a = 5; and int b = a / ++a; You don't know (and should never assume) whether the compiler will look at: o the 'a' first (and the '++a' second)...
Paul Herring
shabble
Offline Send Email
Jul 16, 2009
3:45 pm

A way to help determine how you want the following scenario to play out is to surround the piece of code you want to be sure is enacted on first with ( ). For...
Michael White
michael_white2
Offline Send Email
Jul 16, 2009
4:18 pm

... No it won't. Adding parenthesis does absolutely nothing in this situation. It's still invalid code, because either the numerator or denominator can be...
Paul Herring
shabble
Offline Send Email
Jul 16, 2009
7:05 pm

Which compiler are you using? Every compiler I know reads within ( ) first, and then from right to the left. The only time the code I present would have a...
Michael White
michael_white2
Offline Send Email
Jul 16, 2009
7:21 pm

... This is the special case where the same variable is accessed (both read and write, in this case) multiple times between sequence points. That's undefined...
Brett McCoy
smartandkewl
Online Now Send Email
Jul 16, 2009
7:34 pm

Oh, so you're saying that it should be done more like.... int a; a = 6 / (++a); ... From: Brett McCoy <idragosani@...> Subject: Re: [c-prog] I am not...
Michael White
michael_white2
Offline Send Email
Jul 16, 2009
7:42 pm

... This applies to any compiler that attempts to adhere to The Standard(s.) ... No they don't. What the compilers do is first work out the value of each bit...
Paul Herring
shabble
Offline Send Email
Jul 16, 2009
8:20 pm

... I believe Paul's point is that incrementing a like that will be done before the numerator is taken into account, which will give you 1 every time. int a =...
Christopher Coale
chris95219@...
Send Email
Jul 17, 2009
4:17 am

... Only if a and b were floats ;) (As I mentioned before, this is integer arithmetic) -- PJH http://shabbleland.myminicity.com/com ...
Paul Herring
shabble
Offline Send Email
Jul 17, 2009
8:56 am

In your original code, what's 'a' initialized to? It looks like 'a' is not initialized, so '++a' is incrementing an uninitialized variable as it seems to me....
Dan Presley
dpresley_50201
Offline Send Email
Jul 19, 2009
1:20 pm

It was an incomplete example <smile>. Yea, additional code would have been in a real program before the iteration happened. ... From: Dan Presley...
Michael White
michael_white2
Offline Send Email
Jul 19, 2009
1:25 pm

No: the issue is when you assign to a. You can always do something like int b=a/3+(a*4)-a; but when you use ++a your assigning/changing. ... From: Michael...
Tyler Littlefield
tyler@...
Send Email
Jul 16, 2009
7:53 pm

OK, never-mind my last posting. I didn't see the part about init'ing a to 5. So I tried your operation: #include <iostream> #include <cstdlib> using...
Dan Presley
dpresley_50201
Offline Send Email
Jul 19, 2009
1:20 pm

Just so you know... Everything to the right of the assignment operator '=' happens first. So, even if you wrote your code as '++a' or as 'a++', 'a' would still...
Michael White
michael_white2
Offline Send Email
Jul 19, 2009
1:31 pm

... There is no guarantee in C or C++ of that being the case. Unlike some other languages, = is just another operator. In C and C++, side effects are only...
peternilsson42
Offline Send Email
Jul 20, 2009
1:39 am

... Mom told me about putting a fork in the toaster, but when I tried it... ... The compiler is just one part of the implementation. Optimisors often work at a...
peternilsson42
Offline Send Email
Jul 20, 2009
5:18 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help