Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

Unit 2 B: Introduction to 'C‘ programming

 Operators and expressions,


 Types of Statements: Assignment,
Control, jumping.
Unit 2 B: Introduction to C
programming
 Operators and expressions,
 Types of Statements: Assignment,
Control, jumping.
Operators and
expressions
• The symbols which are used to perform logical and mathematical operations in a C program
are called 'C' operators.
• These 'C‘ operators join individual constants and variables to form expressions.
• Operators, functions, constants and variables are combined together to form expressions.

• Consider the expression A +


B*5 where, +, * are operators
A, B
are variables or
operand,
5 is constant and
A + B * 5 is an
expression.

[A++…unary
Operators in C programming

Arithmetic Operators

Increment and Decrement Operators

Assignment Operators

Relational Operators

Logical Operators

Conditional Operators

Bitwise Operators

Special Operators
A / B = QUOTIENT

A%B = REMAINDER [% CALLED MODULUS OPERATOR]

9/5 , q=1, R=4


/ / ' C‘ Program to demonstrate the working of arithmetic
operators
#include <stdio.h>
int main()
{
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;
}
Increment:

++a; Preincrement
A++; post increment

Decrement:
--a; Predecrement
A--; postdecrement
A=9;
b=4;

__________________
A+=b; => A = A +b;
A(New/updated) = A(old/previous) + b;
A = 9 + 4;
A=13; // The value of A is now 13, old/previous value 9 is lost..
____________________

___________________
A -= b;……………=> A(New/updated) = A(old/previous) – b; A(New/updated) = 13 – 4;
___________________

____________________
A *=b; A = A * b; A New = A old * b; =9 *4; A=36;
A += b;
A = A +b; // A(New/updated) = A (old/previous) + b;
Find the greater number among two numbers a and b

Int main()
{
Int a, b, c;

If (a > b)………………………….// using the relational operator


……….a is greater than b;
Else
……….a is not greater than b // => b is greater than a;
}
****************

If(a = = c) ……………………….// using the relational operator


a and c are same;

Else
a and c are not same;
/* ‘C’ Program to find Largest of Three numbers using If -Statement */
1. IF 2. IF ELSE 3. ELSE IF LADDER
# include <stdio.h>
int main ( )
{ int a, b, c;

printf( “ Please Enter three different values \n “ );


scanf( “ %d %d %d “ , &a, &b, &c);

if (a > b && a > c)


{ printf("\n%d is Greater Than both %d and %d", a, b, c);
}

if (b > a && b > c)


{ printf("\n%d is Greater Than both %d and %d", b, a, c);
}

if (c > a && c > b)


{ printf("\n%d is Greater Than both %d and %d", c, a, b);
}

return 0;
}
12= 00001 1 0 0
. . . .8 4 2 1
0 0 00 8 4 0 0 = 12

Logical bitwise AND -> 21 & 38 = 4


21 = 0 0 0 1 0 1 0 1

38 = 0 0 1 0 0 1 1 0
----------------------------
= 0 0 00 010 0=4

Logical bitwise OR -> 5 | 7 = 7


5= 000001 0 1

7= 000001 1 1
----------------------------
= 000001 1 1 = 7
• https://fresh2refresh.com/c-programming/c-operators-expressions/c-
bit-wise-operators/
CONDITIONAL OR TERNARY OPERATORS IN C:
This statement will store 3 in y if x is greater than 5, otherwise it will
store 4 in y.
#include<stdio.h> #include<stdio.h>

Void main() Void main()


{ {
int x, y ; int x, y ;
scanf ( "%d", &x ) ; scanf ( "%d", &x ) ;

if ( x > 5 )
y=3; y=(x>5?3:4);

else //expression 1 ? expression 2 : expression 3


y=4; }
}
To find whether a given number is Even/Odd
#include<stdio.h> #include<stdio.h>
Int main( ) int main( )
{ {
int x; int x;
printf("Enter any Number"); printf("Enter any Number");
scanf("%d", &x); scanf("%d", &x);

if (x%2 = = 0)
printf(“Number is EVEN”); (x%2 = = 0 ? printf("Number is EVEN") :
printf("Number is ODD"));
else
printf(“Number is ODD”); // expression 1 ? expression 2 : expression 3

return 0; return 0;
} }
Other
operator
Operator: Hierarchy,
precedence and
Associativity
Precedence and Associativity
A+ B + C +, -

A+ B – C *, /, %

A+ B * C

A+ B * C + d

A/ B* C +d

A* B / C +d
• Associativity is used when two operators of same precedence
appear in an expression. Associativity can be either Left to Right
or Right to Left.

• Precedence and Associativity are two characteristics of operators


that determine the evaluation order of subexpressions in absence of
brackets.
Read from
https://www.geeksforgeeks.org/c-operator-precedence-associativity/
1) Associativity is only used when there are two or more operators of same precedence.

2) All operators with same precedence have same associativity

3) Precedence and associativity of postfix ++ and prefix ++ are different (Precedence of postfix ++ is
more than prefix ++, their associativity is also different. Associativity of postfix ++ is left to right and
associativity of prefix ++ is right to left)

4) Comma has the least precedence among all operators and should be used carefully

5) There is no chaining of comparison operators in C

In Python, expression like “c > b > a” is treated as “a > b and b > c”, but this type of chaining
doesn’t
happen in C.
Unit 2 B: Introduction to 'C'
programming
 Operators and expressions,
 Types of Statements: Assignment,
Control, jumping.
Control statements
enable us to specify the flow of program control; ie, the order in which the instructions in a program must be executed.
They make it possible to make decisions, to perform tasks repeatedly or to jump from one section of code to another.

By default, control flow of program is sequential i.e the order in which they are written in program.

But by using selection and looping constructs of ‘C’ Language we can change sequence of execution of instructions
depending upon some condition (by jumping/skipping some instructions).

C has three types of statement:


Jumping with
1) assignment =

2) selection (branching/decision making) GOTO


a) if (expression) else SWITCH
b) switch case
BREAK
3) Iteration (looping) CONTINUE…
a) while
b) do while
c) For

You might also like