By: Daniel Malcolm in C Tutorials on 2007年09月20日 [フレーム]
An expression such asi = i + 2in which the variable on the left side is repeated immediately on the right, can be written in the compressed form
i += 2The operator += is called an assignment operator.
Most binary operators (operators like + that have a left and right operand) have a corresponding assignment operator op=, where op is one of
+ - * / % << >> & ^ |If expr1 and expr2 are expressions, then
expr1 op= expr2is equivalent to
expr1 = (expr1) op (expr2)except that expr1 is computed only once. Notice the parentheses around expr2:
x *= y + 1means
x = x * (y + 1)rather than
x = x * y + 1As an example, the function bitcount counts the number of 1-bits in its integer argument.
/* bitcount: count 1 bits in x */
int bitcount(unsigned x)
{
int b;
for (b = 0; x != 0; x >>= 1)
if (x & 01)
b++;
return b;
}
Declaring the argument x to be an unsigned ensures that when
it is right-shifted, vacated bits will be filled with zeros, not sign bits,
regardless of the machine the program is run on.
Quite apart from conciseness, assignment operators have the advantage that they correspond better to the way people think. We say ``add 2 to i'' or ``increment i by 2'', not ``take i, add 2, then put the result back in i''. Thus the expression i += 2 is preferable to i = i+2. In addition, for a complicated expression like
yyval[yypv[p3+p4] + yypv[p1]] += 2the assignment operator makes the code easier to understand, since the reader doesn't have to check painstakingly that two long expressions are indeed the same, or to wonder why they're not. And an assignment operator may even help a compiler to produce efficient code.
We have already seen that the assignment statement has a value and can occur in expressions; the most common example is
while ((c = getchar()) != EOF) ...The other assignment operators (+=, -=, etc.) can also occur in expressions, although this is less frequent.
In all such expressions, the type of an assignment expression is the type of its left operand, and the value is the value after the assignment.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in C )
Passing double value to a function in C
Printing a simple histogram in C
Sum of the elements of an array in C
Simple arithmetic calculations in C
Passing pointer to a function in C
Find square and square root for a given number in C
Using memset(), memcpy(), and memmove() in C
Latest Articles (in C)
Simple arithmetic calculations in C
Find square and square root for a given number in C
Printing a simple histogram in C
Sum of the elements of an array in C
Passing pointer to a function in C
Passing double value to a function in C
Infix to Prefix And Postfix in C
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate