0% found this document useful (0 votes)
12 views13 pages

Lect 2

The document provides an overview of input and output functions in C, specifically focusing on printf() for output and scanf() for input. It also covers various types of operators in C, including arithmetic, relational, logical, and assignment operators, with examples for each type. The document explains the syntax and usage of these operators through code snippets and their outputs.

Uploaded by

malakmahamad376
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)
12 views13 pages

Lect 2

The document provides an overview of input and output functions in C, specifically focusing on printf() for output and scanf() for input. It also covers various types of operators in C, including arithmetic, relational, logical, and assignment operators, with examples for each type. The document explains the syntax and usage of these operators through code snippets and their outputs.

Uploaded by

malakmahamad376
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/ 13

Lect2

C input and output

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() {

// Prints some text


printf("First Print");

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);

// Prints the age


printf("Age is: %d\n", age);
return 0;
}
Output:
Enter your age: 25 (Entered by the user)
Age is: 25
Explanation: %d is used to read an integer; and &age provides the
address of the variable where the input will be stored.

Operators in C

operator in C can be defined as the symbol that helps us to perform some


specific mathematical, relational, bitwise, conditional, or logical
computations on values and variables. The values and variables used with
operators are called operands. So we can say that the operators are the
symbols that perform operations on operands.

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

The arithmetic operators are used to perform arithmetic operations on


operands. There are 9 arithmetic operators in C language:

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

Flips the sign


Unary
– of the value. -a
Minus

Increases the
++ Increment value of the a++
operand by 1.

Decreases the
— Decrement value of the a–
operand by 1.

Example of C Arithmetic Operators


// C program to illustrate the arithmatic operators
#include <stdio.h>

Int main()
{

int a = 25, b = 5;

// using operators and printing results


printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);

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.

Example of C Relational Operators


// C program to illustrate the relational operators
#include <stdio.h>
7
int main()
{

int a = 25, b = 5;

// using operators and printing results


printf("a < b : %d\n", a < b);
printf("a > b : %d\n", a > b);
printf("a <= b: %d\n", a <= b);
printf("a >= b: %d\n", a >= b);
printf("a == b: %d\n", a == b);
printf("a != b : %d\n", a != b);

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

Logical Operators are used to combine two or more


conditions/constraints or to complement the evaluation of the original
condition in consideration. The result of the operation of a logical
operator is a Boolean value either true or false.

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

Example of Logical Operators in C


// C program to illustrate the logical operators
#include <stdio.h>

int main()
{

int a = 25, b = 5;

// using operators and printing results


printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);

return 0;
}

Output

9
a && b : 1
a || b : 1
!a: 0
Code Analysis

1. Logical AND (&&):

o a && b: Evaluates to true (or 1 in C) if both operands are


non-zero.

o Here, a = 25 and b = 5, both are non-zero, so the result is 1.

2. Logical OR (||):

o a || b: Evaluates to true (or 1 in C) if at least one operand is


non-zero.

o Since both a and b are non-zero, the result is 1.

3. Logical NOT (!):

o !a: Evaluates to true (or 1 in C) if the operand is 0.


Otherwise, it evaluates to false (or 0 in C).

o Here, a = 25 is non-zero, so !a results in 0.

Assignment Operators in C

Assignment operators are used to assign value to a variable.. The value


on the right side must be of the same data type as the variable on the left
side otherwise the compiler will raise an error.

Operator Description
Symbol Syntax

Assign the
value of the
Simple
= right operand a=b
Assignment
to the left
operand.

10
Operator Description
Symbol Syntax

Add the right


operand and
left operand
Plus and
+= and assign a += b
assign
this value to
the left
operand.

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.

Divide the left


operand with
the right
Divide 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.

Example of C Assignment Operators


// C program to illustrate the assignment operators
#include <stdio.h>

int main()
{
int a = 25, b = 5;

// using operators and printing results


printf("a = b: %d\n", a = b);
printf("a += b: %d\n", a += b);
printf("a -= b: %d\n", a -= b);
printf("a *= b: %d\n", a *= b);
printf("a /= b: %d\n", a /= b);
printf("a %%= b: %d\n", a %= b);

return 0;
}

Output

a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
12
13

You might also like