Lect 2
Lect 2
in C, there are many functions used for input and output in different
situations but the most commonly used functions for Input/Output are
scanf() and printf() respectively.
Standard Output Function – printf()
Syntax
printf(―formatted_string‖, variables/values);
Example:
#include <stdio.h>
int main() {
return 0;
}
Output
First Print
#include <stdio.h>
int main() {
int age = 22;
// Prints Age
printf("Age: %d\n", age);
return 0;
}
Output
Age: 22
Here, the value of variable age is printed. You may have noticed %d in
the formatted string. It is actually called format specifier which are used
as placeholders for the value in the formatted string.
1
You also noticed ‘\n’ character. This character is an escape sequence
and is used to enter a newline.
Standard Input Function – scanf()
Scanf () is used to read user input from the console. It takes the format
string and the addresses of the variables where the input will be stored.
Syntax
printf(―formatted_string‖, address_of_variables/values);
Remember that this function takes the address of the arguments where
the read value is to be stored.
Example
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
// Reads an integer
scanf("%d", &age);
Operators in C
2
For example,
c = a + b;
Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’
are operands. The addition operator tells the compiler to add both of the
operands ‘a’ and ‘b’. To dive deeper into how operators are used with
data structures.
Types of Operators in C
C language provides a wide range of operators that can be classified into
6 types based on their functionality:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
1. Arithmetic Operations in C
3
Operator Description
Symbol Syntax
Adds two
+ Plus numeric a+b
values.
Subtracts right
– Minus operand from a–b
left operand.
Multiply two
* Multiply numeric a*b
values.
Divide two
/ Divide numeric a/b
values.
Returns the
remainder
after diving
% Modulus the left a%b
operand with
the right
operand.
Used to
specify the
+ Unary Plus positive +a
values.
4
Operator Description
Symbol Syntax
Increases the
++ Increment value of the a++
operand by 1.
Decreases the
— Decrement value of the a–
operand by 1.
Int main()
{
int a = 25, b = 5;
return 0;
}
5
Output
a + b = 30
a - b = 20
a * b = 125
a/b=5
a%b=0
+a = 25
-a = -25
a++ = 26
a-- = 24
2. Relational Operators in C
The relational operators in C are used for the comparison of the two
operands. All these operators are binary operators that return true or
false values as the result of comparison.
These are a total of 6 relational operators in C:
Operator Description
Symbol Syntax
Returns true if
the left
operand is less
< Less than a<b
than the right
operand. Else
false
Returns true if
the left
Greater operand is
> a>b
than greater than
the right
operand. Else
6
Operator Description
Symbol Syntax
false
Returns true if
the left
operand is less
Less than or
<= than or equal a <= b
equal to
to the right
operand. Else
false
Returns true if
the left
Greater operand is
>= than or greater than or a >= b
equal to equal to right
operand. Else
false
Returns true if
both the
== Equal to a == b
operands are
equal.
Returns true if
both the
!= Not equal to a != b
operands are
NOT equal.
int a = 25, b = 5;
return 0;
}
Output
a<b :0
a>b :1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
Here, 0 means false and 1 means true.
3. Logical Operator in C
8
Operator Description
S. No. Symbol Syntax
Returns true
Logical if both the
&& AND operands are a && b
true.
1
Returns true
if both or any
|| Logical OR of the a || b
operand is
true.
2
Returns true
Logical
! if the operand !a
NOT
is false.
3
int main()
{
int a = 25, b = 5;
return 0;
}
Output
9
a && b : 1
a || b : 1
!a: 0
Code Analysis
2. Logical OR (||):
Assignment Operators in C
Operator Description
Symbol Syntax
Assign the
value of the
Simple
= right operand a=b
Assignment
to the left
operand.
10
Operator Description
Symbol Syntax
Subtract the
right operand
and left
Minus and
-= operand and a -= b
assign
assign this
value to the
left operand.
Multiply the
right operand
and left
Multiply and
*= operand and a *= b
assign
assign this
value to the
left operand.
11
Operator Description
Symbol Syntax
Assign the
remainder in
the division of
Modulus and
%= left operand a %= b
assign
with the right
operand to the
left operand.
int main()
{
int a = 25, b = 5;
return 0;
}
Output
a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
12
13