Connecting Tech Pros Worldwide Forums | Help | Site Map

C programming

Newbie
 
Join Date: Jun 2007
Posts: 1
#1: Jun 22 '07
hi iam sundar really want to clear C concepts please help me I got stuck in operators while I faced this question
main()
{
i=5,j=10;
i=i&=j&&10;
printf("%d %d",i,j);
}
what does &= does mean. And what is the logic means how to solve these problems efficiently.

Member
 
Join Date: Mar 2007
Posts: 63
#2: Jun 22 '07

re: C programming


j&&10 ...is logical AND
so the result is ONE

i&=j&&10 will be bitwise AND i and the result ONE
i.e., five AND ONE ...
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#3: Jun 22 '07

re: C programming


Quote:

Originally Posted by clogics

hi iam sundar really want to clear C concepts please help me I got stuck in operators while I faced this question
main()
{
i=5,j=10;
i=i&=j&&10;
printf("%d %d",i,j);
}
what does &= does mean. And what is the logic means how to solve these problems efficiently.

Let's take this in pieces.

j && 10

is an expression using logical AND. Expressions are true or false. In C and C++ false is zero and true is not false. So, 5 is true and 10 is true so the exporession is true. The value of the expression is 1.

i& is bitwwise AND. A bitwise AND produces a 1 bit only if the the corresponding bits in the operands are both 1. In this case i is 5

00000101 <--- i
00000001 <----the value of j&&10
00000001 <--the result of the bitwise AND

i&= is the same bitwise AND but the result is put back into i. Based on the above, i noe has a value of 1.

i = i &= etc..

is pointless. i has already been changed by the & bitwise AND so this really is

i = i
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jun 22 '07

re: C programming


I apologize for being a partypooper again but still: changing an lvalue more than
once before a sequence point is reached results in undefined behaviour.

kind regards,

Jos
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#5: Jun 22 '07

re: C programming


Quote:

Originally Posted by JosAH

I apologize for being a partypooper again but still: changing an lvalue more than
once before a sequence point is reached results in undefined behaviour.

I don't think so in this case. I think this one has to go R-L.
Expand|Select|Wrap|Line Numbers
  1. i=i&=j&&10;
  2.  
&& precedence 13
&= precedence 16
= precedence 16 R-L

That means the && goes first.
That means &= goes next because = associates R-L
That means = goes last.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Jun 22 '07

re: C programming


Quote:

Originally Posted by weaknessforcats

I don't think so in this case. I think this one has to go R-L.

Nope, sorry; compare it with this one:

Expand|Select|Wrap|Line Numbers
  1. i= 42;
  2. i+= i-= i*= i/= 1;
  3.  
Multiple changes to an lvalue before a sequence point is reached: *kaboom!*, no
matter whatever left to right or right to left associativity.

kind regards,

Jos
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#7: Jun 22 '07

re: C programming


Quote:

Originally Posted by JosAH

i= 42;
i+= i-= i*= i/= 1;

So this works just because the lvalue is different?

Expand|Select|Wrap|Line Numbers
  1. i+= b -= c*= d /= 1;
  2. i = b = c = d = 5;
  3.  
K&R says the order of evaluation of operands is unspecified.

So should I be coding:
Expand|Select|Wrap|Line Numbers
  1. i += (b -= (c *= (d /= )));
  2. i = (b = ( c = (d = 5)));
  3.  
I've never seen code like that.

I know that:
Expand|Select|Wrap|Line Numbers
  1. x = f() + g();
  2.  
is indeterminate if f() and g() alter a variable that they both depend on.

Help me out here.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#8: Jun 23 '07

re: C programming


Quote:

Originally Posted by weaknessforcats

So this works just because the lvalue is different?

Expand|Select|Wrap|Line Numbers
  1. i+= b -= c*= d /= 1;
  2. i = b = c = d = 5;
  3.  
K&R says the order of evaluation of operands is unspecified.

So should I be coding:
Expand|Select|Wrap|Line Numbers
  1. i += (b -= (c *= (d /= )));
  2. i = (b = ( c = (d = 5)));
  3.  
I've never seen code like that.

I know that:
Expand|Select|Wrap|Line Numbers
  1. x = f() + g();
  2.  
is indeterminate if f() and g() alter a variable that they both depend on.

Help me out here.

The definition for undefined behaviour (sic) is quite simple: when a mutable lvalue
(e.g. a variable) is changed more than once before a sequence point is reached
(a semicolon) the behaviour (result) is undefined.

Your example above is fine because your altering different mutable lvalues only once.
Parentheses don't help: they alter the precedence or associativity only, e.g.a-b-c is
different from a-(b-c). The order in which the operands a, b, and c are evaluated is
unspecified indeed.

The original example where the mutable lvalue 'i' is altered twice causes undefined
behaviour according to the spec.

I don't have the Standard text available on this laptop (I left the other one at the office),
but remind me if you want me to email the text (it's the latest draft of the C99 Standard).

kind regards,

Jos
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#9: Jun 23 '07

re: C programming


Quote:

Originally Posted by JosAH

The definition for undefined behaviour (sic) is quite simple: when a mutable lvalue
(e.g. a variable) is changed more than once before a sequence point is reached
(a semicolon) the behaviour (result) is undefined.

I will try to keep this inthe forefornt of my mind.

Thank you for bearing with me.

BTW I would like to see the standard text. I assume in this area C99 and C++ 1998 are the same. Yes?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#10: Jun 23 '07

re: C programming


Quote:

Originally Posted by weaknessforcats

I will try to keep this inthe forefornt of my mind.

Thank you for bearing with me.

BTW I would like to see the standard text. I assume in this area C99 and C++ 1998 are the same. Yes?

On our way to my brother and his wife we stopped by at the office; this is the
relevant paragraph from annex J.2. I wasn't a member of WG-J16 so I don't know
whether or not C++ simply duplicated this annex; I think not because I can
imagine certain situations in C++, non-existent in C, that also can cause undefined
behaviour.

Here's the paragraph:

Expand|Select|Wrap|Line Numbers
  1.        J.2  Undefined behavior
  2.  
  3.          -- Between two sequence points, an object is modified more
  4.             than once, or  is  modified  and  the  prior  value  is
  5.             accessed other than to determine the value to be stored
  6.             (6.5).
  7.  
And this is what paragraph 6.5 talks about:

Expand|Select|Wrap|Line Numbers
  1.        6.5  Expressions
  2.  
  3.        [#1]  An  expression is a sequence of operators and operands
  4.        that specifies computation of a value, or that designates an
  5.        object  or  a  function,  or that generates side effects, or
  6.        that performs a combination thereof.
  7.  
  8.        [#2] Between the previous and next sequence point an  object
  9.        shall  have  its  stored  value modified at most once by the
  10.        evaluation of an expression.  Furthermore, the  prior  value
  11.        shall  be  accessed  only  to  determine  the  value  to  be
  12.        stored.60)                                                   *
  13.  
  14.        [#3]  The grouping of operators and operands is indicated by
  15.        the syntax.61)  Except as specified later (for the function-
  16.        call  (),  &&,  ||,  ?:,  and comma operators), the order of
  17.        evaluation of subexpressions and the  order  in  which  side
  18.        effects take place are both unspecified.
  19.  

kind regards,

Jos
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,375
#11: Jun 24 '07

re: C programming


Thank you so much.

I am sorry to be so difficult about this, but I have had to adjust a core belief. From the section of the standard, I see completely what you have been talkling about.

I have been using C++ for 17 years and still I am plugging holes in my knowledge. This is one less hole.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#12: Jun 24 '07

re: C programming


Quote:

Originally Posted by weaknessforcats

Thank you so much.

I am sorry to be so difficult about this, but I have had to adjust a core belief. From the section of the standard, I see completely what you have been talkling about.

I have been using C++ for 17 years and still I am plugging holes in my knowledge. This is one less hole.

No need to apologize; that Standard is lawyers' stuff; and it's needed now and
then. The days when K&R sloppy definitions ruled are over, C++ started off way
better than C did but still: those Standards are needed, That draft text has no
copyright at all; maybe I can post it in the C/C++ Articles section (it's quite large).
I have to check it out.

kind regards,

Jos
Reply