Operators-Expressions and Escape Sequences in C -Chapter 3

jojo June 26, 2012 8 Comments

We are into third chapter of C programming in which we will discuss operators, operands, expressions and escape sequences. I assume that you have already gone through Chapter 1 – Data Types in C and Chapter 2 – Variables and Keywords in C.

Operator – An operator is usually a symbolic representation of a particular operation. Ex:- means addition * means multiplication etc.

Operand – An operand is usually the data upon which the operator performs an action. Ex:- c=a+b;  Here c, a, b are operands; where as + and = are 2 different operators.Operands can be constants or variables. Ex:- c=a+10;

Expression – An expression is a complete instruction which may be a combination of operator(s) and operand(s). Ex:- c=a+b; is a complete expression. Other examples of complete expressions are a++;       p=p+3;       ++a;

There is nothing much to tell in detail about operands and expression. Lets dive more deep into the world of operators in C.

Operators

There are basically 7 types of operators in C namely:-

  • Arithmetic operators
  • Unary operators
  • Assignment operators
  • Relational and equality operators
  • Logical operators
  • Bitwise operators

Note:– If an operator requires 2 operands to act upon – it is called a binary operator. If an operator requires only one operand to act upon – its then called a unary operator.

Arithmetic Operators

There are 5 arithmetic operators in C as shown in the table below. Here are some points to note while you deal with arithmetic operators. 

  • Both operands should be numeric values (integer quantities,floating point or character quantities (ASCII values)).
  • There is no operator for calculating exponential values.
  • In the case of division operator – if both operands is of integer type, then result will be an integer.Any fractional part that may occur in the case of an integer division will be truncated towards zero.(Ex:- 10/3 yields the answer as 3 not as 3.33). If any one of the operand is a floating point type, then result of the division will be floating point.

Operator

Function

Comments

+

Addition Performs addition on 2 operands.

Subtraction Performs subtraction on 2 operands: Ex:- c=a-b; here b is subtracted from a and result is stored in c

*

Multiplication Performs multiplication on 2 operands.

/

Division Performs integer division. Denominator must be non-zero.

%

Remainder (Mode operator) Outputs the remainder of an integer division. Both operands must be integers. If there is any fractional part in the input value, it will get truncated. Ex: 10.14/3.75 – our answer will be 1 (bcz it gets the remainder of 10/3)

Example program:-

int x=20,y=7,a,s,m,d,r;

float xp=20.75,yp=7.25,ap,sp,mp,dp,rp;

a=x+y; s=x-y;   

m=x*y; d=x/y;

r=x%y;

ap=xp+yp; sp=xp-yp;

mp=xp*yp; dp= xp/yp;

rp= xp%yp;

Output

a=27         s= 13            m= 140                  d= 2                     r= 6

ap=28       sp=13.5       mp=150.43         dp= 2.862        rp=6

Note:- In both cases of float and int – the result produced by mode operator is same (r = rp =6).

Note:- c=a+b; in this case the contents to the right side of ‘=’ is evaluated first and then assigned to variable in the left side.

Unary Operators

They require only a single operand to act upon. They are:-

  • Minus operator
  • Increment and Decrement operator
  • Sizeof operator

Minus operator – is also known as negation operator. This operator always precedes a variable, a constant or an expression. It assigns the negative sign to the variable/constant. Ex:- int i= -8; now variable i is assigned with negative value 8. Another Ex:- int i=-10, j;   j= -i;  now value in j= – (-10) which is equal to +10. 

Increment and Decrement Operator

From the name itself you can guess the use of these 2 operators – they are used for incrementing and decrementing variables. Increment operator is ++  which adds 1 to its operand and decrement operator is — which subtracts 1 from its operand. Both operators can be either post-fixed or pre-fixed. There are differences in the working of postfixed and prefixed operators.

Example:- a++; b–; ++b; –a; etc.

Difference between postfixing and prefixing

int a=5, x,y;

x=a++;   /* post fixing */

y=++a;    /* pre fixing  */

Outputs:-

x = 5 and then a will be incremented by 1 (a=6); Here a is assigned to x first and then only a is incremented

y=6 and a =6  Here a is incremented first and then after incrementing (a), it is assigned y.

Sizeof Operator

This operator returns the size of an operand in bytes.

int a,b;

b=sizeof(a);

Output

b = 2 (since a is an integer variable and integer occupies 2 byes)

Assignment Operator

There are 2 types of assignment operators. The one that we use normally and often is = operator.

Ex:- c=a+b;  Here expression towards the right side of = operator is evaluated first. The result of expression is then assigned to the variable on left side. Here = is called the assignment operator.

Another type of assignment operator is += which is used when the variable on left side and the variable that comes immediately after operator are same.

Example:- a = a+2;     it can also be expressed as    a+=2;

int a=5;

a+=2;

Output:-

a =7 ; here the value inside a is added with 2 and then the new result is assigned back to a itself.

Note: You can use a variable instead of a constant. Ex: a=a+b; can be written as a+=b;

This kind of assignment operation is valid for other operators like –  * and  /.

Examples:- a/=5  (a=a/5);     a -=5   (a=a-5);        a*=5    (a=a*5);

Relational Operators

There are 4 relational operators as shown below.

Operator

Function

Comments

Greater than Compares two values. Ex: a>b – value inside a is compared with value inside b and if a is greater condition is true otherwise condition is false. If condition is true then the expression returns 1 else it returns 0.
>= Greater than or equal to Ex:- a>=b
Less than Ex:- a
 <= strong=””> Less than or equal to Ex:- a<=b td=””>

Equality Operators

Operator

Function

Comments

== Equal to Ex:- a==b – checks if a is equal to b. If they are equal, then expression is considered TRUE, else FALSE.
!= Not equal to Ex:- a!=b – checks if a and b are unequal. If they are unequal, then expression is considered TRUE, else FALSE.

Logical Operators

Operator

Function

Comments

&& Logical AND Ex:- (a>9)&&(b<5) –5=”” –=””>
|| Logical OR Ex:- ( a>9)||(b<5) –5=”” –=””>

Notes:-

Logical AND (&&) – This expression is evaluated from left to right and is considered TRUE only if both conditions (on left and right ) are TRUE. If either of the two condition is a FALSE, then the whole expression will be false.

Logical OR (||) – This expression is evaluated from left to right and is considered TRUE –if any one of the two conditions (on left and right) is TRUE. If both of the two conditions are FALSE, then only the whole expression will turn FALSE.

Bitwise Operators

Operator Function Comments

~

Negation Complements the operand. 1’s are converted to 0’s and 0’s are converted to 1.

&

AND Output is exactly according truth table for AND operation.

|

OR Output is exactly according truth table for OR operation.

^

X-OR (Exclusive OR) Output is exactly according truth table for X-OR operation.

>> 

Shift right Shifts the bits towards right (a fixed no of place as given in the expression)

<< 

Shift left Shifts the bits towards left (a fixed no of place as given in the expression)

Escape Sequences

Escape Sequence Function Comments
\n New line At the output display, this sequence takes cursor to the next line.
\b Backspace Moves cursor from its current to position to left side (one position)
\t Tab space Moves the cursor from current position to one tab space towards right.
\a Alert/Alarm Produces a sound in the speaker connected to computer.
\” Double Quotes Prints a double quote on the output display.
\? Question mark Prints a question mark on the display.
\’ Single quote Prints a single quote
\\ Backslash Prints a back slash
\0 Null  
\f Form feed  
\v Vertical Tab  
\r Carriage return  

The backward slash ‘\’ is called escape character. The character that follows backslash gives meaning/purpose to an escape sequence. An escape sequence is actually considered as a single character(though it may seem like a combination of two characters).

We have covered basics about operators in C now. More specific examples and explanations will be given in coming chapters.

In the image given below, almost all operators in C are listed with its order and priority. I will explain about order and priority of operators in another article.

Image Courtesy

Comments
  • ILAMATHI.K
    September 13, 2012

    This article is very nice and its simple and fruitful. It would be still more useful if you send the concepts of c and c++ in downloadable format. This is my suggestion.

  • Aditya
    July 17, 2012

    what should be C compiler that can be used for Amega8 programing

  • bhuvanesh
    July 11, 2012

    can i learn “Embedded C” from this site?? Is there any article avaiable????? Reply Plzzzz

    • jojo
      July 11, 2012

      We will come up with “Embedded C” tutorial in future.

  • rowan walters
    June 27, 2012

    thanks circuit i cant make technical comments im just glad for the info

  • Elex Wood
    June 27, 2012

    Thanks Sir,
    I have read you previous posts about c programming. and Truly It Helped me a lot.

    Please Write more chapter as soon as possible.

    • jojo
      June 27, 2012

      @Elex Wood and @ Rowan Walters – Thanks a lot for the nice words.

Leave a Reply

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