Reading:  

Getting started with C Language


Operators

Operator

An operator is a symbol in a program which instruct the computer to do many mathematical and logical manipulations. They are used in C language program to operate on data and variables. In C there are a rich set of operators.

Types of operators

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Assignment operators
  • Increment and Decrement operators
  • Conditional operators
  • Bitwise operators
  • Special operators

Arithmetic operators

These are the operators which are used for arithmetic  and unary +or -purposes. The operators are +,-,*,/,%. In c language the results from the expressions on right side is stored in to the variable in left hand side.  

 

operator

              meaning

     +

Addition or unary plus

      -

Subtraction or unary minus

     *

Multiplication

     /

Division

    %

Modulo

Consider A=5 and B=2 then the results of the operations stored in C will be

Operations

          Results

C=A+B

    C= 7

C=A-B

    C =3

C=A*B

    C=10

C=A/B

    C=2

C=A%B

    C=1

C=-A

    C=-5

C=+A

    C=5

 

Relational operator

Relational operator is used to compare two quantifiers depending up on their relation. Relational operators are used for decision making. Any comparisons in program is done with these relational operators. An expression containing a relational operator is called relational expression.

Operator

Meaning

    <

Is less than

    <=

Is less than or equal to

    >

Is greater than

    >=

Is greater than or equal to

    ==

Is equal to

    !=

Is not equal to

 

A relational operator returns a true or false value. The syntax of relational operations is

Arithmetic expression1 relational operator  Arithmetic expression2

The arithmetic expression may be constants variables or combination of them. When using  arithmetic operations in relational operations first evaluate the arithmetic expressions and the results are compared. Relational operators are used in decision making statements like if, while, etc

When A=10 and B=5 then

Relational operator

   Result

           A<B

   False

           A<=B

   False

           A>B

   True

           A>=B

   True

           A==B

   False

           A!=B

   True

 

We can also write relational expressions like

A+B>C+D, a==0, c!=100,….

 

Logical operators

There are 3 logical operators in C language . AND , OR and NOT. Logical operators are used in decision making which we want to test more than one condition. A logical expression valuates and returns the values 0 and 1. We use logical statements with decision making statements.

 

Operator

Representation

Meaning

Logical AND

         &&

Returns true if all conditions are true

Logical OR

           ||

Returns true if any one of the conditions are true

Logical NOT

            !

Returns the opposite value

 

The following table illustrate how the logical operator works whwn the value of A=1, B=5

 Expression

  Result

A==0&&B!=6

False

A==0||B!=6

True

! A

0

 

Assignment operator

Assignment operator is used to assign the values or value of expressions on right hand side to the variable in left side.  The ‘=’ sign is used for the assignment.

V=expression;

Where expression may be  a variable , a value , or any expression or arithmetic operation.

Example :

C Assignment operator example

Thus the value in the right is assigned to the variable in left.

 Short hand assignment operator

A+=1; which is a shorthand statement for  A=A+1

A-=1; which is a shorthand statement for  A=A-1

If A=1,B=2  then

C=A+B;

 returns c=3                                   

Increment and Decrement operators

These are one of the useful operator in C language, also found in other languages.++ and  -- are used to denote increment and decrement operators.The operator ++ adds 1 to the operand while  - - subtracts 1

A++ is equalent to A=A+1

A-- is similar to A=A-1

Also we can write increment and decrement operators like

++A;

--A;

We mainly use these statements while using the loops.

 

Conditional operators

It’s a ternary operator used for decision making using expressions.

Syntax :

expression1?expression2:expression3;

which means evaluates expression1 and if it is true then perform the expression2 and skip the expression3. And if the expression1 is false then skip the expression2 and performs expression3.

example:

consider if A=5,B=10 then

A>B?C=A:C=B;

here evaluates A>B which is checks 5>10 which is false, thus performs the third expression C=B that is assigning the value of B into  C and skips the expression C=A, if the condition were true then the value of  A would be assigned to C. Thus C returns the greatest value.

We can replace the conditional operator with an ‘if’ statement .

Bitwise operator

Bitwise operators are used for manipulating data at its bit level these are used for testing the bits or shifting them in to left or right. It can’t used for float and double data types.

Operator

Meaning

&

Bitwise AND

|

Bitwise OR

^

Bitwise exclusive OR

<< 

Shift left

>> 

Shift right

~

One’s compliment

 

             

Special operators

C supports some special operators such as COMMA operator, SIZEOF operator, POINTER operators(like ‘&’and’*’) and member selection operators (‘.’and ‘->’).

Comma operator:

Comma operator is used to separate expressions while the expressions are evaluated from left to right.

Example :

Value=(x=10,y=5,x+y) here first assigns the value 10 in to x then 5 to y and then the result of x+y (10+5) in to the Variable value.

Sizeof operator:

Sizeof operator is a compile time operator used with an operand, which returns the number of bytes the operand occupies.

Syntax:

sizeof(expression);

the expression can be a variable or a data type.

Example:

A=sizeof(B);

A=sizeof(double);

Pointer operators

We use pointer operators ‘&’and ’*’ along with pointers. By using ‘&’ we gets the address or location of the variable.

P=&Q then the value of P will be the address of the variable Q.

The '*' operator is used to initialize the pointer data types.

Datatype  *pointer-name;

Example :

int *P; which means P is a pointer variable that points to a integer data type.

Member function operator

The member function operators like ‘ . ‘and ’ ->’ are used to access the data members of structure union and linked lists.

Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!