You are on page 1of 13

UNIT-II PROGRAMMING WITH C

OPERATORS IN C

It supports vast no. of operators. Operator is symbol that instructs to perform some
action to the C compiler .Types:

o Arithmetic Operators
o Relational Operators
o Logical Operators
o Assignment Operators
o Unary Operators
o Conditional Operator / Ternary Operator
o Bitwise Operators

Arithmetic Operators: These are used to perform basic arithmetic operators like
addition, subtraction, multiplication etc

Operator Description
+ for Addition
- for subtraction
* for multiplication
/ for division
% modulus operator
Example: int a,b;
a=12;
b=13; c=a+b;
Relational Operators : Relational Operators used to compare two values. They return
either TRUE or FALSE

Operator Description
== Equal
!= Not equal
> Greater than
>= Greater than or Equal
< Less than
<= Less than or Equal
EX:10>12 returns FALSE

12>5 returns TRUE

ASSIGNMENT OPERATORS:

It is used to assign value to variable.

Value will assign from right side to left side.

“ = ” is used as assignment
1|Page
UNIT-II PROGRAMMING WITH C
Example: var a=12;

UNARY OPERATORS:

1.Increment operator ( ++ ) will increase value by 1.

2.decrement operator (--) will decrease value by 1.

CONDITIONAL OPERATOR:

Conditional operator ? and : is also known as Ternary Operator.

Syntax: Expression1? Expression2: Expression3

If Expression1 is true the Expression2 will execute otherwise Expression3 will execute.

Ex: int a=5,b=10;

a>b:printf(“ a is greater”)?printf(“b is greater”);

LOGICAL OPERTORS:

Logical operators are used to join multiple conditional expressions and returns either
TRUE or FALSE

1. AND(&&)- returns TRUE if both conditions are TRUE. Otherwise FALSE.


2. OR(||)-returns FALSE if both conditions are FALSE. Otherwise TRUE.
3. NOT(!)- returns TRUE if expression is FALSE
returns FALSE if expression is TRUE

BITWISE OPERATORS:

These Operators work on bit by bit.

>> --- shifts bit value in right direction

<< ---- shifts bit value in left direction

& ---- Bitwise AND

| ---- Bitwise OR

^ ---- Bitwise XOR

SPECIAL OPERATORS:

Comma ( , ) Operator is used separate the expressions and variables.

Ex: int a,b,c;

2|Page
UNIT-II PROGRAMMING WITH C
sizeof( ) returns the size of variables.

Ex: int a=10,b;

b= sizeof(a);

printf(“size of a is %d”,b);

output: size of a is 4

EXPRESSIONS

Expression is a combination of constants, variable and Operators which produce single


value after evaluation.

EX: int a=10,b=20,c;

c=a+b; // this is an expression.

Evalution of Expression:

Arithmetic expression evaluates from two left to right passes.

Ex: x=6-12/3+3*2-1;

This evaluated in two left to right passes as,

First pass

Step 1: 6-4+3*2-1

Step 2: 6-4+6-1

Pass 2

Step 1: 2+6-1

Step 2: 8-1

Step 3: 9

3|Page
UNIT-II PROGRAMMING WITH C
CONTROL STATEMENTS

Statements which are used to control the flow of execution of the statements based on
certain conditions are called control statements.

Types of control statements:

1. Conditional / branching statements


2. Looping / iterative statements

Conditional Statements:

These are basically used to a test condition. The result determines which block of
statements to be executed.

The following are conditional statements supported by javascript.

o if statement
o if… else statement
o if… else if…. If statement
o switch statement

if Statement

Use if statement to execute some code only if specified condition is TRUE.

Syntax:

if(condition)

code to be executed if condition is TRUE

Ex:
#include<stdio.h>
void main( )
{
int a=10,b=5;
if(a>b)

4|Page
UNIT-II PROGRAMMING WITH C
{
printf(“a is greater”);
}
}
Output: a is greater

If… else Statement

If condition is true then if block will be executed and

if the condition if false then else block will be executed.

Syntax:

If(condition)

code to be executed if condition is TRUE;

Else

code to be executed if condition is False;

Ex:
#include<stdio.h>
void main()
{
int a=10,b=12;
if(a>b)
{
printf(“a is greater than b”);
}

5|Page
UNIT-II PROGRAMMING WITH C
else
{
printf(“b is greater than a”);
}
}
O/P: b is greater than a

If…else if ….else
Use this if one of several blocks of code to executed.

Syntax:

If(condition1)

Statements to executed if condition1 is TRUE

else if(condition2)

Statements to executed if condition2 is TRUE

else

Statements to executed if condition1and condition2 are FALSE

Ex:
#include<stdio.h>
void main()
{
6|Page
UNIT-II PROGRAMMING WITH C
int a=10,b=12,c=11;
if(a>b&&a>c)
{
printf(“a is greatest”);
}
else if(b>c)
{
printf(“b is greatest”);
}
else
{
printf(“c is greatest”);
}
}
O/P: b is greatest

SWITCH

It is mainly used to select one option among multiple options.

Syntax:

switch(n)

case 1: statements;

break;

case 2: Statements;

break;

default: statements;

7|Page
UNIT-II PROGRAMMING WITH C
Ex:

#include<stdio.h>
void main()
{
var a=10,b=12,n;

printf("enter1 for +or 2 for – or 3for / or 4 for * ");

scanf(“%d”,&n);

switch(n)

case 1:printf("addition=%d "+(a+b));

break;

case 2:printf("addition= %d"+(a-b));

break;

case 3:printf("addition=%d "+(a*b));

break;

case 4:printf("addition= %d "+(a/b));

break;

default:printf("enter correct choice");

Output:

enter1 for + or 2 for – or 3for / or 4 for *

Addition is 22

8|Page
UNIT-II PROGRAMMING WITH C

LOOPING STATEMENTS

It allows programmer to executes one or more lines of code repetitively

Types:

1. while loop
2. do while loop
3. for loop
4. for… in loop

While loop:

o It is an entry controlled loop


o It executes block of statements until condition becomes false.
o It contain 3 statements 1.initial value
2. condition
3. increment / decrement

Syntax:
Initial value;
While(condition)
{
Statement 1;
Statement 2;
………….
Statement N;
Increment / decrement;
}

Ex:
#include<stdio.h>
void main()
{
int i=1;
while(i<=5)
{
printf(“%d\n”);
}
}
9|Page
UNIT-II PROGRAMMING WITH C

Output: 1 2 3 4 5
Do…while loop:

o It is an exit controlled loop


o The condition is checked at the end of loop.
o It executes block of statements until condition becomes false.
o The loop will execute at least once even if condition is false at initial stage
o There should be semicolon ( ; ) at the end of condition.

Syntax:

initial value;
do
{
Statement 1;
Statement 2;
………….
Statement N;
Increment / decrement;
}while(condition);

Ex:
#include<stdio.h>
void main()
{
int i=1;
do
{
printf(“%d\n”);
}while(i<=5);
}

Output: 1 2 3 4 5

For loop

o It is an entry controlled loop


o It executes block of statements until condition becomes false.
o It contain 3 statements 1.initial value
2. condition
3. increment / decrement
10 | P a g e
UNIT-II PROGRAMMING WITH C
Syntax:

for(initialization; condition; increment/decrement)


{
Statement 1;
Statement 2;
…………..
Statement N;
}
Ex:
#include<stdio.h>
void main( )
{
int i;
for(i=1;i<=5;i++)
{
printf(“hello\n”);
}
}
Output:
hello
hello
hello
hello
hello

C Type Conversion – Implicit & Explicit Type Conversion in C

When variables and constants of different types are combined in an expression then
they are converted to same data type. The process of converting one predefined type
into another is called type conversion.

Type conversion in c can be classified into the following two types:

Implicit Type Conversion

When the type conversion is performed automatically by the compiler without


programmers intervention, such type of conversion is known as implicit type
conversion or type promotion.

The compiler converts all operands into the data type of the largest operand.

The sequence of rules that are applied while evaluating expressions are given below:

All short and char are automatically converted to int, then,


11 | P a g e
UNIT-II PROGRAMMING WITH C
1. If either of the operand is of type long double, then others will be converted to long
double and result will be long double.
2. Else, if either of the operand is double, then others are converted to double.
3. Else, if either of the operand is float, then others are converted to float.
4. Else, if either of the operand is unsigned long int, then others will be converted to
unsigned long int.
5. Else, if one of the operand is long int, and the other is unsigned int, then

1. if a long int can represent all values of an unsigned int, the unsigned int is converted
to long int.

2. otherwise, both operands are converted to unsigned long int.

6. Else, if either operand is long int then other will be converted to long int.
7. Else, if either operand is unsigned int then others will be converted to unsigned int.

It should be noted that the final result of expression is converted to type of variable on
left side of assignment operator before assigning value to it.

Also, conversion of float to int causes truncation of fractional part, conversion of


double to float causes rounding of digits and the conversion of long int to int causes
dropping of excess higher order bits.

Explicit Type Conversion

The type conversion performed by the programmer by posing the data type of the
expression of specific type is known as explicit type conversion.

The explicit type conversion is also known as type casting.

Type casting in c is done in the following form:

(data_type)expression;

where, data_type is any valid c data type, and expression may be constant, variable or
expression.

For example,

x=(int)a+b*d;

The following rules have to be followed while converting the expression from one type
to another to avoid the loss of information:

1. All integer types to be converted to float.


12 | P a g e
UNIT-II PROGRAMMING WITH C
2. All float types to be converted to double.
3. All character types to be converted to integer.

13 | P a g e

You might also like