Professional Documents
Culture Documents
Unit 2 B: Introduction To 'C Programming
Unit 2 B: Introduction To 'C Programming
[A++…unary
Operators in C programming
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Conditional Operators
Bitwise Operators
Special Operators
A / B = QUOTIENT
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;
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;
return 0;
}
12= 00001 1 0 0
. . . .8 4 2 1
0 0 00 8 4 0 0 = 12
38 = 0 0 1 0 0 1 1 0
----------------------------
= 0 0 00 010 0=4
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>
if ( x > 5 )
y=3; y=(x>5?3:4);
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.
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
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).