0% found this document useful (0 votes)
27 views36 pages

Basic C Operators

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views36 pages

Basic C Operators

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Basic C Operators

Operator
• An operator is a symbol which operates on a value or a variable. For
example: + is an operator to perform addition.
Types of Operators
• Arithmetic Operators
• Increment and Decrement Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Conditional Operators
• Bitwise Operators
• Special Operators
Arithmetic Operators
• An arithmetic operator performs mathematical operations such as addition,
subtraction and multiplication on numerical values (constants and variables).
Example #1
•{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c=a/b;
printf("a/b = %d \n",c);
c=a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
Increment and decrement operators
• C programming has two operators increment ++ and decrement -- to
change the value of an operand (constant or variable) by 1.
• Increment ++ increases the value by 1 whereas decrement -- decreases
the value by 1.
• These two operators are unary operators, meaning they only operate
on a single operand.
Example #2
• #include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
Output
• ++c = 11.500000
++d = 99.500000
Assignment Operators
• An assignment operator is used for assigning a value to a variable. The
most common assignment operator is =
Example #3
• int main()
{
int a = 5, c;
c = a;
printf("c = %d \n", c);
c += a; // c = c+a
printf("c = %d \n", c);
c *= a; // c = c*a
printf("c = %d \n", c);
c /= a; // c = c/a
printf("c = %d \n", c);
c %= a; // c = c%a
printf("c = %d \n", c);
return 0;
}
Output
•c=5
c = 10
c=5
c = 25
c=5
c=0
Relational Operators
• A relational operator checks the relationship between two operands. If
the relation is true, it returns 1; if the relation is false, it returns value
0.
• Relational operators are used in decision making and loops.
Example #4
• int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d = %d \n", a, b, a == b); // true
printf("%d == %d = %d \n", a, c, a == c); // false
printf("%d > %d = %d \n", a, b, a > b); //false
printf("%d > %d = %d \n", a, c, a > c); //false
printf("%d < %d = %d \n", a, b, a < b); //false
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false
printf("%d != %d = %d \n", a, c, a != c); //true
printf("%d >= %d = %d \n", a, b, a >= b); //true
printf("%d >= %d = %d \n", a, c, a >= c); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true
printf("%d <= %d = %d \n", a, c, a <= c); //true
return 0;
}
Output
• 5 == 5 = 1
5 == 10 = 0
5>5=0
5 > 10 = 0
5<5=0
5 < 10 = 1
5 != 5 = 0
5 != 10 = 1
5 >= 5 = 1
5 >= 10 = 0
5 <= 5 = 1
5 <= 10 = 1
Logical Operators
• An expression containing logical operator returns either 0 or 1
depending upon whether expression results true or false. Logical
operators are commonly used in decision making in C programming.
Example #5
• int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) equals to %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) equals to %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) equals to %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) equals to %d \n", result);
result = !(a != b);
printf("!(a == b) equals to %d \n", result);
result = !(a == b);
printf("!(a == b) equals to %d \n", result);
return 0;
}
Output
Bitwise Operators
• During computation, mathematical operations like: addition,
subtraction, addition and division are converted to bit-level which
makes processing faster and saves power.
• Bitwise operators are used in C programming to perform bit-level
operations.
• Bitwise operator works on bits and perform bit-by-bit operation. The
truth tables for &, |, and ^ are as follows:
Other Operators
• Comma
• Sizeof()
Comma and sizeof()
• Comma operators are used to link related expressions together. For
example:
• int a, c = 5, d;
The sizeof operator
• The sizeof is an unary operator which returns the size of data
(constant, variables, array, structure etc).
Example #6
• int main()
{
int a, e[10];
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
printf("Size of integer type array having 10 elements = %lu bytes\n", sizeof
return 0;
}
Output
• Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Size of integer type array having 10 elements = 40 bytes
Ternary Operator (?:)
• A conditional operator is a ternary operator, that is, it works on 3
operands.
• Conditional Operator Syntax
conditionalExpression ? expression1 : expression2
• The conditional operator works as follows:
• The first expression conditionalExpression is evaluated first. This
expression evaluates to 1 if it's true and evaluates to 0 if it's false.
• If conditionalExpression is true, expression1 is evaluated.
• If conditionalExpression is false, expression2 is evaluated.
• Condition ? Expression1 : Expression2
The statement above is equivalent to:
if (Condition)
Expression1
else
Expression2
if/else statement:
if (total > 60)
grade = ‘P’
else
grade = ‘F’;
Conditional Statement
• total > 60 ? grade = ‘P’: grade = ‘F’;
OR
grade = total > 60 ? ‘P’: ‘F’;

• if/else statement:
if (total > 60)
printf(“Passed!!\n”);
else
printf(“Failed!!\n”);
Conditional Statement:
printf(“%s!!\n”, total > 60? “Passed”: “Failed”);
Example #7
• int main(){
char February;
int days;
printf("If this year is leap year, enter 1. If not enter any integer: ");
scanf("%c",&February);
// If test condition (February == 'l') is true, days equal to 29.
// If test condition (February =='l') is false, days equal to 28.
days = (February == '1') ? 29 : 28;
printf("Number of days in February = %d",days);
return 0;
}
Output
• If this year is leap year, enter 1. If not enter any integer: 1
Number of days in February = 29
• Other operators such as & (reference operator), * (dereference
operator) and -> (member selection) operator will be discussed in C
pointers.
Precedence Rules
• Precedence rules come into play when there is a mixed of arithmetic
operators in one statement. For example: x = 3 * a - ++b%3;
• The rules specify which of the operators will be evaluated first.
For example: x = 3 * a - ++b % 3;
how would this statement be evaluated?
If we intend to have the statement evaluated differently from the way specified by the
precedence rules, we need to specify it using parentheses ( )
Using parenthesis, we will have
x = 3 * ((a - ++b)%3);
The expression inside a parentheses will be evaluated first.
The inner parentheses will be evaluated earlier compared to the outer parentheses.
SUMMARY
This chapter exposed you the operators used in C
Arithmetic operators
Assignment operators
Equalities and relational operators
Logical operators
Conditional operator
Precedence levels come into play when there is a mixed of arithmetic
operators in one statement.
Pre/post fix - effects the result of statement

You might also like