C PROGRAMMING Lesson 5
C PROGRAMMING Lesson 5
13. What do you understand by float type? Write down with example.
Depending on the number of operands that an operator can act upon, operators can be
classified as follows:
1. Unary Operators
2. Binary Operators
3. Ternary Operators
1. Unary Operators:Those operators that require only single operand to act upon are
known as unary operators. For Example increment(++) and decrement(–) operators.
2. Binary Operators:Those operators that require two operands to act upon are called
binary operators.Binary operators are classified into :
1. Arithmetic operators (+, -, * etc.)
2. Relational Operators ( <, >, ==)
3. Logical Operators (&&, ||)
4. Assignment Operators (=, +=, -=)
5. Bitwise Operators (&, |)
3. Ternary Operators: These operators requires three operands to act upon. For
Example Conditional operator(?:).
Operators Description
Relational operators: These operators are used to compare the value of two operands. For
example: checking if one operand is equal to the other operand or not, an operand is greater
than the other operand or not etc.
Logical operators: Logical Operators are used to combine two or more conditions/constraints
or to complement the evaluation of the original condition in consideration. The result of the
operation of a logical operator is a boolean value either true or false.
Assignment operators: These operators are used to assign the values for the variables in C
programs. The left side operand of the assignment operator is a variable and right side
operand of the assignment operator is a value. The value on the right side must be of the same
data-type of variable on the left side otherwise the compiler will raise an error.
Simple assignment operator. Assigns values from right C = A + B will assign the
= side operands to left side operand value of A + B to C
Increment/decrement operators: Increment Operators are used to increase the value of the
variable by one and Decrement Operators are used to decrease the value of the variable by
one in C programs. Both increment and decrement operator are used on a single operand and
variable, so it is called as a unary operator. Unary operators are having higher priority than the
other operators it means unary operators are executed before other operators.
Example of pre-increment
4
main()
5
{
6
int x,i;
7
i=10;
8
x=++i;
9
printf("x: %d",x);
10
printf("i: %d",i);
11
getch();
12
}</span>
post-increment (variable ++): In Post-increment first value of variable is used in the
expression (initialize into another variable) and then increment the value of variable.
Example of post-increment
4
void main()
5
{
6
int x,i;
7
i=10;
8
x=i++;
9
printf("x: %d",x);
10
printf("i: %d",i);
11
getch();
12
}</span>
o pre-decrement
o post-decrement
Pre-decrement (– variable): In pre-decrement first decrement the value of variable and then
used inside the expression (initialize into another variable).
Example of pre-decrement
void main()
int x,i;
i=10;
x=--i;
printf("x: %d",x);
printf("i: %d",i);
getch();
#include<stdio.h>
#include<conio.h>
void main()
int x,i;
i=10;
x=i--;
printf("x: %d",x);
printf("i: %d",i);
getch();
Conditional operators: Conditional operators return one value if condition is true and returns
another value is condition is false. This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
In above example, if A is less than 0, Negative is returned else Positive is returned. This is
equal to if else conditional statements.
Bitwise operators: These operators are used to perform bit-level operations on the
operands. The operators are first converted to bit-level and then calculation is performed on
the operands. Bitwise operators can not be used on float and double type data.
Operators Description
4.3.Expression
Expression: Operators, functions, constants and variables are combined together to form
expressions. Consider the expression A + B * 5. where, +, * are operators, A, B are variables,
5 is constant and A + B * 5 is an expression.
Mathematical expressions are written in c program in the following way:
Operator precedence and associativity in expression:
Operator precedence determines which operator is performed first in an expression with more
than one operators with different precedence. For example 10 + 20 * 30 is calculated as 10 +
(20 * 30) and not as (10 + 20) * 30.
Associativity is used when two operators of same precedence appear in an expression.
Associativity can be either Left to Right or Right to Left. For example ‘*’ and ‘/’ have same
precedence and their associativity is Left to Right, so the expression “100 / 10 * 10” is treated
as “(100 / 10) * 10”.
Here, operators with the highest precedence appear at the top of the table, those with the
lowest appear at the bottom. Within an expression, higher precedence operators will be
evaluated first.
Lesson Evaluation-