Ch-10 (ICS II) - Input Output
Ch-10 (ICS II) - Input Output
Chapter 10
Input/Output
Contents
• Standard Input & Standard Output
• The printf() Function
• Format Specifier
• Field-Width Specifier
• Displaying Output Values Left-Justified
• Displaying Character Type Data
• String Variable
• Escape Sequence
• The scanf() Function
• Character Input
• The gotoxy() Function
•
Standard Input & Standard
Output
Q.1 What do you mean by standard input and standard output? How is standard
input/output performed in C?
Standard Input
• Anything given to the computer is called input. The input is mostly given to computer
through keyboard. Keyboard is called the standard input device. The input given to the
computer through keyboard is called standard input.
• Data is given to the computer program as input for processing. Input can be given to the
program through keyboard during program execution. C-language provides various
standard input library functions. These functions are used to get input from user through
keyboard during execution of program. The important and commonly used standard
input functions in C language are scanf(), gets(), getch() and getche().
Standard Output
• The processed data is called output. The program gets input data, processes it and gives
result as output. The output is mostly displayed on the display screen (monitor). The
display screen is the standard output device. The output displayed on the display screen
is called standard output.
• C-language also provides various standard output library functions. These functions are
used to display the output of program on the display screen. The important and
commonly used standard output functions in C language are printf() and puts().
• The standard input and output functions are defined in the header file “stdio.h” (stdio
stands for standard input and output). This header file must be included in the program
if any standard input/output function is used in the program.
The printf() Function
Q.2 Illustrate the use of printf() function with examples.
The printf() Function
• The printf() function is used to display the output on monitor in a specified format. It is
also called the formatted output function. It is most commonly used in C programs to
display the output. This function is defined in the “stdio.h” header file.
• Syntax:
• format_string:The format_string is written within double quotes. It is also called control string. It may
consist of the following:
Text: It specifies the message that is to be displayed along with the data values. Its use is
optional.
Format Specifier: It specifies the format according to which a value of variable is to be
displayed.
Escape Sequences: These are special characters that are used to control the output displayed on
the monitor.
• Arguments: It indicates the list of variables, or constants, or expressions, or combination of these,
whose data is to be displayed. They are written separated by commas. The values of the arguments
are displayed according to the corresponding format specifiers given in the format_string. For
example, the value of first argument is displayed in place of first format specifier. The value of
second argument is displayed in place of second format specifier and so on. The use of arguments is
The printf() Function
• Examples:
• printf(“Programming with C”);
• printf(“Computer\nScience”);
• printf(“Total Marks = %d”, marks);
Format Specifier
Q.3 What is format specifier? Describe different format specifiers used in C
language.
Format Specifier
• The format specifier is used in printf() function to specify the format
according to which a data value is to be displayed / printed on the output
device. It is also used in scanf() function to specify the format according to
which a value is to be read from an input device. The format specifiers are
written starting (or prefix) with % symbol.
• The format specifier must be used if a variable name is given in the printf()
or scanf() function as argument. If multiple variables are used as
arguments then format specifiers for each variable must be specified.
Otherwise compiler will display an error message. In printf() function,
format specifiers can be used before the text message, between the text
message or after the text message.
• Integer format specifiers
• Character format specifiers
• Floating-Point format specifiers
Format Specifier
• Integer Format Specifiers
Format Specifier
• Character Format Specifiers
• A string variable can also be declared and initialized with a string without
specifying its length. For example, above statement can be written as:
char str[] = “PAKISTAN’;
• In the above statement, the string variable ‘str’ is declared and “PAKISTAN” is
assigned to it. The Null character (\0) will be added at the end of the string.
String Variable
• String Input and Output
• The gets() function and scanf() function can be used for input data into a
string variable. Mostly, the gets() function is used for this purpose.
Similarly, puts() function and printf() function are used to display the data
of string variable on the screen. The format specifier “%s” with formatted
input and output functions is used for input data into string and to display
its data respectively.
• For example, to display the contents of “str” variable, the statement is
written as: printf(“%s”, str);
String Assignment
• Assigning a value to string variable is different than assigning values to other variables. The
values are assigned to other variables using assignment statement. The assignment statement
cannot be used to assign value to string variable. It is invalid statement and compiler will generate
an error: name = “I Love Pakistan”;
• The string variable “name” does not represent to a single memory location. It is an array. An
array consists of consecutive group of memory locations with the same name and data type. So
different characters in different memory locations will be stored. One character of the string will
be stored in one memory location.
• C language provides built-in functions for processing strings. These functions are defined in
header file “string.h”. The string function strcpy() is used to copy or assign a string to a string
variable.
• The general syntax this function is: strcpy(strvar, string);
• strvar It indicates the string variable in which the string is to be copied/assigned.
• string It indicates the string to be copied/assigned.
• For example to assign a string “I Love Pakistan” to string variable “name”, the statement is
written as: strcpy(name, “I Love Pakistan”)
Escape Sequence
Q.11 What are escape sequences? Discuss different escape sequences in C.
Escape Sequence
• The special characters that can be used in format-string in printf() function to control
the displaying behavior of the “printf()” function, are called escape sequences. An
escape sequence prefixed with a backslash (\) and a code character is used to control
the displaying behavior. The backslash (\) is called an escape character. The escape
sequence looks like two characters.
• The escape sequences are special non-printing characters. It means that escape
sequences are not printed with string.
• The escape sequence can be inserted in any position of the format string such as:
• at the beginning of the string.
• at the middle of the string.
• at the end of the string etc.
• For example, the escape sequence ‘\n’ is used to insert a new line. The cursor moves
from current position on the output device to the beginning of the next line. If escape
sequence ‘\n’ is inserted at the beginning of the string then the string will be displayed
after printing a blank line e.g. printf(”\nWelcome”);
Escape Sequence
The scanf() Function
Q.12 Explain the use of scanf() function with examples.
The scanf() Function
• The scanf() function is used to get input from user through keyboard during
execution of program. The value is input into a variable in a specified format.
This function can be used to get input into numeric, character and string
type variables. This function is defined in the “stdio.h” header file.
• The general syntax of scanf() function is: scanf (format_string, variables);
• format_string It indicates the format specifiers. It is written within double quotes.
The format specifiers are same as mentioned with printf() function. The format_string
used in scanf() function is slightly different from the format_string used in printf()
function. In scanf() function, the text message inside the format_string cannot be
written. Only the format specifiers are given in place of format string.
• Variables It indicates the list of variables in which entered values are
stored. These variables are written separated by commas. An & (ampersand) sign is
given before each variable (except string type variable). The & is called address
operator. It indicates the address of the variable in memory where the input value is
to store.
The scanf() Function
• Examples:
• scanf(“%f”, &x);
• scanf(“%c %d %d”, &ch, &a, &b);
• A proper message should be displayed using
printf() function before the input statement.
printf(“Enter your age in years ?”);
scanf(“%d”, &year);
The scanf() Function
• The clrscr() Function
• The clrscr() function is used to clear the screen of the monitor. After
executing this function, screen cursor moves to the top-left corner of the
screen. This function is defined in “conio.h” header file.
• The syntax to call this function is: clrscr();
• Following program clears the screen of the computer monitor.
#include<conio.h>
main()
{
clrscr();
}
Character Input
Q.14 What is character input? Which functions are used for character input?
OR Describe the getch() and getche() functions.
Character Input
• The process to enter characters into computer is called character input. The
scanf() function can be used for character input. However, C language
provides functions that are specially used for characters input. The important
character input functions are getch() and getche(). Both these functions are
defined in header file “conio.h”.
• The getch() Function
• The word “getch” stands for get character. This function is used to get (input)
a single character from keyboard during execution of program. The entered
character is not displayed on the screen. Similarly, the input process is
automatically completed without pressing the Enter key. This function is
normally used to pause the execution of program.
• The general syntax to call this function is: [var =] getch();
• The ‘var’ is of ‘int’ type. The ASCII value of entered character is stored into it.
Character Input
• The getche() Function
• The word “getche” stands for get character echo. The word echo means
repeat or display. This function is similar to getch( ) function. It is also used
to get (input) a single character from keyboard during execution of
program. However, the entered character is displayed on the screen.
• The general syntax to call this function is: [var =] getche();
• The ‘var’ is of ‘int’ type. The ASCII
value of entered character is stored
into it. The use of [var =] is optional.
The gotoxy() Function
Q.15 Describe the gotoxy() function.
The gotoxy() Function
• This function is used to position the screen cursor on the specified location
on the screen. Usually, gotoxy() function is used when an output is to be
displayed on a specified location on the screen. This function is also
defined in “conio.h” header file.
• The general syntax to call gotoxy() function is: gotoxy(x, y);
•x It indicates the x-coordinate or column number on the screen. Its
value can be from 0 to 79.
•y It indicates the y-coordinate or row number on the screen. Its value
can be from 0 to 24 or 39.
• For example, to display “Welcome” on the screen starting from row 16 and
column 5, the statements are:
gotoxy(4, 15);
printf(“Welcome”);
The gets() Function &
puts() Function
Q.16 Describe the gets() and puts() functions used for string input and
output.
The gets() Function
• The word “gets” stands for get string. This function is specially used to input (or get)
data into a string variable from the keyboard during program execution.
• Any type of characters, including spaces and special characters can be entered using
this function. Enter key is pressed to complete the input process. When Enter key is
pressed, a null character (’\0’) is automatically added at the end of the string
variable.
• The general syntax to call this function is: gets(strvar);
• strvar It represents the string variable into which data is to be stored.
Difference Between scanf() Function and gets() Function
• Data can also be entered into a string variable using scanf() function. But space
character cannot be inserted into the string variable using this function. When a
space key is pressed during input process, this function immediately appends the
null character (‘\0’) at that place in the string variable. The characters entered after
the space are not stored in the string variable. Therefore, gets() function is specially
The puts() Function
• The word “puts” stands for put string. This
function is used to display the string on the
computer screen. The new line is
automatically inserted after displaying the
string.
• The general syntax to call this function is:
puts(string);
• string It represents the string to be displayed
on the screen. It can be a string variable or string
constant. In case of string constant, the string is
enclosed in double quotation marks.
• For example, to display “Welcome” on
screen, the statement is written as:
puts(“Welcome”);
Assignment
• Write a program that initializes value to a variable x1 of float type and displays the value of x1 in the following
ways:
• using %f
• using %e
• using %E
• using %g
• Write a program to convert 20.3 centigrade temperature into Fahrenheit using the formula F= 9/5*C+32.
Display the output value on screen using field-width of 10 with precision of 3.
• Write a program to declare and initialize data into two string type variables. Display the contents of these
variables on the screen in right-justified and left-justified formats with field widths of 25 characters.
• Write a program to print the output as given under by using escape sequence.
C:\Windows>
‘P’ ‘A’ ‘K’
“Pakistan”
• Write a program to declare and initialize values into three variables; name, roll_no, and marks. Print the
output as shown below using ‘\n’ and ‘\t’ escape sequences.
Name Roll No Marks
======================================
Assignment
• Write a program that converts the height of a person from inches to centimeters using formula 2.54*height.
• Write a program that converts distance from kilometers into meters.
• Write a program that inputs four numbers and calculates the sum, average, and product of these numbers.
• Write a program that inputs two numbers and interchanges their values. The program should display the values of
variables before and after exchange.
• Write a program that inputs base and height from the user and calculates area of triangle by using the formula:
• Area = ½ *base*height
• Write a program that inputs the radius of a circle by using formula πR2. Assign the value 3.14159 of π in a constant by
using ‘define’ directive.
• Write a program that inputs the radius of sphere from user. Calculates its volume and surface area using the formula
Area = 4πR2 and circumference = 4/3 πR3. The value of π is 3.14159.
• Write a program to get the values of three sides of a triangle and calculate its area.
• Write a program that displays the ASCII code of the character entered by user using scanf() function.
• Write a program that converts a temperature in degrees Fahrenheit into degrees Celsius. For conversion, use the
following formula: C= 5/9(F-32)
• Write a program that stores the values ‘A’, ‘U’, 3.456E10 and 50 in separate memory cells. Your program should get the
first three values as input data, but use an assignment statement to store the last value.
• Write a program that inputs a three-digit number from user and displays it in reverse order. For example, if user enters
For more details and solved assignment, refers to
PM Series
Computer Science
ICS Part-II
by
CM Aslam, Aqsa Aslam, Abdur Rehman &
Mudassir Saleem