Operators are used to perform operations on variables and values.
C operators
are one of the features in C which has symbols that can be used to perform
mathematical, relational, bitwise, conditional, or logical manipulations. The C
programming language has a lot of built-in operators to perform various tasks
as per the need of the program. Usually, operators take part in a program for
manipulating data and variables and form a part of the mathematical,
conditional, or logical expressions.
C divides the operators into the following groups:
◘ Arithmetic operators
◘ Assignment operators
◘ Comparison operators
◘ Logical operators
◘ Bitwise operators
Arithmetic Operators:
Arithmetic operators are used to perform common mathematical operations.
Operator Name Description Example
+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x/y
% Modulus Returns the division remainder x%y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x
Assignment Operators:
Assignment operators are used to assign values to variables.
A list of all assignment operators:
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Comparison Operators:
Comparison operators are used to compare two values (or variables). This is
important in programming, because it helps us to find answers and make
decisions.
The return value of a comparison is either 1 or 0, which means true (1)
or false (0). These values are known as Boolean values.
A list of all comparison operators:
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Logical Operators:
You can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables or values.
Operator Name Description Example
&& Logical and Returns true if both statements are true x < 5 && x < 10
|| Logical or Returns true if one of the statements is true x < 5 || x < 4
! Logical not !(x < 5 && x < 10)
Reverse the result, returns false if the result is
true
Bitwise Operators:
Bitwise operators are used on (binary) numbers:
Operator Name Description Example
& AND Sets each bit to 1 if both bits are 1 x&y
| OR Sets each bit to 1 if one of two bits is 1 x|y
^ XOR Sets each bit to 1 if only one of two bits is 1 x^b
<< Shift left by pushing zeros in from the right x << 2
Zero fill left
shift
>> x >> 2
Signed right Shift right by pushing copies of the leftmost bit in from the
shift left, and let the rightmost bits fall off
Level: 1
1. What would be the value of 'a':
a. int a = 10/45*23%45/(45%4*21)
b. float a = 10+45.0*23-45+(4*21.0)
2. True or false (0 or not):
a. 4>5 && 5>4
b. 4>5 || 5>4
c. (232+23*1233) || 0
d. (232+23*1233) &&
3. If a is 15, then what would be the value of:
printf("%d",++a);
printf("%d",a++); printf("%d",--
a); printf("%d",a--);
4. What would be the output of:
printf("%d\n",1==5==5);
5. What is the error in:
int a;
3=a
Level: 2
1. Length and breadth of a rectangle are 5 and 7 respectively. Write a program to calculate the
area and perimeter of the rectangle.
2. Write a program to input the value of the radius of a circle from keyboard and then calculate its
perimeter and area.
3. Write a program to enter a 4 digit number from keyboard. Add 8 to the number and then divide
it by 3. Now, the modulus of that number is taken with 5 and then multiply the resultant value
by 5. Display the final result.
4. Now, solve the above question using assignment operators (e.g. +=, -=, *=).
5. Enter two numbers from keyboard. Write a program to check if the two numbers are equal.
6. Write a program to enter the values of two variables 'a' and 'b' from keyboard and then check if
both the conditions 'a < 50' and 'a < b' are true.
7. Now solve the above question to check if atleast one of the conditions 'a < 50' and 'a < b' is
true.
8. If the marks of Robert in three subjects are 78, 45 and 62 respectively (each out of 100 ), write a
program to calculate his total marks and percentage marks.
9. Write a program to enter the values of two variables from the keyboard and then interchange
the values of the two variables. E.g.: If entered value of x is 5 and y is 10 then printf("%d
%d",x,y )
should print 10 5.
10. Take input of some length in meter and covert it to feet and inches.
11. Write a program to convert Fahrenheit into Celsius.
12. The total number of students in a class are 45 out of which 25 are boys. If 80% of the total
students secured grade 'A' out of which 15 are boys, then write a program to calculate the total
number of girls getting grade 'A'.
Level: 3
1. Write a program to calculate the sum of the first and the second last digit of a 5 digit
number entered from the keyboard.
2. Suppose you entered a 4 digit number. Write a program to display a number whose digits
are 2 greater than the corresponding digits of the entered number. For example, if he entered
number is 5696, then the displayed number should be 7818.
3. Write a program to calculate the sum of the digits of a 3-digit number which is entered
from keyboard. E.g. INPUT : 132 OUTPUT : 6
4. Write a program to reverse a 3-digit number which is entered from keyboard. E.g. INPUT:
132 OUTPUT: 231