Maksks ODULE 2 - POP - Notes
Maksks ODULE 2 - POP - Notes
Operator Description
Example:
#include<stdio.h>
void main(){
int num1=10, num2=3;
printf("Addition Result = %d\n", (num1 + num2));
printf("Subtraction Result = %d\n", (num1 - num2));
printf("Multiplication Result = %d\n", (num1 * num2));
printf("Division Result = %d\n", (num1 / num2));
printf("Remainder = %d\n", (num1 % num2));
}
Output:
Addition Result = 13
Subtraction Result = 7
Multiplication Result = 30
Division Result = 3
Remainder = 1
Arithmetic operators -mixed data types
Arithmetic operators can be used in expressions with operands of different data types.
For example: the addition operator (+) can be used on int and float data types with the result being
a float data type.
If the same is performed on float and double data types, the result will be a double data type, i.e.,
the result will always be of the data type with the highest memory capacity.
Consider the following examples with different combinations of arithmetic operators and data
types.
Operator Description
The table given below demonstrates the use of various relational and equality operators using
variables
int num1 = 7;
float num2 = 5.5;
char ch = 'w';
Example:
#include <stdio.h>
void main() {
int num1 = 7;
float num2 = 5.5;
char ch = 'w';
printf("Result1 = %d\n", (num1 > 5));
printf("Result2 = %d\n", ((num1 + num2) <= 10));
printf("Result3 = %d\n", (ch == 119));
printf("Result4 = %d\n", (ch != 'p'));
printf("Result5 = %d\n", (ch >= 10 * (num1 + num2)));
}
Output:
Result1 = 1
Result2 = 0
Result3 = 1
Result4 = 1
Result5 = 0
3. Logical Operator
Logical operators are used to perform logical operations on the given expressions.
An expression containing a logical operator returns either 0 (or) 1 depending on the evaluation of
the expression to either false or true respectively.
Given below are the three logical operators in C:
logical
&& It returns true when both conditions are true, else, it returns false
AND
logical It returns true when the given expression is false and returns false when
!
NOT the given expression is true
The below table demonstrates the use of various relational and equality operators using variables
int num1 = 7;
float num2 = 5.5;
char ch = 'w';
Example:
#include <stdio.h>
void main() {
int num1 = 7;
float num2 = 5.5;
char ch = 'w';
printf("Result1 = %d\n", ((num1 >= 6) && (ch == 'w')));
printf("Result2 = %d\n", ((num2 < 11) && (num1 > 100)));
printf("Result3 = %d\n", ((ch != 'p') || ((num1 + num2) <= 10)));
printf("Result4 = %d\n", !(num1 > (num2 +1)));
printf("Result5 = %d\n", !(num1 <= 3));
}
Output:
Result1 = 1
Result2 = 0
Result3 = 1
Result4 = 0
Result5 = 1
4. Unary Operators
• no space should be provided between a unary operator and the operand.
• While using a unary minus operator, a - sign precedes a single operand. The result is a
negative value of the operand. It is the same as multiplying the operand by -1.
• While using a unary plus operator, a + sign precedes a single operand. It is the same as
multiplying the operand by the constant 1.
Operator Description
A few examples of unary plus and unary minus are : -125, -a, -(a + b), +5, +x, +(a * b), +0xAB5.
Prefix operators
When the operators ++ or -- appear before the operands, they are called as the prefix operators.
Postfix operators
When the operators ++ or -- appear after the operands, they are called as the postfix operators.
Pre-increment operator
When an increment (++) is used as a prefix (i.e., applied before an operand) for example, as ++i
the value of the operand is changed first. The changed value is then substituted in the expression.
#include<stdio.h>
void main(){
int x =14; //Initialize integer variable x with a value of 14
int y = ++x; // ++x increments x by 1, making x = 15. The incremented value (15) is then. assigned
to y, so y = 15.
printf("x value : %d", x);/ / Print the current value of x, which is now 15
printf("y value : %d", y);// Print the current value of y, which is also 15
++x;// x is incremented by 1 again, making x = 16.
printf("x value : %d", x); // Print the updated value of x, which is now 16
}
Output:
x value : 15
y value : 15
x value : 16
Pre-decrement operator
When an decrement (--) operator is used as a prefix (i.e., applied before an operand) for example,-
-i, the value of the operand is changed first. The changed value is then substituted in the expression.
#include<stdio.h>
void main(){
int x = 14;//Intialize integer variable x with a value of 14
int y = --x; // --x decrements x by 1, making x = 13, the decremented value(13) is then assigned to
y, so y = 13
printf("x value : %d\n", x); //print the current value of x, which is now 13
printf("y value : %d\n", y); //print the current value of y, which is also 13
--x; // x is decremented by 1 again, making x = 12
printf("x value : %d\n", x); // print the updated value of x, which is now 12
}
Output:
x value : 13
y value : 13
x value : 12
Post-increment operator
When an increment (++) operator is used as a postfix (i.e.,applied after an operand) for example,
as, i++ the original value of the operand is first substituted in the expression and then the value
stored in the operand is changed.
#include<stdio.h>
void main(){
int x = 14; //Initialize integer variable x with declare a value 14
int y = x++; // Post-increment x (y is assigned the current value of x, then x is incremented) Here,
y = 14, and then x becomes 15
printf("x value : %d", x); );//print the current value of x, which is now 15
printf("y value : %d", y);//print the current value of y, which is 14 (value of x before incrementing)
x++; // Post-increment x again (x becomes 16)
printf("x value : %d", x); // Print the updated value of x, which is now 16
}
Output:
x value : 15
y value : 14
x value : 16
Post-decrement operator
When an a decrement (--) operator is used as a postfix (i.e.,applied after an operand) for example, i-
-, the original value of the operand is first substituted in the expression and then the value stored in
the operand is changed.
#include<stdio.h>
void main(){
int x = 14; //Initialize integer variable x with declare a value 14
int y = x--; // Post-decrement x (y is assigned the current value of x, then x is decremented) Here, y
= 14, and then x becomes 13
printf("x value : %d\n", x);//print the current value of x, which is now 13
printf("y value : %d\n", y); //print the current value of y,which is now 14
x--; // Post-decrement x again (x becomes 12)
printf("x value : %d\n", x); // Print the updated value of x, which is now 12
}
Output:
x value : 13
y value : 14
x value : 12
5. Assigment operator
LHS = RHS; (or) L-value = R-value;
The LHS (left-hand side) of the = operator has to be a variable. The RHS (right-hand side) can be a
value, a variable or an expression.
For Example:
#include<stdio.h>
void main(){
int x, y, z;
x = 10; // variable x is assigned a value 10
y = 20; // variable y is assigned a value 20
z = x + y; // variable z is assigned the result of the expression x + y
}
Compound Assignment Operators
Compound assignment operators which are a combination of various operators and a single
assignment operator as shown below:
+=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=
Operator Description
& Bitwise AND
| Bitwise OR
~ One's Complement
It sets a bit to 1 if and only if both the corresponding bits in its operands are 1, and to 0 if
the bits differ or both are 0.
The format for using a Bitwise AND (&) operator is:
operand1 & operand2
where operand1 and operand2 should be of integral type.
Example : 1010 & 1100 will result in 1000.
10 ⟹ 1010
&
12 ⟹ 1100
1000 ⟹ 8
2. Bitwise OR operator (|)
It sets a bit to 1 if one or both the bits in its operands are 1, and to 0 if both
The bits are 0.
The format for using a Bitwise OR (|) operator is:
operand1 | operand2
where operand1 and operand2 should be of integral type.
For example, 1010 | 1100 will result in 1110.
10 ⟹ 1010
|
12 ⟹ 1100
1110 ⟹ 14
10 ⟹ 1010
^
12 ⟹ 1100
0110 ⟹ 6
where operand1 should be of integral type and operand2 should also be an integral number
which indicates the number of positions that should be shifted to the left in operand1.
The value of number << bits to be shifted is number left-shifted by bits to be shifted bit
positions. This is equal to multiplying operand1 by 2bitstobeshifted .
Example: 3<<2
Binary value of 3 is 011
3 << 2 will result in 1100
i.e, 3 << 2 = 3 X 22 = 12.
Output:
Bitwise Left shift result = 120
Bitwise Right shift result = 1
Special Operators
Comma Operator
The Comma(,) operator is used to declare multiple expressions in a single statement.
Comma operator acts as a separator between multiple expressions, in declaring multiple
variables and in function call parameters.
In the expression result = (expression1, expression2);
expression1 is first evaluated. After that, expression2 is evaluated and the value of
expression2 is returned for the whole expression.
Example:
#include <stdio.h>
void main() {
int num1, num2, result;
result = (num1 = 5, num2 = num1 + 10, num1 + num2);
printf("Result = %d\n", result);
}
Output:
Result = 20
Sizeof Operator
sizeof operator is also called compile-time operator and when used with an operand, will
return the number of bytes that are occupied by the operand..
Since sizeof returns an unsigned integer, %zu has to be used instead of %d for the format
string.
Example:
#include <stdio.h>
void main() {
float floattype;
double doubletype;
char chartype;
int inttype;
printf("Size of int: %zu bytes\n", sizeof(inttype));
printf("Size of float: %zu bytes\n", sizeof(floattype));
printf("Size of double: %zu bytes\n", sizeof(doubletype));
printf("Size of char: %zu byte\n", sizeof(chartype));
}
Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Precedence
Operator Operation Associativity
level
Functional
() cell(or)
Parentheses
[]
1 Array subscript Left to Right
.
Dot
--->
Arrow
Logical NOT
!
One's
~ compliment
- Unary minus
++ Increment
2 -- Decrement Right to Left
& Address of
* Indirection
(data_type) Cast oparator
sizeof() sizeof special
operator
* Multiplication
3 / Division Left to Right
% Modulus
+ Addition
4 Left to Right
- Subtraction
Less than
<
Greater than
>
6 Less than or Left to Right
<= equal to
>= Greater than or
equal to
== Equal to
7 Left to Right
!= Not equal to
= += -= *=
/= %= Simple and
14 compound Right to Left
>>= <<== assignment
&= ^= |=
Example :
#include <stdio.h>
void main() {
int a = 2 + 3 * 4 % 8 - 5 / 4;
printf("a = %d\n", a);
}
Output:
5
Type Conversions
• Type conversion is performed to convert one or both the operands to an appropriate data
type before evaluation.
• Type conversion means converting one data type value into another data type value.
1. Implicit Conversion
Implicit type conversion, the compiler automatically converts one data type value into another
data type value.
Implicit type conversions can occur during assignment or while using any other operators. during
assignment, the R-value is converted to the type of L-value.
Conversion order
char ⟹ short int ⟹ int ⟹ long ⟹ float ⟹ double ⟹ long double
For example: The statement float a = 5 + 5.6; integer type value 5 is automatically converted into
a float type as 5.0 before the addition is performed.
Example:
#include <stdio.h>
void main() {
int i , intsum;
printf("i = ");
scanf("%d\n",&i);// read value of i
char ch = 'a'; // assing character 'a' to ch
float floatsum;
intsum = i+ch; // add values of i and ch
floatsum = i+ch; // add values i and ch
printf("Integer result = %d, Float result = %f\n",intsum,floatsum); // print intsum and
floatsum values and observe the output
}
Output:
i = 20
Integer result = 117, Float result = 117.000000
i = 115
Integer result = 212, Float result = 212.000000
2. Explicit Conversion
A programmer can instruct the compiler to explicitly convert a value of one type to another using
a typecast operator.
When a typecast operator is used explicitly, the type conversion process is called explicit type
conversion or type casting.
In the expression ((int)num)%2, if num is a float variable with value 5.5, then the expression
evaluates to 1. Given below is an example which demonstrates type casting:
Example:
#include <stdio.h>
void main() {
float x, y;
x = 7 / 5;
y = (float)7 / 5; // Convert the int value explicitly into float
printf("x = %f, y = %f\n", x, y);
}
Output:
x = 1.000000, y = 1.400000
Lesson 2: Conditional Statements
Conditional Statements
A special statement to control the execution of one or more statements depending on a
condition. Such statements are called as control statements or control-flow statements.
There are three types in control-flow statements:
1. Selection Statement - is a statement whose execution results in a choice being made as to
which of two or more paths should be followed.
1. if construct
2. if-else construct
3. if-else-if construct
4. Nested if-else-if construct
5. switch-case construct
2. Iterative / Looping Statement - is a statement which executes a set of statements repeatedly
depending on a condition.
1. while loop
2. do-while loop
3. for loop
3. Unconditional Control Statement - is a statement which transfers control-flow to some
other section of the program without the need of a condition.
1. break statement
2. continue statement
3. goto statement
1. Selection Statement
1. if construct:
The if construct is a selective statement, if statement will execute its block only
when condition evaluates to 1 (true), otherwise the control goes to the first statement
after the if construct.
Syntax:
if(condition) {
statement-1;
statement-2;
....
statement-n;
}
Example:
#include<stdio.h>
int marks, distinction_marks = 75;
printf("Enter marks : ");
scanf("%d", &marks);
if (marks > distinction_marks) {
printf("Secured distinction.\n");
}
Output:
Enter marks : 76
Secured distinction.
2. if-else construct
The if-else statement is a decision-making statement that is used to decide whether
the part of the code will be executed or not based on the specified condition or
expression.
If the given condition is true, then the code inside the if block is executed, otherwise
the code inside the else block is executed.
Syntax:
if (expression) {
statement-1;
} else {
statement-2;
}
Example:
#include<stdio.h>
void main(){
int num1, num2;
printf("Enter 2 numbers : ");
scanf("%d %d",&num1,&num2);
if(num1>num2){
printf("The largest number : %d\n",num1);
}
else{
printf("The largest number : %d\n",num2);
}
}
Output:
Enter 2 numbers : 10 20
The largest number : 20
Enter 2 numbers : 1234 243
The largest number : 1234
3. if-else-if construct
The if-else-if construct is used whenever we have multiple mutually
exclusive if conditions which work on the same input.
In a if-else-if construct the conditions are evaluated from top to bottom. Whenever a
condition evaluates to true (1), the control enters into that if-block and after that the
control comes out of the complete if-else-if construct ignoring all the
remaining if and else constructs that may exist below the currently satisfied if-block.
Syntax:
if (expression_1) {
statement_1;
} else if (expression_2) {
statement_2;
} else if (expression_3) {
statement_3;
} else if (expression_4) {
statement_4;
} else {
statement_5;
}
Example:
#include <stdio.h>
void main() {
int year;
printf("Enter a year : ");
scanf("%d", &year);
if(year % 400==0) {
printf("%d is a leap year\n", year);
} else if(year % 100==0) {
printf("%d is not a leap year\n", year);
} else if(year % 4==0) {
printf("%d is a leap year\n", year);
} else {
printf("%d is not a leap year\n", year);
}
}
Output:
Enter a year : 1900
1900 is not a leap year
Enter a year : 2013
2013 is not a leap year
Syntax:
switch (expression) {
case constant_1 :
statement_1;
break;
case constant_2 :
statement_2;
break;
case constant_3 :
statement_3;
break;
...
...
default :
statement_n;
break;
}
Example:
#include<stdio.h>
void main(){
int weekday;
printf("Enter weekday number (0-6) : ");
scanf("%d",&weekday);
switch(weekday){
case 0:
printf("Sunday\n");
break;
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
default:
printf("Invalid weekday number\n");
}
}
Output:
Enter weekday number (0-6) : 0
Sunday
Enter weekday number (0-6) : 7
Invalid weekday number
Lesson 3: Looping Statements
1. while loop
while loop provide a special construct/statement using which we can repeatedly execute
one or more statement as long as a condition is true.
Syntax :
while (condition) {
statement_1;
statement_2;
....
}
Working
The condition is an expression which should always evaluate to either true or false.
• If it evaluates to true, the body containing one or more code statements is executed.
• If the expression evaluates to false, the control skips executing the while-
loop body.
Example code which uses a while-loop to read multiple numbers from standard input
and prints their sum when the sum exceeds 100.
#include<stdio.h>
void main(){
int number;
int sum = 0;
while(sum <= 100){
printf("Enter a number : ");
scanf("%d",&number);
sum = sum+number;
}
printf("The total of given numbers is : %d\n",sum);
}
2. do-while loop
do-while loop statement is used to execute a section of code atleast once and then
repeatedly execute the same section of code as long as a certain condition is true.
Its syntax is similar to the reverse of while loop as shown below: And note the
extra ; which we have to provide at the end of while(condition)
Syntax:
do {
statement1;
statement2;
....
} while (condition);
Note: The statements inside the do-while block will be executed once before
the condition in the while is evaluated.
The condition is an expression which should always evaluate to either a true or a false value.
• If it evaluates to true, the do-while body containing one or more code statements is
executed again.
• If the expression evaluates to false, the control comes out of the do-while construct.
Example:
#include<stdio.h>
void main(){
int value = 1;
do{
printf("%d ",value);
value++;
}while(value<=5);
}
Output:
12345
3. for loop
for-loop is used to iterate over a range of values using a loop counter, which is a variable
taking a range of values in some orderly sequence (e.g., starting at 0 and ending at 10
2. The loop continues to execute as long as the condition expression evaluates to true.
3. The update expression is executed after each iteration through the loop,
to increment, decrement or change the loop counter.
Example :
#include<stdio.h>
void main(){
int i;
for (i = 1; i < 10; i++) {
printf("%d",i);
}
Output:
123456789
1. Above for loop statement initializes an integer variable i (which is the loop
counter) as part of the initialization expression.
3. The expression in condition is i < 10. The for-loop keeps on executing the code
inside the loop body as long as this condition evaluates to true. And the loop
terminates when the condition evaluates to false.
3. Unconditional Control Statement
1. Break Statement:
Break terminates the execution at that point and transfers execution control to the
statement that immediately follows the loop or the switch-case statement
Example:
#include <stdio.h>
void main() {
int i;
for (i = 1; i < 10; i++) {
if (i % 5 == 0) {
break;
}
printf("i : %d\n",i );
}
}
For example, in the below code the control is terminated only from the inner loop:
#include <stdio.h>
void main() {
int i, j;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (i == j) {
break; // breaks from inner for-loop, if i is equal to j.
}
printf("i : %d, j : %d\n", i, j);
}
}
}
Output:
i : 2, j : 1
i : 3, j : 1
i : 3, j : 2
2. Continue Statement:
When a continue statement is encountered inside a loop, control jumps to the beginning of
the loop for next iteration, skipping the execution of statements inside the body of loop for
the current iteration. Instead of terminating the loop, it forces the next iteration of the loop
to take place.
Example Code:
#include <stdio.h>
void main() {
int i;
for (i=1;i<10;i++) {
if (i % 2 == 0) {
continue;
}
printf("i : %d\n", i);
}
}
Output:
i:5
i:7
i:9
Example 2:
For example, the below code skips printing the values of i and j using the continue;
statement whenever they are same:
#include <stdio.h>
void main() {
int i, j;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (i == j) {
continue; // continues next iteration of the inner for-loop, if i is equal to j.
}
printf("i : %d, j : %d\n", i, j);
}
}
Output:
i : 1, j : 2
i : 1, j : 3
i : 2, j : 1
i : 2, j : 3
i : 3, j : 1
i : 3, j : 2
3. goto Statement
The goto construct causes an unconditional transfer of execution control from the current
location to any other labelled location.
goto Label;
.....
....
Label:
....
....
Example:
#include <stdio.h>
void main() {
char ch;
start:
printf("Enter a character : " );
scanf(" %c", &ch);
printf("The given character is : %c\n", ch);
if (ch != '#') {
goto start;
}
}
Output:
Enter a character : a
The given character is : a
Enter a character : @
The given character is : @