Linux C Programming Tutorial Part 12

In this ongoing C programming tutorial series, we have already discussed some of the basic stuff like arithmetic, logical, and relational operators as well as conditional loops like ‘if’ and ‘while’. Adding upon that, this tutorial will focus on assignment operators (other than =) and conditional expressions.

Let’s start with assignment operators. If you have created basic C programs until now (which I am sure you would have especially after following our tutorial series), there are high chances you’d have done something similar to the following:

a = a + 1;

Right?

The new thing we’re going to learn today is that you can rewrite this expression as the following:

a += 1;

Yes, the  += is an operator and is also called an assignment operator. 

Now, if I say using assignment operators like these makes the code more compact and readable, some of you may argue that in context of the example we used, an increment operator would have been equally good as that also makes sure the value of ‘a’ gets increased by 1.

a++;

I agree, but think of other scenarios like the following:

b = b + 10;
b = b + c

In these cases, using the assignment operator definitely makes the code writing, reading, and reviewing one step easy. 

Still not convinced? Perhaps, the following generic example will give you an even better idea on the value of assignment operators. Take a look:

arr1[arr2[integer1 + integer2] + arr3[integer3 + integer4] + arr4 [integer5 + integer6]] = arr1[arr2[integer1 + integer2] + arr3[integer3 + integer4] + arr4 [integer5 + integer6]] + 5 

This line of code takes time to be understood specifically because you have to make sure the two arrays (on each side of the = operator) refer to the same value or not. In this case, they do, so a better way would be to use an assignment operator in the following way:

arr1[arr2[integer1 + integer2] + arr3[integer3 + integer4] + arr4 [integer5 + integer6]] += 5;

So, using an assignment operator in this case not only made the line very easy to understand but also made it more compiler friendly in the sense that it may help compiler produce efficient code.

Now that you are convinced (I really hope you do) about the fact that assignment operators are useful, here’s a list of operators that have a corresponding binary operator:

+ - * / % << >> & ^ |

The first five operators in the list we’ve already discussed in our tutorials so far. The last five are bitwise operators, and we’ll be discussing them in one of our upcoming tutorials. Meanwhile, here’s a quick reference to assignment operators corresponding to some of these operators:

a += b;
a -= b;
a *= b;
a /= b;
a %= b;

So in general, you can keep in mind that the following:

expression1 = (expression1) op (expression2)

can be expressed as:

expression1 op= expression2

where ‘op’ is the operator in use, like +, -, *, and more.

Please note that the following expression as well as expressions similar to it:

a *= b - c 

will expand as:

a = a * (b-c)

I hope I was able to make the concept of assignment operators clear at least on the basic level. Moving on, now let’s quickly discuss conditional expressions.

Take the following example:

if(a==b)
c = c + 1;
else
c = c - 1;

Conditional expressions help you write logics like these in a single line. Here’s how:

c = (a==b) ? (c+1) : (c -1)

So here, first the condition (a==b) is checked. If it’s true, then (c+1) is assigned to c, else (c-1) is assigned to c. Here’s a sample code to make things more clear:

int main()
{
int a = 6, b = 5, c = 9;

c = (a==b) ? (c+1) : (c -1) ;

printf("n %d n", c);

return 0;
}

Since a is not equal to b here, so c is assigned (c-1), which means the new value of c becomes 8. Just in case you need, here’s the generic form of conditional expressions:

expression1 ? expression2 : expression3

I hope you got a basic idea about assignment operators and conditional expressions. You are now encouraged to use these in your day to day coding activities, and if you run into any kind of issue, don’t forget to leave us a comment here. 

BROUGHT TO YOU BY : FREELANCE WEB DESIGNER KUALA LUMPUR

Leave a Reply

Your email address will not be published. Required fields are marked *

*