0% found this document useful (0 votes)
24 views49 pages

Ch-10 (ICS II) - Input Output

This document covers the concepts of standard input and output in C programming, detailing functions like printf() and scanf() for displaying and receiving data. It explains format specifiers, field-width specifiers, and how to handle strings and escape sequences in C. Additionally, it provides examples and syntax for using these functions effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views49 pages

Ch-10 (ICS II) - Input Output

This document covers the concepts of standard input and output in C programming, detailing functions like printf() and scanf() for displaying and receiving data. It explains format specifiers, field-width specifiers, and how to handle strings and escape sequences in C. Additionally, it provides examples and syntax for using these functions effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

COMPUTER SCIENCE – 12

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

• Floating-Point Format Specifiers


Format Specifier
Field-Width Specifier
Q.4 Describe the use of field-width specifier with example.
Q.5 How field-width for integers is specified? Explain with example.
Q.6 How field-width for floating point numbers is specified?
Field-Width Specifier
• The number of columns (or spaces) used to display or print a value on the output
device (such as display screen) is called field-width. The field-width specifier describes
the number of columns that should be used to display/print the value. The place
where the value of argument is printed is called its field.
• Syntax: % flag field-width precision
• % This sign indicates the beginning of field-width specifier.
• flag + or - or space can be used as flag. The uses of these symbols are as follows:
• + It is used to display sign with value. Usually it is used to display a + sign with positive value.
• - It is used to display output value left-justified in the field. By default, the output value is displayed
right-justified.
• space It is used to display blank spaces.
The use of flag is optional.
• field-width It is used to specify the field width for displaying the output value. Its use
is optional.
• precision It is used to specify the value for precision. It is generally used with
floating point values. Its use is optional.
Field-Width Specifier
• Examples:
• printf("Value of x = %10d", x);
• printf(“%15.2f”, y);
• Specifying Field-Width for Integers
• The integer format specifier %d or %i is used to display or read integer value. An
integer number is written between the symbol % and the format character ‘d’ or
‘i’. This integer number specifies the field-width.
• Suppose values of integer variable ‘x’ and ‘y’ are 62 and 716 respectively. To
display values of variables ‘x’ and ‘y’ with field widths of 5 and 8 respectively, the
printf() function is written as: printf(“%5d %8d”, x, y);
Field-Width Specifier
• Specifying Field-Width for Floating-point numbers
• In case of floating-point numbers (or real values), the field width as well as the number of
decimal places can also be specified.
• Syntax: %W.Df
• % This sign indicates the beginning of field-width specifier.
• W It indicates the total field width including the decimal point and the fractional part. Its use is
optional.
• D It indicates the number of decimal places (after the decimal point). The value after decimal point
is rounded off. By default, 6 digits are displayed to the right of decimal point. Its use is optional.
• The total field width should include:
• one space for digit ‘0’ before decimal point
• one space for the decimal point
• one space for the minus sign if the number is negative.
• Suppose a variable ‘x’ has value 48.557. To display the value of ‘x’ having field width of 10
with two decimal places, the statement is written as: printf(“%10.2f”, x);
Field-Width Specifier
Displaying Output Values
Left-Justified
Q.7 How is output values displayed/printed left-justified?
Displaying Output Values Left-
Justified
If a minus sign (-) is specified before the field
width, the output value will be displayed left-
justified. Suppose the value of x = 62, and y =
716. To display the values of ‘x’ and ‘y’ left-
justified, the statement is written as;
printf(“%-5d %-8d”, x, y);
Displaying Character Type
Data
Q.8 How can you print character value in C language?
Displaying Character Type Data
• The format specifier %c is used in printf() function to display character type data.
• For example, to display the contents of a character type variable ‘xy’, the printf() function is
written as: printf(“The value of xy = %c”, xy);
• Suppose the value of variable ‘xy’ is ‘A’, then the output of the above statement will be as:
The value of xy = A
• The value of integer variable can be displayed using %c format specifier. In this case,
integer value is converted into its equivalent character and then character is displayed.
• Suppose an integer variable ‘x’ has value 67. To display its equivalent character, the statement is
written as: printf(“The equivalent character of ASCII %d is = %c”, x, x);
• The output of the statement will be: The equivalent character of ASCII 67 is = C
• Similarly, if format specifier %d is used to display the value of character variable, then
character is converted into its equivalent ASCII value.
• For example, if x = ‘B’, then to display its equivalent ASCII value, the statement is written as:
printf(“The ASCII equivalent of %c is = %d”, x, x);
• The output of the statement will be: The equivalent ASCII value of B is = 66
Displaying Character Type Data
String Variable
Q.9 What is a string and string variable? How is string variable declared and
initialized in C?
Q.10 How a string is assigned to a string variable?
String Variable
• A string is a collection of characters. The characters may include alphabets,
digits and special characters. A variable that is used to store a string is
called string variable.
• The last character of every string variable is a null character. The null
character is denoted by ‘\0’. This character marks the end of string. Thus
when data is entered into a string variable, null character at the end of
string will be added automatically. Thus if a string variable is declared with
10 characters, it can store only 9 characters.
• Suppose the size of a string variable is 12. The value “PAKISTAN” is
assigned to it.
String Variable
• Declaring String Variable
• Specifying the name of string variable and its length is called declaring
string variable. The data type of string variable is ‘char’ type. The maximum
number of characters that a string variable is to store is specified in square
brackets in declaration statement.
• Syntax: char string[len];
• string It is the name of string variable.
• len It indicates the size or length of string variable. It is an unsigned integer
value.
• For example, to declare the string variables ‘name’ and ‘address’ of length
25 and 35 respectively, the statement is written as:
char name[25], address[35];
String Variable
• Initializing String Variable
• Like other variables, a string variable can also be initialized with a string at
the time of its declaration.
• For example, to declare a string variable ‘str’ and to assign a string
“PAKISTAN” into it, the statement is written as: char str[14] = “PAKISTAN”;

• 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

Publisher: Majeed Sons


22- Urdu Bazar, Lahore

You might also like