473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C programming

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.
Jun 22 '07 #1
11 1399
askcq
63
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 ...
Jun 22 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
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
Jun 22 '07 #3
JosAH
11,448 Expert 8TB
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
Jun 22 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
Jun 22 '07 #5
JosAH
11,448 Expert 8TB
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
Jun 22 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Jun 22 '07 #7
JosAH
11,448 Expert 8TB
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
Jun 23 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
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?
Jun 23 '07 #9
JosAH
11,448 Expert 8TB
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
Jun 23 '07 #10
weaknessforcats
9,208 Expert Mod 8TB
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.
Jun 24 '07 #11
JosAH
11,448 Expert 8TB
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
Jun 24 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now...
3
by: user | last post by:
Hi all, At the outset, I regret having to post this slightly OT post here. However, I strongly feel that people in this group would be the best to advise me on my predicament. I am working as...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
30
by: Jakle | last post by:
I have been googling, but can seem to find out about C GUI libraries. My main platform is Windows, but it would be nice to find a cross platform library. I've been programming with php, which...
111
by: Enteng | last post by:
Hi I'm thinking about learning C as my first programming language. Would you recommend it? Also how do you suggest that I learn it?What books/tutorials should I read for someone like me? Thanks...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.