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.
11 1399
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 ...
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
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
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.
&& 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.
I don't think so in this case. I think this one has to go R-L.
Nope, sorry; compare it with this one: -
i= 42;
-
i+= i-= i*= i/= 1;
-
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
i= 42;
i+= i-= i*= i/= 1;
So this works just because the lvalue is different? -
i+= b -= c*= d /= 1;
-
i = b = c = d = 5;
-
K&R says the order of evaluation of operands is unspecified.
So should I be coding: -
i += (b -= (c *= (d /= )));
-
i = (b = ( c = (d = 5)));
-
I've never seen code like that.
I know that:
is indeterminate if f() and g() alter a variable that they both depend on.
Help me out here.
So this works just because the lvalue is different? -
i+= b -= c*= d /= 1;
-
i = b = c = d = 5;
-
K&R says the order of evaluation of operands is unspecified.
So should I be coding: -
i += (b -= (c *= (d /= )));
-
i = (b = ( c = (d = 5)));
-
I've never seen code like that.
I know that:
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
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?
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: -
J.2 Undefined behavior
-
-
-- Between two sequence points, an object is modified more
-
than once, or is modified and the prior value is
-
accessed other than to determine the value to be stored
-
(6.5).
-
And this is what paragraph 6.5 talks about : -
6.5 Expressions
-
-
[#1] An expression is a sequence of operators and operands
-
that specifies computation of a value, or that designates an
-
object or a function, or that generates side effects, or
-
that performs a combination thereof.
-
-
[#2] Between the previous and next sequence point an object
-
shall have its stored value modified at most once by the
-
evaluation of an expression. Furthermore, the prior value
-
shall be accessed only to determine the value to be
-
stored.60) *
-
-
[#3] The grouping of operators and operands is indicated by
-
the syntax.61) Except as specified later (for the function-
-
call (), &&, ||, ?:, and comma operators), the order of
-
evaluation of subexpressions and the order in which side
-
effects take place are both unspecified.
-
kind regards,
Jos
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |