0% found this document useful (0 votes)
3 views64 pages

Module 2

The document provides an overview of operators in the C programming language, categorizing them into arithmetic, relational, equality, logical, unary, conditional, bitwise, assignment, comma, and sizeof operators. It explains the functionality and usage of each operator type, along with examples, and discusses operator precedence, type conversion, typecasting, decision control, looping statements, and the use of break and continue statements. Additionally, it includes assignments and programming tasks for practical understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views64 pages

Module 2

The document provides an overview of operators in the C programming language, categorizing them into arithmetic, relational, equality, logical, unary, conditional, bitwise, assignment, comma, and sizeof operators. It explains the functionality and usage of each operator type, along with examples, and discusses operator precedence, type conversion, typecasting, decision control, looping statements, and the use of break and continue statements. Additionally, it includes assignments and programming tasks for practical understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

MODULE 2

( O P E R ATO R S I N C )
OPERATORS IN C
• Operator is a symbol that specifies that mathematical, logical or relational operation to be performed.
• C language supports different types of operators, which can be used with variables and constants to form
expression.
Following are the category of operators:
1. Arithmetic operator
2. Relational operator
3. Equality operator
4. Logical operator
5. Unary operator
6. Conditional operator
7. Bitwise operator
8. Assignment operator
9. Comma operator
10. sizeof operator
Arithmetic operator:
• Arithmetic operators can be applied to any integer or floating-point number
• the addition subtraction multiplication and division operators perform the usual arithmetic operations in C programs.
• The operator % finds the remainder of an integer division. This operator can be applied only to integer operands and cannot
be used on slot or double operands. (a=9, b=3).
Relational operator & Equality operator
• Also known as comparison operator
• Expressions that contain relational operators are called as relational expressions.
• Can be used to determine the relationship between the operands.
• Relational operators are evaluated from left to right.
Logical operators:
• Logical AND (&&), logical OR (||), Logical NOT (!).
• logical expressions are also evaluated from left to right
• If both or one operands is false, then the whole expression evaluates to false.
• Majority of the used in combination of relational operators.

(a < b) && ( b > c) ( if first expression is false second exp is not evaluated)
(a < b) || ( b > c)
b= !a;
Produces 0, if the expression evaluated to non-zero value.
Produces 1, if the expression produces 0.
Unary operator:
• Unary operators act on single operands.
• C supports three unary operator: unary minus, increment, decrement.

Unary minus
The unary operator negates the value.
int a,b=10;
a= -(b);
Increment operator ( ++ ) and Decrement operator ( -- )
• Increment operator increases the value of the operand by 1.
• Decrement operator decreases the value of the operand by 1.
• Both of them have two variants: prefix and postfix.
• In prefix expression (++x or - -x), the operator is applied before the operand is fetched for the
computation and thus the altered value is used for computation of the expression in which it
occurs.
• On contrary in a postfix expression(x++ or x--), an operator is applied after an operand is
fetched for computation therefore the unaltered value is used for computation of the
expression in which it occurs.
• x++ is not same as ++x
Conditional operator:
• The conditional operator or the ternary( ?: ) is just like an if else statement that can be used within
expression.
• the syntax of the conditional operator is
exp1 ? exp2 : exp3
Exp1 is evaluated first. If it is true, then exp2 is evaluated and becomes the result of the expression, otherwise
exp3 is evaluated and becomes the result of the expression.

large = (a>b) ? a : b
Bitwise operators:
• bitwise operators are those operators that perform operations at the bit level.
• Bitwise AND, bitwise OR, shift operators.

Bitwise AND: ( & )


• Bitwise AND performs operations on bits instead of bytes, char etc.
• The bit in the first operand is ANDed with the corresponding bit in the second operand.

Bitwise OR: ( | )
• The bit in the first operand is ORed with the corresponding bit in the second operand

Bitwise NOT: ( ~ )
The bitwise NOT, or complement, is a unary opeartor that performs logical negation on each bit of the operand.
Bitwise not operator set the bit to 1, if it was initially zero and sets it to 0 if it was initially one

Bitwise XOR: ( ^ )
a^b is 1 only if one of the inputs is 1.
Shift Operators:
• C supports to shift operators: shift left ( << ) and shift right ( >> ).
• These operations are simple and are responsible for shifting bits either left or to the right.
• The syntax of shift operation:
operand op num;
Assignment operators:
• Assignment operator is responsible for assigning values to the variables.
• The assignment operator has right to left associativity, so the expression
a=b=c=10;
id evaluated as
(a=(b=(c=10)));

• The operand to the left of the assignment operator must be a variable name.
• C Does not allow any expression constant or function to be placed to the left of the assignment operator.
• The statement a+b=sum is invalid in C. sum=a+b is valid.

Shorthand assignment operator:


Num1+ = num2; is equivalent to num1= num1+num2;
Advantages of shorthand operators:
• Shorthand expressions are easier to write as the expression on the left side need not be repeated.
• The statements involving shorthand operators are easier to read as they are more concise.
• The statements involving shorthand operators are more efficient and easy to understand.

If , num1=3, num2=5;
num1+=num2 * 4 -7
What is the final value of num1?
Comma operator:
• The comma Operator in C takes 2 operands.
• It works by evaluating the first and discarding it and then evaluate the second and returns the value as the result
of the expression.
• Comma Separated operands when changed together are evaluated in left to right sequence which is the right
most value yielding the result of the expression.
• Among all the operators, the comma operator has the lowest precedence.

int a=2, b=3, x=0;


x = ( ++a , b+=a);
what is the value of x?
sizeof operator:
The size of operator is a unary operator used to calculate the size of data types.
This operator can be applied to all data types.
When using this operator the keyword sizeof is followed by a type name variable or expression.
The operator returns the size of the variable data type or expression in bytes i.e, the size of operator is used to
determine the amount of memory space the variable or expression or data type will take.
int a=10;
result = sizeof (a);
Operator precedence chart:
• C operator has 2 properties: priority and associativity

X= 3 * 4 + 5 * 6
X= 2 * (4 % 5) / 2
X= 3 * 4 % ( 5/ 2).
Type conversion and Typecasting:
• What happens when expressions involved 2 different data types like
multiplying a floating-point number and an integer.
• Such type of situations are handled either through type conversion or
type casting.
• Type conversion or type casting of variable (value) refers to changing of
variable of one data type into another.
• Type conversion is done implicitly whereas typecasting has to be done
explicitly by the programmer.

Type conversion:

float x; float f=3.5


int y=3; int i;
x=y; i=f;
Integer data type is promoted to float this is known as promotion.
Demotion is also possible but with loss in information. (No compilation
warning message is generated when information is lost while demoting the
type of data)
Typecasting:
• Typecasting is also known as forced conversion.
• Type casting and arithmetic expression tells the compiler to represent the value of the
expression in a certain way.
• Casting is under the programmers control and not under compilers control.

float salary=1000.00;
int sal;
sal = (int) salary;

int a=500, b=50;


float res;
res = (float) a/b;
Assignment:
1. Write a program to convert floating point number into the corresponding integer by type casting.
2. Write a program to convert integer number into the corresponding floating point by type casting.
Decision Control and Looping Statements
Assignment
1. Write a program to print the ASCII value of a character.
2. Write a program to read a character in upper case and then print it in lower case
3. Write a program to convert degrees Fahrenheit into degrees Celsius.
4. Write a program that displays size of every data type
• Dangling else problem.

Rules to remember:
1. The expression must be enclosed in parenthesis.
2. No semicolon Is placed after if/ if else/if-else-if statement. Semicolon displaced only at the end of statements
in the statement block.
3. The statement block begins and ends with a curly brace.
Switch Case:
Switch statements are mostly used in two situations:
1. When there is only one variable to evaluate in the expression.
2. When many conditions are being tested or cases has to be matched.

A switch-case is multi-way decision statement version of an if-else block that evaluates only variable.

Advantages of switch-case:
• Easy to debug
• easy to read and understand
• ease of maintenance as compared with its equivalent if else statements
• switch statements can also be nested
• executes faster than equivalent if else construct
Iterative statements:
• Iterative statements are used to repeat the execution of a list of statements depending on the
value of an integer expression.
• C language supports 3 types of iterative statements also known as looping statements
1. while loop
2. do-while loop
3. for loop

In any iterative statements three points are must


1. Loop variable initialization
2. Test condition
3. Condition update.
while loop:
• The while loop provides a mechanism to repeat one or more statements while the particular condition is
true.
• Loop variable is important.
do-while
• The do while loop is similar to while loop, the only difference is that in a do while loop the test condition is
evaluated at the end of the loop.
• As the test condition is evaluated at the end this clearly means that the body of the loop gets executed at least one
time(even if the condition is false)
for loop
• For loop provides a mechanism to repeat a task until a particular condition is true.
In the below code, i is the loop variable.
Points to be remembered about for loop:
• In a for loop any or all the expressions can be omitted. In case all the expressions are omitted then there
must be 2 semicolons in the for loop. for(; ;)
• There must be no ; after a for statement.
• Multiple initializations must be separated with the comma.
• Multiple statements can be included in the third part of the for statement.
for(i=0, j=10; i<j; i++, j--)

• The controlling variable can be incremented or decremented by any value. (not only 1)
for(i=0; i<=10; i+=2)

• Never use floating point variable as the loop control variable this is because floating point
number values are just approximations and therefore may result in imprecise values.

Pre-test loop  for, while


Post-test  do-while
Nested loop:
Assignment Write the C program to get the following output.
Assignment:
1. Write a program using for loop to print the factorial of a given positive number.
2. Write a program to classify the given number as prime or composite number.
break statement:
break statement:
• Used to terminate the execution of the
nearest enclosing loop in which it appears.
• Used widely with for loop, while loop and do-
while loop.
continue statement:
• When compiler encounter the continue
statement, then the rest of the statements in
the loop are skipped and the control is
unconditionally transferred to the loop
continuation portion of the nearest
enclosing loop.
Assignment:
1.Write a program using for loop to print all the numbers from range of m to n thereby classifying them as even
or odd
2. Write a program to print the reverse of a number.
3. Write a program to enter a binary number calculate and display the decimal equivalent of this number.
Study assignment:
Study assignment:
goto statement :
• Used to transfer control to a specified label.
• Label must reside in the same function.
• goto can't be used to jump across different functions.
• To jump between functions, you can look at setjmp()
and longjmp() functions.
• It is not necessary to use goto statement as it can always be eliminated by rearranging the code
• using the goto statement violates the rules of structured programming
• it is a good programming practice to use break, continue, and return statements in preference to goto
• go to statements make the problem code complicated and render program and readable
• Please refer textbook for more programs and execute.

You might also like