0.5 – C Language Fundamentals 2

by subbu on July 21, 2013


Operator is a symbol that tells the computer to perform some arithmetic, relational, or some other operations. Operators are used to manipulate the data. C language is rich in operators and supports different operators like

  1. Arithmetic operators (+ – * / %)
  2. Relational operators (<  >  <=  >=  ==  !=)
  3. Logical operator (&&  ||  !)
  4. Assigning operator (=)
  5. Ternary operators (?   : )
  6.  Increment/decrement operators (++  –)
  7. Assigning arithmetic operators (+=  -=  *=  /=  %=)
  8. Bit-wise operators (~  &  |  ^  <<  >>  <<=  >>=)
  9. Pointer operators (&   *   *.   ->)
  10.  Direct member access operator ( . )
  11. Special operators ( sizeof, comma (,) , # , ## etc.)

To start up with we will discourse the assigning and arithmetic operators.

Assigning operator (=):

It is an operator used to assign or store a value into the specified variable. While using assigning operator there must be a variable to the left side of assigning operator to receive the value.

 assigning operator

In the above example
Line1:  int x=10;  is a declaration statement, which allocates memory (integer type)  with the name x and assigns value 10 called initializing while declaration.
Line2:  int y;  is a declaration statement allocates memory (integer type) with the name y
Line5:  y=45; assigns 45 to the variable y
Line6:  12=18; is invalid because 12 is not a variable can’t be assigned with a value

Arithmetic operators:

These are the operators used to write arithmetic expressions. C supports the following arithmetic operators. + – * and / are known operators but %  behaves differently in C language

arithmetic operators

Modulo operator (%):

It looks like a percentage operator but works differently in C language. It is called modulo operator, returns (gives) remainder of an expression. For example 15%6 gives 3, because 3 will be remainder when we divide 15 by 6

Modulo operators can’t be applied to float and double type constants

modulo operator

It is so important to know that the sign of the result would be the sign of left operand to %. For example -6%-7 gives -6, -14%3 gives -2, -14%-3 gives -2 and 14%-3 gives 2 as results.

Any how the sign of the result for % is machine dependent for negative operands


modulo operator example

Arithmetic Expression:

Combination of variables, constants and arithmetic operators forms an arithmetic expression. Here constants and variables are called operands

arithmetic expression

Precedence  of  arithmetic operators:

Arithmetic expression doesn’t execute in a single step, executes in number of steps according the priority of arithmetic operators. Computer may be of any speed, executes a single operator at a time. When multiple operators are there with the same priority within the expression, then operators get priority according to their associativity. Associativity of arithmetic operators is left to right hence leftmost operator executes first.   Though “()” is not a part of arithmetic operators, it is used to give explicit propriety to a part of expression. Priority of arithmetic operators is as follows

   presidence-arithmetic operators


evaluation of arithmetic expression

Example explained:

Pass1:  First priority goes to the (), so (5+2) executes.
Pass2:  Though / * % have the same priority, / appears first, so 4/2 executes.
Pass3:  Though * % have the same priority, * appears first, so 8*4 executes.
Pass4:  % gets the higher priority, so 7%3 executes.
Pass5: Though + – have the same priority, + appears first, so 2+32 executes.
Pass6: – gets the higher priority, so 34-1 executes.
Pass7: = gets the least priority, so 33 is assigned to X

Previous post:

Next post: