Class: X - M Subject: Computer Science
Unit # 02
User Interaction
What is the relationship between a computer and programming language?
A computer is a device that takes data as input, processes that data and generates the output. Thus, all the
programming languages must provide instructions to handle input, output and processing of data.
What are input and output functions?
Input functions: The data or instructions given to a program are called input. In programming, input means to
enter or feed data in a computer program. C language provides the scanf() function to get input from the user.
Output functions: In computer programming, output means to display information on screen or print on printer.
C language provides the printf() function to output information on a computer screen.
What is the purpose of printf() function? Or explain printf() with example.
printf() is a built-in function in C programming language to show output on screen. Its name comes from "print
formatted" that is used to print the formatted output on screen.
Example:
#include<stdio.h>
void main()
{
printf(“Hello World”);
}
Output:
Hello World
In the above example printf() function is used to display Hello World on the screen. Whatever we write inside the
double qoutes and in the printf() function, gets display on screen.
Why are format specifiers used?
Format specifier is used to specify the format of data type during input and output operations. Format specifier is
always preceded by percentage (%) sign. The following table shows format specifiers against different data types in
C language.
Data Type Format Specifier
int %d or %i
float %f
char %c
1
Which format specifier is used for integer?
If we want to show int type data, we must specify it inside the printf() by using the format specifier %d or %i.
Example:
#include<stdio.h>
void main()
{
int age = 35;
printf(“age is %d”, age);
}
Output:
35
Note: While displaying output, the format specifier is replaced with the value of variable/data after the ending
quotation mark i.e age.
Which format specifier is used for float type data? Or how do we specify the number of digits after a
decimal point in floating-point format specifiers?
If we want to show float type data, we must specify it inside the printf by using the format specifier %f. When we use
%f to display a float value, it displays 6 digits after the decimal point. If we want to specify the number of digits
after decimal point then we can write %.nf where n is the number of digits.
Example:
#include<stdio.h>
void main()
{
float height = 5.8;
printf("height is %f", height);
printf("\nheight is %.2f", height);
}
Output:
height is 5.800000
height is 5.80
Which format specifier is used for character type?
The format specifier %c is used to read and print character type data.
Example:
#include<stdio.h>
void main()
{
char grade = “A”;
printf(“The grade is %c”, grade);
}
Output: The grade is A
2
Are format specifiers used for variables only? Justify your answer with example. Or define with
example how does format specifier display result of any expression.
No, format specifiers are not only used for variables. They are used to display the result of any expression involving
variables, constants, or both.
Example:
#include<stdio.h>
void main()
{
printf(“Sum of 3 and 2 is %d”, 3 + 2);
}
Output:
Sum of 3 and 2 is 5
What is the purpose of scanf() function? Or what is the input function? Explain scanf() function with
example.
scanf() is a built-in function in C language that takes input from user into the variables. We specify the expected
input data type in scanf function with the help of format specifier.
Syntax: scanf(“format specifiers”,&var1, &var2, &var3,…);
There are two main parts of scanf() function that are listed below.
i. Format specifiers:
The first part is written inside double quotes is the list of format specifiers. If user enters integer data type,
format specifier mentioned in scanf() must be %d or %i, for float used %f and for character used %c.
ii. Address operator (&):
The symbol & is called address operator. It is used before the name of variable to specify the memory address
of variable where data is to be initialized.
What happens if '&' symbol is not used with scanf() function?
It is very common mistake to forget “&” sign in the scanf(). Without “&” sign, the program gets executed but
does not behave as expected.
Example:
#include<stdio.h>
void main()
{
int age;
printf(“Enter age:”);
scanf(“%d”, &age);
printf(“Age is %d”, age);
}
Output:
Enter age: 35
Age is 35
3
Is it possible to take multiple inputs using a single scanf() function?
we can take multiple inputs using a single scanf()
Example: scanf(“%d%d%f”,&a,&b,&c);
It takes input into two integer type variable a and b, and one float type variable c.
What is the purpose of getch() Function?
getch() function is used to read a single character from user. The character entered by user does not get displayed
on screen. To use this function, we need to include the library conio.h in the header section of program.
Example:
#include<stdio.h>
Void main()
{
printf(“Please enter any key to exit program”);
getch();
}
Output:
Please enter any key to exit program
Note: In the above program prompts user to enter a character and then waits for the user’s input before finishing the
execution of program.
When we read character through getch(), it does not wait for enter key to be pressed. Function reads a character and
proceeds to the execution of next line, whereas in scanf() requires us to press enter key for further execution.
Define statement terminator.
Statement terminator is an identifier for compiler which identifies end of line. In C language semi colon (;) is used
as terminator. If we do not end each statement with a ; it results in error.
Example:
prinf(“Hello World”);
What is the purpose of escape sequences with printf function? Or what is escape character? Describe an
escape sequence used for a new line/ any two escape sequence with example.
Escape sequences are used in printf() function inside double quotation (“”). They force printf() to change its normal
behavior of showing output. These characters are not printed. Escape sequences consist of two characters. The first
character is always back slash (\) and the second character varies according to the functionality that we want to
achieve.
Example: \n,\t etc.
Sequence Purpose Sequence Purpose
\a Generates an alert (bell) sound \v Creates vertical space
\b Removes previous one character \\ Display back slash (\)
\n Moves cursor to start of the new line \’ Display single quote ()
\t creates horizontal space \” Display double quotation mark (“”)
4
What is the purpose of the new line (\n) escape character?
After escape character (\), n specifies movement of the cursor to start of next line. This escape sequence is used to
print the output on multiple lines.
Example:
printf(" My name is zulfiqar ali\n I live in Lahore ");
Output:
My name is Zulfiqar ali
I live in Lahore
What is the purpose of tab (\t)?
Escape sequence \t specifies the I/O function of moving to the next tab stop horizontally. A tab stop is collection of 8
spaces. Using \t takes cursor to the next tab stop. This escape sequence is used when user presents data with more
spaces
Example:
printf("Name:\tzulfiqar);
Output:
Name: zulfiqar
Define operator and its need.
An operator is a symbol used to command the computer to perform a certain mathematical or logical operation on
data. We need to perform computations on data through programming. We can also perform mathematical operations
in our programs. C language offers numerous operators to manipulate and process data that is listed below,
● Assignment operator
● Arithmetic operators
● Logical operators
● Relational operators
What is an assignment operator?
Assignment operator is used to assign a value to a variable or assign a value of variable to another variable.
Equal sign (=) is used as an assignment operator in C.
Example:
int age = 35; (Value 35 is assigned to a variable named age after executing this line code.)
What are arithmetic operators? Or what is the purpose of arithmetic operator?
Arithmetic operators that are used to perform arithmetic operations on numeric values are known as arithmetic
operators.
Examples: Division(/), Multiplication(*), Addition(+), Subtraction(-) and Modulus operator(%).
Define division operator.
Division operator (/) is a binary operator that divides the value of the left operand by the value of right operand.
5
Example:
float result = 3.0/2.0;
After the statement, the variable result contains the value 1.5.
Important Note: If both operand are of type int, then the result of division is also type of int. Remainder is
truncated.
So, answer is 1, and this 1 is assigned to variable of type float, then this value 1 is converted to float, so value 1.0
stored in variable result. If we want to get a precise answer then one of the operand must be floating type.
float result = 3.0/2;
in the above example, the value stored in variable result is 1.5
Define multiplication operator.
Multiplication operator (*) is a binary operator which performs the product of two numbers.
Example: int mul = 5 * 5;
After execution of the statement, the variable mul is initialized with the value of 25.
Define addition operator.
Addition operator (+) calculates the sum of two or more operands.
Example: int sum = 10+10;
After execution of statement, the variable sum is initialized with the value of 20.
What is the increment operator?
In C language the statement a = a + 1; is used to increase the value of variable a by 1. It can also be written as a++ or
++a. It is also called an increment operator.
Define subtraction operator.
Subtraction operator (-) subtracts right operand from the left operand.
Example: int sub = 20 - 10;
After execution of the statement, the variable sub is initialized with the value of 10.
What is a decrement operator?
In language C the statement a = a - 1; is used to decrease the value of variable a by 1. It can also be written as a-- or
--a. It is also called a decrement operator.
What is the purpose of modulus operator?
Modulus operator (%) performs division of left operand by the right operand and returns the remainder value after
division. Modulus operator works on integer data types.
Example: int rem = 17% 2;
As, when we divide 17 by 2, we get a remainder of 1, so the value stored in variable rem is 1.
6
#include<stdio.h>
void main()
{
int x,y,sum,sub,mul,div,rem;
x=4;
y=2;
sum = x+y;
sub=x-y;
mul=x*y;
div=x/y;
rem=x%y;
printf(“Sum is %d\n”, sum);
printf(“Subtract is %d\n”, sub);
printf(“Multiplication is %d\n”, mul);
printf(“Division is %d\n”, div);
printf(“Remainder is %d\n”, rem);
}
Output:
Sum is 6
Subtraction is 2
Multiplication is 8
Division is 2
Remainder is 0
What are relational operators?
Relational operators compare two values to determine the relationship between values. Relational operators identify
either the values are equal, not equal, greater than or less than one another. C language allows us to perform relational
operations on numeric and char type data.
Relational Operator Description
== Equal to
!= Not equal
> Greater than
< Less than
>= Greater than equal to
<= Less than equal to
Relational operators perform operations on two operands and return the result in Boolean expression (true or false). A
true value is represented by 1, whereas false value is represented by 0.
7
What is the difference between assignment operator and equal to operator?
Assignment operator (=):
The assignment operator is used to assign the value or expression on the right side to the variable on the left side.
Example:
int x = 2;
int z = x + y;
Equal to operator (==):
In C language, == is used to check whether right and left operands are equal or not. We can also use printf() to show
the result of a relational expression.
Example:
a ==1
printf(“%d”, 5==5); //This statement displays 1
What are logical operators? Or explain the logical operators with the help of truth table.
Logical operators are used to perform logical operations on the given Boolean expression and produce a Boolean
expression as a result. Logical operators can be performed to evaluate more than one relational expression. There are
three types of logical operators.
Operator Description
&& AND
|| OR
! NOT
What is AND logical operator (&&)?
AND operator (&&) takes two Boolean expressions as operands and produces the result true if both of its operands
are true. It returns false if any of the operands is false.
Truth Table:
Expresion Results
False && False False
False && True False
True && False False
True && True True
8
What is OR logical operator (||)?
OR operator (||) accepts Boolean expression and returns true if at least one of the operands is true. It is considered
false only if both of the operands are false.
Truth table:
Expression Result
False || False False
True || False True
False || True True
True || True True
What is NOT operator (!)?
NOT operator (!) negates or reverses the value of Boolean expression. It makes it true, if it is false and false if it is
true.
Truth table:
Expression Result
!(True) False
!(False) True
What is short circuiting?
C language performs short - circuit evaluation. It means that:
1. While evaluating an AND operator, if sub expression at left side of the operator is false then the results is
immediately declared as false without evaluating complete expression.
2. While evaluating an OR operator, if sub expression at left side of the operator is true then the results is
immediately declared as true without evaluating complete expression.
How many types of operators are based on the number of operand? Explain
There are three types of operators.
i. Unary operator
ii. Binary operators
iii. Ternary operators
9
What is the difference between unary and binary operators?
Unary Operators: Unary operators are applied over one operand only.
Example: logical not (!) operator has only one operand. Sign operator (-) is another example of a unary operator e.g -
5.
Binary Operator: Binary operators require two operands to perform the operation.
Example: All the arithmetic operators and relational operators are binary operators. The logical operators && and ||
are also binary operators.
What is a ternary operator?
Ternary operators require three operands to perform the operation.
What is meant by the precedence of operators? Which operator has the highest precedence in C
language? What do you know about operator precedence?
A single expression in C language may have multiple operators of different types. The C language compiler evaluates
its value based on the operator precedence and associativity of operators. A precedence has been given to each
operator. An operator with higher precedence is evaluated before the operator with lower precedence. In case of equal
precedence, the operator at left side is evaluated before the operator at right side. The precedence of operators is given
below.
Precedence Operator
1 ()
2 !
3 *, /, %
4 +,-
5 >, <, >=, <=
6 == , !=
7 &&
8 ||
9 =
Example:
7 + 3 * (12 + 2)
= 7 + 3*14 //evaluate () first
= 7 + 42 // evaluate *
= 49 // evaluate +
10