Operators

December 7th, 2009 by Kevin | Posted under C Programming.

Arithmetic Operators

In C programming, we have the following arithmetic operators:

+                                             Addition

-                                              Subtraction

*                                             Multiplication

/                                             Division

%                                            Modulo

So,

int a = 10;

int b = 5;

int c = 0;

c = a + b; /* c=15 */

c = a – b; /* c=5 */

c = a * b; /*c=50 */

c = a / b; /* c=2 */

c = a % b; /* c=0 */ /* by mod we get the remained of a divided by b */

Each of the above statements are called expressions in C. Every expression is supposed to be terminated by a ; else the compiler will complain. Expressions consist of operands and operators and they produce a result.

Ex: c = a + b /* Here ‘a’ and ‘b’ are operands, ‘+’ is the operator and the result is placed in ‘c’ */

Relational Operators

Following are the relational operators in C programming.

<                                                        Less than

>                                                        Greater than

<=                                                     Less than or equal to

>=                                                    Greater than or equal to

==                                                     Equal to

!=                                                       Not equal to

Hence,

int a = 10;

int b = 5;

int c = 0;

int d = 5;


c = (a < b) /* c=0 */

c = (a > b) /* c=1 */

c = (a <= d) /*c=0*/

c= (b >= d) /*c=1*/

c = (b == d) /*c=1*/

c = (b != d) /*c=0*/

So as it can be seen from the examples above, the result of relational operators will be either 1 or 0 (also know as True or False). Relational operators and Logical Operators (given below) are very useful when we want to check conditions in a program.

Ex: if (water_level >= danger_level) printf(“ALERT”);

Note that programmers often get bugs due to the improper use of ‘=’ instead of ‘==’.

int a = 3;

int b = 9;

if (a = b)   /* MISTAKE: Here a is assigned value of b. INSTEAD WE SHOULD USE (a == b).

printf(“TRUE”);

Logical Operators

The operators && (AND) and || (OR) are known as logical operators. They are binary operators as well i.e they need two operands to function. Operands are evaluated from left to right. && evaluation stops at the first FALSE operand. || evaluation stops at the first TRUE operand.

Note: We can have expressions in place of operands.

if (a < b) && (c < d) || (p < q)

printf(“OK);

&& has higher precedence than ||. If (a<b) is FALSE then we get no print, the statement doesn’t go forward and check for the (c<d) || (p <q)

Increment and Decrement Operators

We have the operators ‘++’ and ‘–’ known as increment and decrement operators.

Ex:

n = n + 1 can be replaced by n++

n = n – 1 can be replaced by n–

Also the increment and decrement operators can be used as prefix or postfix to the variable. Means we can either use n++ or ++n. Both result in the increment of n. The difference comes to light below:

c = n++; /* if n = 5, then after this execution, n = 6 and c = 5 as first n is assigned to c and then incremented*/

c = ++n; /* if n =5, then after this execution, n = 6 and c = 6 as n is first incremented and then assigned to c*/

Increment and decrement operators are useful because compilers can produce optimized code for them.

This seems like a long post already :) In the next post, will mention something about bitwise operators.

 

Related Posts:

Tags: , ,

Comments

2 Responses to “Operators”
  1. Lakshmikandh says:

    thanks for adding a link to my blog in yours…!

    [Reply]

  2. kevin says:

    Your Welcome. You have a really good C programming blog.

    [Reply]

Do you have any comments on Operators ?

Spam protection by WP Captcha-Free