0% found this document useful (0 votes)
4 views

c Programming-chapter 03

The document provides an overview of operators and expressions in the C programming language, detailing various types of operators including arithmetic, relational, logical, bitwise, increment/decrement, assignment, and special operators. It explains their functionalities, syntax, and provides examples for better understanding. Additionally, it covers operator precedence, evaluation of expressions, and lists built-in mathematical functions available in the 'math.h' header file.

Uploaded by

flxv07
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)
4 views

c Programming-chapter 03

The document provides an overview of operators and expressions in the C programming language, detailing various types of operators including arithmetic, relational, logical, bitwise, increment/decrement, assignment, and special operators. It explains their functionalities, syntax, and provides examples for better understanding. Additionally, it covers operator precedence, evaluation of expressions, and lists built-in mathematical functions available in the 'math.h' header file.

Uploaded by

flxv07
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/ 27

WELCOME

PRESENTED BY
SHEYONA G
CHAPTER-03
Operators and
Expressions
OPERATORS
 Operators are symbols that are used to perform mathematical or logical
operations. The C programming language is rich with built-in operators.
TYPES OF OPERATORS

a. Arithmetic Operators
b. Relational Operators
c. Logical Operators
d. Bitwise Operators
e. Increment and Decrement Operator
f. Assignment Operator
g. Special Operators
h. Conditional / Ternary Operator
ARITHMETIC OPERATORS

Arithmetic Operators are used to performing mathematical calculations like addition (+), subtraction (-),
multiplication (*), division (/) and modulus (%).
Symbol Description Syntax

+ Plus Adds two numeric values. a+b

- Minus Subtracts right operand from left operand a-b


* Multiply Multiply two numeric values a*b

/ Divide Divide two numeric values. a/b

% Divide Returns the remainder after diving the left a%b


operand with the right operand.
RELATIONAL OPERATORS

Relational operators are used to comparing two quantities or values. These are used with
the if condition evaluator. The logic is :

if (condition true)
{
Execute statements in this block
}
else // if not true
{
Execute statements in this block
}
LOGICAL OPERATORS
C provides three logical operators when we test more than one condition to make
decisions. These are:

&& (meaning logical AND),


|| (meaning logical OR)
! (meaning logical NOT).
Example: Program to find entered character is vowel or not
Void main()
{
char ch;

// Checking if the character ch


// is a vowel or not.
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E'|| ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
{

printf("The character %c is a vowel.\n", ch);


}
else
{
printf("The character %c is a consonant.\n", ch);
}
BITWISE OPERATORS
The data is always stored in computer in the form of bytes (8 bits). C provides a special operator for bit
operation between two variables.
In arithmetic-logic unit (which is inside the CPU), mathematical operations like: addition, subtraction,
multiplication and division are done at bit-level. To perform bit- level operations in C programming,
bitwise operators are used.
Operator Description

& Bitwise AND Operator


| Bitwise OR Operator
^ Bitwise XOR Operator
<< Bitwise LEFT SHIFT Operator
>> Bitwise RIGHT SHIFT Operator
BITWISE AND OPERATOR
The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand
is 0, the result of corresponding bit is evaluated to 0.
BITWISE OR OPERATOR

The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Programming,
bitwise OR operator is denoted by |.

BITWISE XOR OPERATOR


The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite.
It is denoted by ^
Bitwise Right & Left Shift Operators

Right Shift Operator


Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by >>.
If a=21 which is 10101 in Binary Form
if a is right shifted by 1 i.e a = a >> 1

Left Shift Operator

Left shift operator shifts all bits towards left by certain number of specified bits. It is denoted by <<.
If a=21 which is 10101 in Binary Form
if “a is left-shifted by 1” i.e a = a << 1
INCREMENT AND DECREMENT OPERATORS

The increment ( ++ ) and decrement (- - ) operators in C are unary operators for incrementing and
decrementing the numeric values by 1 respectively

Increment Operator in C
The increment operator ( ++ ) is used to increment the value of a variable in an expression by 1
Increment Operator can be used in two ways

1. Pre-Increment: ++ m
2. Post-Increment: m++

Decrement Operator in C
The decrement operator ( - -)is used to decrement the value of a variable in an expression by 1
decrement Operator can be used in two ways

3. Pre-decrement: ++ m
4. Post-decrement: m++
Increment Operator decrement Operator
If a= 10 If a= 10
X=++a X=- -a
X=a++ X=a - -

Pre Increment Operation => x=++a Pre decrement Operation => x=- -a
a = 11 a=9
x = 11 x=9
Post Increment Operation=> x=a- -
Post Increment Operation=> x=a++
a = 10
a = 10
x=9
x = 11
ASSIGNMENT OPERATORS
Assignment operators applied to assign the result of an expression to a variable. C has a collection of
shorthand assignment operators
SPECIAL OPERATORS
C supports some special operators

#include <stdio.h>
void main()
{
int i=10;
printf("integer: %d\n",
sizeof(i));
getchar();
}
CONDITIONAL OPERATOR
CONDITIONAL OPERATOR is called as ternary operator which is the conditional
operator to construct conditional expressions.
?: is the conditional operator

Syntax:

ConditionExpression ? True statement or value : False statement or value;

Example:
int a=30,b=20,c;
c=a>b?a:b;
OPERATORS PRECEDENCE, EVALUATION OF EXPRESSIONS,
MATHEMATICAL FUNCTIONS

OPERATORS PRECEDENCE IN C

 When more than one arithmetic operator appears in an arithmetic expression then it is
important to note which operator is evaluated first, which is operated second and
which operator has the last priority. The order in which the arithmetic operators are
executed is called the hierarchy of operations.
 C operators in order of precedence (highest to lowest). Their associatively indicates in
what order operators of equal precedence in an expression are applied.
Example:

Determine the hierarchy of operations and evaluate the following

k=3/2*4+3/8+3
Stepwise evaluation of this expression is shown below:

k=3/2*4+3/8+3

k=1*4+3/8+3 operation 1: 3/2 (from left to right)


k=4+3/8+3 operation 2: 1*4 (from left to right)
k=4+0+3 operation 3: 3/8 (from left to right)
k=4+3 operation 4: 4+0 (from left to right)
k=7 operation 5: 4+3
LIST OF INBUILT C FUNCTIONS IN MATH.H FILE

The “math.h” header file supports all the mathematical related functions in C
language.
All the arithmetic functions used in C language use math.h header file
Function Description

floor ( ) This function rounds a number down to the nearest Floor(4.7) is 4


integer. Floor of (-4.7) is 5
round ( ) This function returns the nearest integer value of the The rounded value of 1.4 is 1.0
float/double/long double argument passed to this The rounded value of 1.5 is 2.0
function. The rounded value of -1.5 is -2.0
If decimal value is from ".1 to .5", it returns integer The rounded value of -1.4 is -1.0
value less than then argument. If decimal value is from
".6 to .9", it returns the integer value greater than the
argument.
ceil ( ) This function rounds a real number up to the nearest Ceil of (4.7) is 5
integer Ceil of (-4.7) is 4
Function Description

sin ( ) This function is used to calculate sine value. Sin(90) = 1


cos ( ) This function is used to calculate cosine. cos(90) = 0
tan ( ) This function is used to calculate tangent. tan(90) = not defined
exp () This function is used to calculate the exponential "e" to Where e is (Euler’s Number = 2.71828)
the xth power.
log () This function is used to calculates natural logarithm. log(5.6) = 1.72
sqrt () This function is used to find square root of the Sqrt(4)=2
argument passed to this function
pow ( ) This is used to find the power of the given number. Pow(2,3) = 23=8
trunc.(.) This function truncates the decimal value from floating Trun(4.9)=4
point value and returns integer value. Trun(3.768)=3
ASSIGNMENT QUESTIONS

1. List out any 4 operator in C language


2. Explain Arithmetic operator in C with example
3. Write a C program to perform arithmetic operations by using +,-,* operators by declaring int data type.
4. Explain Relational operator in C with example
5. What are the logical operator in C and write the symbol.
6. What is the symbol of assignment operator and how will you assign a value using this operator?
7. What is the value of x if int x=5%2+5/2
8. Find the value of x if x=9/4+5
9. List out any 4 mathematical function in C language

You might also like