CP Unit -03 Study Material engineering
CP Unit -03 Study Material engineering
CP Unit -03 Study Material engineering
Syllabus:
Contents
1. Operators
2. Types of Operators:
1. Arithmetic
2. Relational
3. Logical
4. Assignment
5. Increment-decrement
6. Conditional
7. Bitwise
8. Special
3. Arithmetic expression
4. Evaluation of Expression
7. Formatted Input
8. Formatted Output
1. OPERATORS:
➢ An operator is a symbol that tells the computer to perform certain mathematical or
logical manipulations.
➢ The operators are used in mathematical or logical expressions.
➢ The C Language supports various operators such as:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Conditional Operators or ternary operator
5. Bitwise Operators
6. Assignment Operators
7. Increment and Decrement Operators
8. Special Operators
2. TYPES OF OPERATOR:
➢ The arithmetic operators are used to perform various arithmetic operations like
addition, subtraction, multiplication and division, etc.
➢ These are binary operators as they take two operands and operand must be numeric
values.
➢ The result of operation will depend upon the type of operands used in instruction.
➢ E.g.
1. c = a + b;
2. z = x * y;
➢ The arithmetic operators are used to perform various arithmetic operations like
addition, subtraction, multiplication and division, etc.
➢ These are binary operators as they take two operands and operand must be numeric
values.
➢ The result of operation will depend upon the type of operands used in instruction.
➢ E.g.
1. c = a + b;
2. z = x * y;
#include<stdio.h>
void main()
scanf(“%d”, &days);
months = days/30;
}
2.2 Relational Operators:
➢ Relational operators are used to compare two numbers and taking decisions
based on their relation.
➢ Relational expressions are used in decision statements such as if, for, while, etc…
➢ Each of these operators compare their Left Side with Right Side.
1. We can use the relational operator with if statement to control the flow of execution.
➢ Logical operators are used to test more than one condition and make decisions.
➢ Logical OR ( || ):
➢ Logical NOT ( ! ):
➢ E.g. ! (x>=y).
if( ! (x>y) )
➢ This operator is used to check the condition and has two parts to be executed based
in evaluation of condition. Conditional operator is also known as ternary operator.
➢ Syntax:
- exp2 is evaluated and its value becomes the value of the expression
- exp3 is evaluated and its value becomes the value of the expression
#include<stdio.h>
void main()
int a, b, large;
a=10; b=20;
large = (a > b) ? a : b ;
Operator Meaning
+= a += 1 is same as a = a + 1
-= a -= 1 is same as a = a - 1
*= a *= 1 is same as a = a * 1
/= a /= 1 is same as a = a / 1
%= a %= 1 is same as a = a % 1
Simple Assignment Shorthand Assignment
a=a+1 a += 1
a=a–1 a -= 1
a = a * (n+1) a *= (n+1)
a = a / (n+1) a /= (n+1)
a = a % (n+1) a %= (n+1)
Example Explanation
x=100; After the execution the value of x will be 101.
x++;
Example Explanation
x=100; After the execution the value of x will be 99.
x--;
Operator Description
Operator Description
#include<stdio.h>
void main()
int a, b, c, d;
Modify Operators :-
Example 1:
int main( )
int x = 10 ,y;
y = ++ x;
Output: 11 11
Example 2:
int main( )
int x= 10 ,y;
y = x ++;
Output: 11 10
➢ These operators are used for testing , complementing or shifting the bits of binary
data.
Operator Meaning
| bitwise OR
^ bitwise exclusive OR
~ Bitwise complement
sizeof() operator in C
1. The sizeof() operator is a special operator used to return the size of its operand in
bytes.
2. Example:
1. int a, b;
2. b = sizeof(a);
6. It is used to allocate the memory space dynamically to variables during the execution.
#include<stdio.h>
void main()
{
int a, b;
float x = 4.5;
char m = ’A’;
double z = 6.0234;
a = 10;
b = sizeof(a);
b = sizeof(x);
b = sizeof(m);
b = sizeof(z);
}
Right Shift Operators
1. Representation >>
int a = 10 >>2;
1010 0010 2
#include<stdio.h>
int main( )
int a = 10>>2;
printf(“%d”, a);
getch( );
return 0;
Output : 2
1. Representation <<
00001010 00101000 40
#include<stdio.h>
int main( )
int a = 10<<2;
printf(“%d”, a);
getch( );
return 0;
Output : 40
Operator Meaning
* Pointer operator, it is used to declare pointer variable and to get value from it.
2. An expression may consist of one or more operands, and zero or more operators to
produce a value.
4. EVALUATION OF EXPRESSIONS:
➢ When there are multiple operators in an expression, they are evaluated according to
their precedence and associativity.
1. Example:
1. Area-of-C = 3.14 * r * r;
#include<stdio.h>
void main()
{
float a, b, c, x, y, z;
a = 9; b = 12; c = 3;
x = a – b / 3 + c * 2 – 1;
y = a – b / (3 + c) * 2 – 1;
z = a – ( b / (3 + c) * 2) – 1;
Output:-
x = 10.000000
y = 4.000000
z = 4.000000
5. OPERATOR PRECEDENCE:
➢ The operator with higher precedence is evaluated first and the operator with the least
precedence is evaluated last.
➢ We say that the multiplication operator (*) has higher "precedence" or "priority" than
the addition operator (+), so the multiplication must be performed first.
Operator Associativity
➢ Associativity is only used when there are two or more operators of same precedence.
➢ We say that the subtraction operator (-) is "left associative", so the left subtraction
must be performed first.
1. High Priority: *, /, %
2. Low Priority: +, -
#include<stdio.h>
void main()
float a, b, c, x, y, z;
a = 9; b = 12; c = 3;
x = a – b / 3 + c * 2 – 1;
y = a – b / (3 + c) * 2 – 1;
z = a – ( b / (3 + c) * 2) – 1;
Output
x = 10.000000
y = 4.000000
z = 4.000000
1. The input and output are two important operations that a C program is expected to do.
2. The input refers to read data from the console for the C program.
3. The output refers to display the data and results generated as a result of execution of a
C program.
4. The C language provides various Input and Output related functions and are classified
as Formatted and Non-formatted Input Output Functions(already covered).
5. We can use scanf() and printf() functions for input and output respectively.
6. Consider an example program which takes input from user and display output.
1. To read the character inputs and display it on the screen as output, we can use %c as a
format specifier.
2. The program is example for reading characters and display it as output can be given
as:
//Program to use character input and output
#include<stdio.h>
void main()
char t;
scanf(“%c”, &t);
Output
1. The formatted Input and Output(I/O) Functions can read and write values of all data
types.
3. We need to use a proper format specifier in both scanf() and printf() functions.
1. %d – for integers
2. %c – for characters
3. %f – for float
4. %e – for double
//Program to find largest of two numbers
#include<stdio.h>
void main()
int a, b, big;
big = (a > b) ? a : b ;
1. sprintf( ) :
sprintf stands for “string print”. This function is similar to printf( ) function but this function
prints the string into a character array instead of printing it on the console screen.
Syntax:
2. sscanf( ):
sscanf stands for “string scanf”. This function is similar to scanf() function but this function
reads data from the string or character array instead of the console screen.
Syntax:
#include <stdio.h>
int main()
{
char str[50];
int a = 2, b = 8;
printf("%s", str);
return 0;
Out put:
1. The non-formatted or unformatted Input and Output(I/O) Functions work only with
character data type.
#include <conio.h>
#include <stdio.h>
int main()
{
getch();
return 0;
Output:
1. E.g. putchar(variable_name);
2. putchar(x).
#include <conio.h>
#include <stdio.h>
int main()
return 0;
getchar( ) :
The getchar( ) function is used to read only a first single character from the keyboard
whether multiple characters is typed by the user and this function reads one character at
one time until and unless the enter key is pressed. This function is declared in
stdio.h(header file)
Syntax:
Variable-name = getchar();
#include <conio.h>
#include <stdio.h>
int main()
char ch;
ch = getchar();
printf("%c", ch);
return 0;
}
putchar( ):
The putchar() function is used to display a single character at a time by passing that
character directly to it or by passing a variable that has already stored a character. This
function is declared in stdio.h(header file)
Syntax:
putchar(variable_name);
#include <conio.h>
#include <stdio.h>
int main()
char ch;
// Reads a character
ch = getchar();
putchar(ch);
return 0;
}
Output:
gets():
gets() function reads a group of characters or strings from the keyboard by the user and
these characters get stored in a character array. This function allows us to write space-
separated texts or strings. This function is declared in stdio.h(header file).
Syntax:
char str[length of string in number]; //Declare a char type variable of any length
gets(str);
#include <conio.h>
#include <stdio.h>
int main()
// of length 50 characters
char name[50];
gets(name);
return 0;
Output:
puts():
Syntax:
puts(identifier_name );
#include <stdio.h>
int main()
char name[50];
gets(name);
// Displays string
puts(name);
return 0;
Output:
Assignment Questions: