Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views53 pages

PICUNIT3pdf 2025 08 08 07 48 56

Uploaded by

Biplab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views53 pages

PICUNIT3pdf 2025 08 08 07 48 56

Uploaded by

Biplab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Vidhya Rachchh

Programming Practices (Basics of C) [BCA – 1]


UNIT 3 – OPERATORS AND IO STATEMENTS
OPERATORS IN C

C language supports a lot of operators to be used in expressions. These


operators can be categorized into the following major groups

Arithmetic Operators
Relational Operators
Equality Operators
Logical Operators
Unary Operators
Conditional Operators
Bitwise Operators
Assignment Operators
Increment / Decrement
Sizeof Operator
ARITHMETIC OPERATORS

OPERATION OPERATOR SYNTAX COMMENT RESULT

Multiply * a*b result = a * b 27

Divide / a/b result = a / b 3

Addition + a+b result = a + b 12

Subtraction - a-b result = a – b 6

Modulus % a%b result = a % b 0

Value of a=9, b=3


ARITHMETIC OPERATOR EXAMPLE

#include <stdio.h>
void 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);
}
ARITHMETIC OPERATOR EXAMPLE

Output:

a + b = 13
a-b=5
a * b = 36
a/b=2
Remainder when a divided by b=1
ASSIGNMENT OPERATORS

The assignment operator is responsible for assigning values to the variables.


While the equal sign ( = ) is the fundamental assignment operator, C also
supports other assignment operators that provide shorthand ways to represent
common variable assignments. They are shown in the table.
ASSIGNMENT OPERATORS

OPERATOR SYNTAX (Shorthand EQUIVALENT TO


Operator)
/= variable /= expression variable = variable / expression

%= variable %= expression variable = variable % expression

*= variable *= expression variable = variable * expression

+= variable += expression variable = variable + expression

-= variable -= expression variable = variable - expression


ASSIGNMENT OPERATOR EXAMPLE
#include <stdio.h>
void main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
}
ASSIGNMENT OPERATOR EXAMPLE

Output:

c=5
c = 10
c=5
c = 25
c=5
c=0
RELATIONAL OPERATORS

Also known as a comparison operator, it is an operator that compares two values.


Expressions that contain relational operators are called relational expressions.
Relational operators return true or false values, depending on whether the
conditional relationship between the two operands holds or not.
RELATIONAL OPERATORS

OPERATOR MEANING EXAMPLE

< Less Than 3 < 5 Gives 1

> Greater Than 7 > 9 Gives 0

>= Greater Than Or Equal To 100 >= 100 Gives 1

<= Less Than Or Equal To 100 <=50 Gives 0


EQUALITY OPERATORS

C language supports two kinds of equality operators to compare their operands for
strict equality or inequality. They are equal to (==) and not equal to (!=) operator.

The equality operators have lower precedence than the relational operators.

OPERATOR MEANING

== Returns 1 if both operands are equal, otherwise returns 0

!= Returns 1 if both operands are not equal, otherwise returns 0


RELATIONAL & EQUALITY OPERATOR EXAMPLE
#include <stdio.h>
void main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
}
RELATIONAL & EQUALITY OPERATOR EXAMPLE

Output:

5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
LOGICAL OPERATORS

C language supports three logical operators. They are- Logical AND (&&), Logical
OR (||), and Logical NOT (!).

As in the case of arithmetic expressions, the logical expressions are evaluated from
left to right.

A B A&&B A B A || B
A !A
0 0 0 0 0 0
0 1 0 0 1 1 0 1

1 0 0 1 0 1
1 0
1 1 1 1 1 1
LOGICAL OPERATORS EXAMPLE
#include <stdio.h>
void main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", result);
}
LOGICAL OPERATOR EXAMPLE

Output:

(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
CONDITIONAL OPERATORS

The conditional operator (?:) is just like an if .. else statement that can be written
within expressions.

The syntax of the conditional operator

is

exp1? exp2: exp3


CONDITIONAL OPERATORS

Here, exp1 is evaluated first. If it is true then exp2 is evaluated and becomes the result
of the expression, otherwise, exp3 is evaluated and becomes the result of the
expression.

For example,
large = ( a > b) ? a : b

Conditional operators make the program code more compact, more readable, and
safer to use as it is easier both to check and guarantee the arguments that are used
for evaluation.
A conditional operator is also known as a ternary operator as it is neither a unary nor a
binary operator; it takes three operands.
CONDITIONAL OPERATOR EXAMPLE

A = 10;
B = 15;
X = (A > B ) ? A : B;
ANS : (10>15)?
Output : 15 (B)
BITWISE OPERATORS

C provides six bitwise operators for manipulating the individual bits in an integer
quantity.

Bitwise operators expect their operands to be integer quantities and treat them as bit

sequences.

Bitwise negation is a unary operator that complements the bits in its operands.
BITWISE OPERATORS

Operators Meaning Associativity

& Bitwise AND Left to Right

| Bitwise R Left to Right

~ 1’s Complement Left to Right

^ Bitwise Exclusive OR Left to Right

<< Shift left Left to Right

>> Shift right Left to Right


BITWISE OPERATORS

Example: Consider the following set of expressions

int a,b,c;
a = 12;
b = 9;
c = a & b;
// Output is : 8
c = a | b;
// Output is : 13

The bitwise AND operation will be performed at the bit level as follows
BITWISE OPERATORS

The bitwise AND operation will be performed at the bit level as follows :

Expression Bits

a=12 001100

b=9 001001

c=a&b 001000

c=a|b 001101
UNARY OPERATORS
Unary operators act on single operands. C language supports three unary operators.
They are unary minus, increment, and decrement operators.

When an operand is preceded by a minus sign, the unary operator negates its value.

The increment operator is a unary operator that increases the value of its operand by 1.
Similarly, the decrement operator decreases the value of its operand by 1.

For example,

int x = 10, y;

y = x++;

is equivalent to writing

y = x; whereas, y = ++x;

x = x + 1; is equivalent to writing x = x + 1;

y = x;
RULES FOR INCREMENT / DECREMENT OPERATOR

❑ If you use the ++ operator as a prefix like ++var, the value of var is
incremented by 1; then it returns the value.

❑ If you use the ++ operator as a postfix like var++, the original value of var
is returned first; then var is incremented by 1.

❑ The -- operator works in a similar way to the ++ operator except --


decreases the value by 1.
INCREMENT / DECREMENT OPERATOR

M = 5;
Y = ++M;

Output:
Y=6
M=6

M = 5;
Y = M++;

Output:
Y=5
M=6
INCREMENT / DECREMENT OPERATOR EXAMPLE

#include <stdio.h>
void main()
{
int x = 10, y = 20,p;
int p = ++x + ++y;
printf("%d\n", x);
printf("%d\n", y);
printf("%d\n", p);
}
INCREMENT / DECREMENT OPERATOR EXAMPLE

Output:

11
21
32
INCREMENT / DECREMENT OPERATOR EXAMPLE

#include <stdio.h>
void main()
{
int var1 = 5, var2 = 5;

// 5 is displayed
// Then, var1 is increased to 6.
printf("%d\n", var1++);

// var2 is increased to 6
// Then, it is displayed.
printf("%d\n", ++var2);
}
INCREMENT / DECREMENT OPERATOR EXAMPLE

Output:

5
6
COMMA OPERATORS

The comma operator in C takes two operands.

Comma-separated operands when chained together are evaluated in left-to-


right sequence with the right-most expression as the value of the combined
expression.
Among all the operators, the comma operator has the lowest precedence.

For example,

Value = (x = 10, y=5, x+y);


First assigns the value 10 to x, then assigns 5 to y, and finally assigns 15 (10+5) to
the value. Since the comma operator has the lowest precedence of all
operators, the parenthesis is necessary.
SIZEOF OPERATORS

size of is a unary operator used to calculate the sizes (Bytes) of data types.
It can be applied to all data types.

The operator returns the size of the variable, data type, or expression in bytes.
The 'sizeof' operator is used to determine the amount of memory space that the
variable/expression/data type will take.
For example,

sizeof(char) returns 1, which is the size of a character data type.

The sizeof(variable)operator computes the size of a variable. And, to print


the result returned by sizeof, we use either %lu or %zu format specifier.
SIZEOF OPERATORS EXAMPLE

#include<stdio.h>
void main()

{
int intType;
float floatType;
double doubleType;
char charType;

// sizeof evaluates the size of a variable


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));
}
SIZEOF OPERATORS EXAMPLE

Output:

Size of int: 4 bytes


Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
ASSOCIATIVITY OF OPERATORS

If two operators of the same precedence (priority) are present in an


expression, the Associativity of operators indicates the order in which they
execute.

9 – 12 / 3 + 3 * 2 – 1
(1) (2)
9– 4 + 6- 1
(3)
5 + 6 -1
(4)
11 -1
(5)
10
ANS: 10
ASSOCIATIVITY OF OPERATORS

Arithmetic Operator: Left to Right


Assignment Operator: Right to Left
Unary Operator: Right to Left
Relational Operator: Left to Right
Equality Operator: Left to Right
ASSOCIATIVITY OF OPERATORS
ASSOCIATIVITY OF OPERATORS
TYPE CONVERSION AND TYPE CASTING

Type conversion and type casting of variables refer to changing a variable of


one data type into another.

While type conversion is done implicitly, casting has to be done explicitly by


the programmer. We will discuss both of them here.

Type conversion is done when the expression has variables of different data
types. So to evaluate the expression, the data type is promoted from a lower to a
higher level where the hierarchy of data types can be given as double, float, long,
int, or char.
TYPE CONVERSION AND TYPE CASTING

For example, type conversion is automatically done when we assign an integer


value to a floating point variable.

For example,

float x;
int y = 3;
x = y;
Now, x = 3.0,
TYPE CONVERSION AND TYPE CASTING

Type casting is also known as forced conversion.

It is done when the value of a higher data type has to be converted into the
value of a lower data type. For example, we need to explicitly typecast an integer
variable into a floating point variable.

float salary = 10000.00;


int sal;
sal = (int) salary;

Typecasting can be done by placing the destination data type in parentheses


followed by the variable name that has to be converted.
TYPE CONVERSION HIERARCHY
FORMATTING INPUT / OUTPUT

C language supports two formatting functions

printf()
scanf()
PRINTF()

The printf function is used to display information required to the user and also
prints the values of the variables. Its syntax can be given as

printf (“control string”, variable list);

The parameter control string is a C string that contains the text that has to be written
onto the standard output device.
PRINTF()

specifier Qualifying Input

c For single character

d For decimal values

f For floating point numbers

E, e Floating point numbers in exponential format

o For Octal number.

s For a sequence of (string of) characters

u For Unsigned decimal value

x For Hexadecimal value.


PRINTF()

length Description
When the argument is a short int or unsigned short int.
h short int i = 3; printf( "%hd", i );

When the argument is a long int or unsigned long int for


l
integer specifiers.
long int i=200000; printf(“%ld”,i);

When the argument is a long double (used for floating point


specifiers)
L long double i=200000; printf(“%Lf”,i);
SCANF()

The scanf() is used to read formatted data from the keyboard. The syntax of the
scanf() can be given as,

scanf(“control string”, &arg1, &arg2, ……&argn);

The control string specifies the type and format of the data that has to be obtained
from the keyboard and stored in the memory locations pointed by the arguments arg1,
arg2,…, argn.
SCANF()

The prototype of the control string can be given as:

[%[Format Specifier][&]Variable Name];

scanf(“%d”,&a);

Note:
Width is an optional argument that specifies the maximum number of characters to be read.
SCANF()

specifier Qualifying Input

c For single character

d For decimal values

f For floating point numbers

E, e Floating point numbers in exponential format

o For Octal number.

s For a sequence of (string of) characters

u For Unsigned decimal value

x For Hexadecimal value.


SCANF()

#include<stdio.h>
#include<conio.h>

void main()
{
int i;
clrscr();
printf(“\n Enter Value of i :”);
scanf(“%d”,&i);
printf(“\n The Entered Value of i is : %d”,i);
getch();
}
THANK
YOU

MOTIVATION GETS
YOU STARTED,
DISCIPLINE KEEPS
YOU GOING.

You might also like