Class 2nd

 An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators

·         Arithmetic Operators

·         Relational Operators

·         Logical Operators

·         Assignment Operators

 

 

 

 

 

 

 

 

 

 

Arithmetic Operators

The following table shows all the arithmetic operators supported by the C language. Assume variable A holds 10 and variable B holds 20 then

 

 

Operator

Description

Example

+

Adds two operands.

A + B = 30

Subtracts second operand from the first.

A B = -10

*

Multiplies both operands.

A * B = 200

/

Divides numerator by de-numerator.

B / A = 2

%

Modulus Operator and remainder of after an integer division.

B % A = 0

++

Increment operator increases the integer value by one.

A++ = 11

--

Decrement operator decreases the integer value by one.

A-- = 9

 

 


 

Relational Operators

The following table shows all the relational operators supported by C. Assume variable A holds 10 and variable B holds 20 then

 

Operator

Description

Example

==

Checks if the values of two operands are equal or not. If yes, then the condition becomes true.

(A == B) is not true.

!=

Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.

(A != B) is true.

> 

Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true.

(A > B) is not true.

< 

Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true.

(A < B) is true.

>=

Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true.

(A >= B) is not true.

<=

Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true.

(A <= B) is true.

 


 

Logical Operators

Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then

Show Examples

Operator

Description

Example

&&

Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.

(A && B) is false.

||

Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true.

(A || B) is true.

!

Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.

!(A && B) is true.

 


 

Assignment Operators

The following table lists the assignment operators supported by the C language

Show Examples

Operator

Description

Example

=

Simple assignment operator. Assigns values from right side operands to left side operand

C = A + B will assign the value of A + B to C

+=

Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.

C += A is equivalent to C = C + A

-=

Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.

C -= A is equivalent to C = C - A

*=

Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.

C *= A is equivalent to C = C * A

/=

Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.

C /= A is equivalent to C = C / A

%=

Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.

C %= A is equivalent to C = C % A

 


 

Operators Precedence in C

Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Show Examples

Category

Operator

Associativity

Postfix

() [] -> . ++ - -

Left to right

Unary

+ - ! ~ ++ - - (type)* & sizeof

Right to left

Multiplicative

* / %

Left to right

Additive

+ -

Left to right

Shift

<< >>

Left to right

Relational

< <= > >=

Left to right

Equality

== !=

Left to right

Bitwise AND

&

Left to right

Bitwise XOR

^

Left to right

Bitwise OR

|

Left to right

Logical AND

&&

Left to right

Logical OR

||

Left to right

Conditional

?:

Right to left

Assignment

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

Right to left

Comma

,

Left to right

C Expressions

An expression is a formula in which operands are linked to each other by the use of operators to compute a value. An operand can be a function reference, a variable, an array element or a constant.

Let's see an example:

a-b;  

In the above expression, minus character (-) is an operator, and a, and b are the two operands.

There are four types of expressions exist in C:

  • Arithmetic expressions
  • Relational expressions
  • Logical expressions
  • Conditional expressions

 

Evaluation of expression

Description of each operation

6*2/( 2+1 * 2/3 +6) +8 * (8/4)

An expression is given.

6*2/(2+2/3 + 6) + 8 * (8/4)

2 is multiplied by 1, giving value 2.

6*2/(2+0+6) + 8 * (8/4)

2 is divided by 3, giving value 0.

6*2/ 8+ 8 * (8/4)

2 is added to 6, giving value 8.

6*2/8 + 8 * 2

8 is divided by 4, giving value 2.

12/8 +8 * 2

6 is multiplied by 2, giving value 12.

1 + 8 * 2

12 is divided by 8, giving value 1.

1 + 16

8 is multiplied by 2, giving value 16.

17

1 is added to 16, giving value 17.

 


 

Statements

 C programs are collection of Statements, statements is an executable part of the program it will do some action. In general all arithmetic actions and logical actions are falls under Statements Categories anyway there are few Statement categories

§  Expression Statements.

§  Compound Statements.

§  Selection Statements.

§  Iterative Statements.

§  Jump Statements.

Expression Statements:  

                  It is combination of variables, Constants, operators, Function Calls and followed by a semicolon. Expression can be any operation like Arithmetic operation or Logical Operation.

Few Examples for expression Statements 

X = Y + 10 ;

20 > 90;

a ? b : c ;

a = 10 + 20 * 30;

;   (This is NULL Statement ).

 

Compound Statement :

                Compound statement is combination of several expression statements. Compound Statement is Enclosed within the Braces { }.

Compound statement is also called as Block Statement.

Example for Compound Statement

{
        int a=10,b=20,c;
        c = a + b;
        printf(“value of C is : %d n”,c);



Selection Statements :

                Selection Statements are used in decisions making situations . Here is the few examples of Selection statements

§  if

§  if…else

§  switch

Iterative Statements :

              These are also Called as Loops. If we want to Execute a part of program many times we will use loops. We will going to explain each and Every loop in Detail in Later Tutorials. Here is the List of Basic loops in C language.

§  for loop.

§  while loop.

§  do-while loop.

Jump Statements : 

            These are Unconditional statements Jump statements are useful for Transfer the Control one part of program to other part of Program there are few Jump Statements in C

§  goto.

§  continue.

§  break.

§  return.

 

 

Concept of header file

A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

 

Preprocessor Directive

The C Preprocessor is not a part of the compiler, but is a seprate step in the compilation process. In simple terms, a C preprocessor is just a text substitution tool.

All preprocessor commands start with a hash symbol (#).

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.