bpops-203-module-2
bpops-203-module-2
Module -2
Operators In C
EXPRESSIONS
OPERATOR:
It is a symbol or a token that specifies the operation to be performed on various types of data.
Ex: +, -, *
OPERAND:
A constant or a variable or a function which returns a value is an operand. An operator may
have one or two o three operands.
EXPRESSION:
A sequence of operands and operators that reduces it to a single value on solving it is an
expression. Eg: a+b-c*a/2
CLASSIFICATION OF OPERATORS
It is classified based on:
1. The number of operand on operator has.
2. The type of operation being performed.
a. UNARY OPERATOR:
An operator which acts on only one operand to produce the result. Eg: -10, -a, *b, a++ etc.
b. BINARY OPERATOR:
An operator which acts on two operands to produce the result. Eg: a+b, a*b.
c. TERNARY OPERATOR:
An operator which acts on three operands to produce a result. Eg: a?b:c
1. ARITHMETIC OPERATORS:
The operators that are used to perform arithmetic operations such as addition, subtraction,
multiplication, division and modulus operations are called arithmetic operators. These
operators perform operation on two operands and hence are binary operators.
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
ARITHMETIC EXPRESSIONS:
An expression is a sequence of operands and operators which when evaluated results in a single
value. Two types:
TYPE CONVERSION:
In C language, a programmer can instruct the compiler to convert the data from one data type
to another data type. This process is called type conversion.
INTEGER EXPRESSIONS:
If all the operands in an expression are integers, the expression is called integer expression. Eg:
4/2=2, 2+5=7.
If all the operands in an expression are floating point numbers or double values, it is floating
point expression. Eg: 4.0/2.0=2.0
An expression that has operands of different data types is called mixed mode expressions. Eg:
4.0/3=1.333
Highest to lowest:
ASSOCIATIVITY OF OPERATORS:
In programming languages and mathematical notation, the associativity (or fixity) of an
operator is a property that determines how operators of the same precedence are grouped in the
absence of parentheses.
1. Left associativity (left to right): operators are grouped from the left.
2. Right associativity (right to left): operators are grouped from the right
2. RELATIONAL OPERATORS:
The operators that are used to find the relationship between two operands are called relational
operators.
i. Less than ( < )
ii. Less than or equal to ( <= )
iii. Greater ( > )
iv. Greater than or equal to ( >= )
v. Equal ( == )
vi. Not equal ( != )
3. LOGICAL OPERATORS:
If the data value is 0, it is considered false. Else it is true. The operators that are used to combine
two or more relational expressions are called logical operators. Logical operators are used to
combine two or more relational expressions. The output will be either true or false.
1. Unary operator ( ! )
2. Binary operator ( && , || ) Left to right associativity for both.
Logical NOT ( ! ):
OPERAND !OPERAND
True (1) False (0)
False (0) True (1)
Logical OR ( || ):
4. ASSIGNMENT OPERATORS:
An operator which is used to assign the data or result of an expression into a variable (also
called memory location) is called an assignment operator. It is denoted by ‘=’ sign. A statement
with assignment operator is called assignment statement or assignment expression.
i. POST INCREMENT
eg:
#include <stdio.h>
int main()
{
int a=5, b;
b=a++;
return 0;
}
#include <stdio.h>
int main()
{
int a=5, b;
b=++a;
return 0;
}
DECREMENT OPERATOR:
Types:
i. Post decrement. Eg: a++
ii. Pre decrement. Eg: ++a
return 0;
}
#include <stdio.h>
int main()
{
int a=5, b;
b=--a;
return 0;
}
6. CONDITIONAL OPERATOR
It is also called ternary operator.
#include <stdio.h>
int main()
{
int a=7, b=8, big;
big=(a>b)?a:b;
printf(“%d”, big);
return 0;
}
output: 8
7. BITWISE OPERATOR
The operators that are used to manipulate the bits of given data are called bitwise operators.
Types:
i. Bit-wise negative (~)
ii. Left shift (<<)
iii. Right shift (>>)
iv. Bit-wise AND (&)
v. Bit-wise OR (|)
vi. Bit-wise XOR (^)
#include <stdio.h>
int main()
{
int a=10;
int b=~a;
return 0;
}
Syntax: b=a<<num;
Eg: b=5<<1;
Syntax: b=a>>num;
Eg: b=5>>1;
then, a: 00001 0 10
b: 00000 110
c: 00000 010 i.e 2 in decimal.
v. BIT-WISE OR:
If the corresponding bit positions in both the operands are 0, then OR operation results in 0.
Otherwise OR operation results in 1.
b: 0000 0110
b: 00000 110
8. SPECIAL OPERATORS
i. COMMA OPERATOR:
It has the least precedence among all the operators and is left associative. It is normally
used to:
Separate items in the list. Eg: a=12,13,14; // 13 and 14 are discarded.
Combine two or more statements into a single statement. Eg: a=10, b=20, c=30;
Eg:
printf(“%d%d”,a,b);
sizeof():
this operator is used to determine the number of bytes occupied by a variable or a
constant in the memory.
EXPRESSION FORMATS
Types of C expressions:
1. Primary
2. Unary
3. Binary
4. Ternary
5. Assignment
6. Comma
PRIMARY EXPRESSIONS:
An expression with only one operand but without any operator.
1. Names: variable or a function. Eg: const int MAX=10; int a,b;
2. Constants: a piece of data that cannot be changed during the execution of the program.
3. Parenthesized expressions: any expression or a value enclosed in parentheses must be
reducible to a single value.
UNARY EXPRESSIONS:
An expression with only one operand and one operator is called unary expression.
1. Unary minus expression. Eg: -5
2. Unary plus expression. Eg: +5
3. Prefix expression. Eg: ++i
4. Postfix expression. Eg: i++
BINARY EXPRESSIONS:
An expression containing two operands and an operator.
1. Multiplicative expressions. Eg: 2*4, a/b
2. Additive expressions. Eg: a-b, a+b
3. Relational expressions. Eg: a>b, a<b
4. Logical expressions. Eg: a &&b
5. Bitwise expressions. Eg: a&b, a|b
TERNARY EXPRESSIONS:
An expression containing three operands and two operators. Eg: a?b:c
ASSIGNMENT EXPRESSIONS:
A statement with assignment operator. E: a=10, a=b*c
COMMA EXPRESSIONS:
A set of statements separated by comma are evaluated from left to right one after the other.
Eg: a=10, b=30;
STATEMENTS
In a programming language, it is used to inform the computer to perform an action when a
program is executed. A statement can alter the value of a variable, it can accept the input, it
can manipulate the data and display the data.
1. Expression statement
2. Compound statement
3. Control statement
1. EXPRESSION STATEMENT:
An expression followed by a semicolon. An expression statement include:
Statements with assignment operator. Eg: a=b*c; a=50;
Statements consisting of only expressions. Eg: i++;
Statements that invoke the functions. Eg: printf(“hello”); sum(n);
2. COMPOUND STATEMENT:
The sequence of statements enclosed within a pair of braces ‘{‘ and ‘}’ is called a compound
statement. Eg:
{
sum=a+b;
printf(“sum = %d “,sum);
}
3. CONTROL STATEMENT:
The order in which the statements are executed is called control flow.
Three types:
1. Sequential control statements
2. Branching statements
3. Loop statements
ALGORITHM:
An algorithm is defined as unambiguous, step by step procedure to solve a given problem in
finite number of steps by accepting a set of inputs and producing desired result for the given
problem. After producing the result, algorithm should terminate.
FLOW CONTROL:
A algorithm or a program contains various actions to be executed in the form of statements.
Specifying the order in which various actions have to be executed is called flow control.
CHARACTERISTICS/PROPERTIES OF ALGORITHMS:
1. Each and every instruction should be precise and unambiguous i.e. each and every
instruction should be clear and should have only one meaning.
2. Each instruction should be performed in finite time.
3. One or more instructions should not be repeated infinitely. It means that the algorithm
must terminate ultimately.
4. After the instructions are executed, the user should get the required results.
DISADVANTAGES:
It is time consuming & cumbersome as an algorithm is developed first which is converted into
flowchart and then into a computer program.
FLOWCHART:
A flowchart is a type of diagram that represents an algorithm or process, showing the steps as
boxes of various kinds, and their order by connecting them with arrows. This diagrammatic
representation illustrates a solution to a given problem. Process operations are represented in
these boxes, and arrows; rather, they are implied by the sequencing of operations. Flowcharts
are used in analyzing, designing, documenting or managing a process or program in various
fields.
DISADVANTAGES:
Complex logic: Sometimes, the program logic is quite complicated. In that case,
flowchart becomes complex and clumsy.
Alterations and Modifications: If alterations are required the flowchart may require re-
drawing completely.
FLOWCHART ALGORITHM
It is a graphical or pictorial representation along They are expressed in English language along with
with instructions mathematical expressions
For complex problems, flowcharts become They can be used for complex or simple problems
complex
Modification is difficult Can be modified easily
TYPES OF ERRORS
1. Syntax errors
2. Logical errors
3. Runtime errors
SYNTAX ERRORS:
Each language has a set of rules that are to be followed while writing a program. During
compilation, the programming statements that violate these grammatical rules are identified by
the compiler and issue error messages so that programmer can locate and correct them.
Eg:
#include <stdio.h>
void main()
{
int a;
a=10 //error: missing ;
print(“a value= %d”,a); //error: print is undefined. It must be printf
}
LOGICAL ERRORS:
It is also called as semantic errors. When a program is compiled and executed successfully and
if the desired output not obtained, then logical errors are present.
Eg:
#include <stdio.h>
void main()
{
int a,b,sum;
printf(“enter 2 numbers: “);
scanf(“%d%d”,&a,&b);
sum=a-b; //we find the difference and not the sum! No error but answer is not
expected. printf(“\nsum= %d”,sum);
}
RUNTIME ERRORS:
It occurs when a program attempts to perform a task that is not allowed. The statements which
results in run time errors are syntactically and logically correct.
Eg: attempting to divide by a zero.
#include <stdio.h>
void main()
{
CONTROL STATEMENT
The order in which the statements are executed is called control flow. The statements that are
used to control the flow of execution of the program are called control statements. Based on
the order in which the statements are executed, the various control statements are classified
into:
1. Sequential control statements
2. Branching statements: if else
3. Loop statements: for, while
SEQUENTIAL STATEMENTS:
The programming statements that are executed sequentially (one after the other) are called
sequential statements.
Eg:
void main()
{
int a,b,sum;
printf(“enter 2 numbers: “);
scanf(“%d%d”,&a,&b);
sum=a+b;
printf(“\nsum= %d”,sum);
}
ADVANTAGES:
1. No separate control statements are required to execute the sequential statements
one after the other.
2. They are executed in the order they appear in the program.
DISADVANTAGES:
1. The sequence of execution of the program cannot be changed.
CONTROL STRUCTURES
A program is nothing but the execution of sequence of one or more instructions.
Quite often, it is desirable to change the order of execution of statements based on
certain conditions or
This involves a kind of decision making to see whether a particular condition has
occurred or not and direct the computer to execute certain statements accordingly.
Based on application, it is necessary / essential
To alter the flow of a program
Test the logical conditions
Control the flow of execution as per the selection these conditions can be placed in the
program using decision-making statements.
}
Rest of the code;
Firstly, the expression is evaluated to true or false. Only if the expression is true then
true part statements will be executed otherwise false part statements are executed.