Sei sulla pagina 1di 25

Chapter 4: Input/Output

fQ1: Describe the purpose of input/output statements?


Input & Output:
The statements that are used to provide data to the program during
its execution are called input statements. The following functions are
used in C to input data into the program:

• scanf function
• gets function
• getch function
• getche function

Similarly, the statements that are used to get data from the program
and send it to an output device are called output statements. The
following functions are used in C to send data to an output device:

• printf function
• puts function

The scanf and printf are most commonly used functions. These are
used to input and output values, respectively, these functions are defined
in the stdio.h header file. This header file must be included in the
preprocessor directive if these functions are to be used in the program. It
is included in the beginning of the program as shown below:

#include <stdio.h>

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

Q2: Describe the printf () function?


The printf Function:
The printf function is used to print values and text on the output
device in a specified format. It is also called the formatted output
function. The general syntax of this function is:

Printf (control_string [, list of arguments]);

control_string:
It consists of text, the format specifies and the escape sequence. It
is written within double quotes.
• The text specifies the message that is to be printed along
with the values.
• A format specifier specifies the format according to which a
value is to be printed.

List of arguments:
It consists of a list of variables, constants or arithmetic expressions,
separated by commas, whose values are to be printed.
The values are printed according to the corresponding format
specifier specified in the control string. The format specifier and the
arguments are given such that the first specifier applies to the first
argument, the second specifier to the second argument, and so on.
The arguments in the printf function are optional. When the printf
function is used to print only a message, the arguments are omitted.
For example to print a message on the screen, the printf function is
written as:
printf ("this is a program in C");

Program:
Write a program to print a message on screen by using the printf function.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf ("C++ is based on the C language and");
printf ("both were developed at bell Lab. USA");
getch();
}

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

Q3: Describe the format specifies used in formatted


input/output?
Format Specifiers
A format specifier specifies the data type, field width and format of
a value. It is used to specify the format according to which a value is to
be printed. It is also used to specify the format according to which a value
is to be read from an input device.
The general syntax of the format specifier is shown below:

%flags field-width precision h/1/L con-character

% the percent sign indicated the beginning of the specifier.


Flags the flag character +, -, # and blank are used. Their use is
optional. These affect the output as shown below:

Flag Use
+ A plus or minus sign is always used with the signed
output values
- It specifies that the output value is left justified in the
output field. If this flag is not used, the values are
output as right-justified.
# If this flag is used then:
• The octal values are precedent by 0.
• The hexadecimal values are preceded by 0x or
0X.
• The floating point values contain a decimal
point.
Blank It specifies that zero or a positive value is to preceded
by a blank rather than a positive sign.

Field-width The field width specifies the minimum number of


columns reserved for the output value. If the value
requires more columns, the field is expanded. If the
value is smaller than the specified, then the empty
spaces are filled with blanks. Its use is optional.
Precision It is generally used with floating point values. A
precision .n specifies that n decimal places are to be
output. If the value being output has more than n
decimal digits, 9it is rounded.
Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252
‫ملک طیب عارف‬
Chapter 4: Input/Output

h/1/L The h, 1 or L is used to specify that the output is short,


long or long double data type, respectively.
Con-character The conversion character is used to convert an output
value. The output value is first converted and then
displayed on the output device.
• However, for integer type data only integer type
conversion characters can be used.
• The character type data is also dealt as integer
type data in C language. Thus, for character type
data, any integer conversion character an be
used, and vice versa.
• Similarly, for float type data, only float
conversion characters can be used.

The following conversion characters are used:


Conversion Output
For Integers
i The output is converted to signed integer value.
d The output is converted to signed decimal integer
value.
o The output is converted to unsigned octal integer
value.
u The output is converted to unsigned decimal integer
value.
x The output is converted to unsigned hexadecimal value
with lowercase letters: a, b, c, d, e, f.
X The output is converted to unsigned hexadecimal value
with uppercase letters: A, B, C, D, E, F.
For Floating Point
f The output is converted to signed real value.
e The output is converted to signed real value using e-
notation.
E The output is converted to signed real value using E-
notation.
g It is used for large floating point value using e-
notation. Or f form, which ever is shorter.
G It is used for large floating-point value using E-
notation or F form, which ever is shorter.
For Characters
c It is used for a single character.
s It is used for string.

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

Q4: Describe how the character type values are printed


using formatted output.
Characters:
The conversion character c is used in the format specifier to output
character type values using printf function. For example, to print the
contents of a character type variable ch, the printf function is written as:

printf ("The value of ch = %c", ch);

if the value in the variable ch is B, then the above statement will print:

The value of ch = B

If a decimal integer value is given, the type conversion character converts


the decimal value to its equivalent character and then prints it. For
example to print the character corresponding to the ASCII value of 65,
the printf () function is written as:

printf ("The ASCII equivalent of 65 is = %c", 65);

The character 'A' is represented by the ASCII value 65. The conversion
character will first convert the integer value 65 into its equivalent ASCII
code (i.e. A) and then print it:

The ASCII equivalent of 65 is = A

Program:
Write a program to declare and initialize the values to variables x, y & z
of char type and then print their contents on screen by using printf
function.
#include<stdio.h>
#include<conio.h>
main()
{
char x='D', y='B', z='C';
clrscr ();
printf ("Data in x = %c \n", x);
printf ("Data in y = %c \n", y);
printf ("Data in z = %c \n", z);
getch ();

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

}
Note: the 'n' has been used in string to print the data of each statement on
a new line.
Output
Data in x = D
Data in y = B
Data in z = C

Q5: What are string variable? How are they declared?


String Variables
The variable that is used to hold a string is called string variable.
Since a string consists of a combination of characters, the char type
variables are used to hold a string.
The maximum number of characters that a string variable is to hold
is specified in square brackets next to the name of a character type
variable. The syntax to declare a string type variable is:

char string [18];

A string always has a Null character (\0) added to it as its last


character. Thus a string variable declared to hold 18 characters can only
hold a maximum of 17 characters of the string. The last character is
always the null character.
For example, when a value "This is a String" is assigned to the
string variable string [18], it is represented in the memory as shown
below:
1 2 3 4 5 6 7 8 9 1 11 1 1 1 1 1 1 18
0 2 3 4 5 6 7
T h i s i s a S t r i n g . \0

A string variable can also be declared without specifying the


maximum number of character and only by initializing a value to it. In
this syntax, the string variable is declared as:

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

char string[] = "This is a Long String";

In this syntax, C specifies the number of characters. In the above


example, the string length will be 23 characters (22 for the string
characters and 1 for the null character.).
A string variable can also be declared by both initializing it and
specifying its character length. This syntax to declare a string variable is:

Char string[20] = "Short String";

In the above example, although the string can hold a maximum of


19 characters, only its first 13 characters are initialized. This is shown
below:

1 2 3 4 5 6 7 8 9 1 11 1 1 1 1 1 1 1 1 20
0 2 3 4 5 6 7 8 9
S h o r t S t r i n g \0

Q6: Describe how the string type values are printed using
formatted output?
Strings
The conversion character 's' is used in the format specifier to output
string type values using the printf () function.
For example, if name is a string type variable that contains the
name of a person, the string field specifier is used:

Printf ("Customer name is %s", name);

If the variable name contains the value "Ahmed Nisar", then the
above printf function will print:

Customer name is Ahmed Nisar


Program:
Write a program to declare and initialize data into string type variables

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

names and city and print these variables on the screen by using the printf
function.
#include<stdio.h>
#include<conio.h>
mian()
{
char name[13] = "Marriam Ahmed";
char city[25] = "Lahore";
clrscr ();
printf ("Name: %s \n", name);
printf ("City: %s \n", city);
getch ();
}
Output:
Name: Marriam Ahmed
City: Lahore

Q7: Describe how integer type values are printed using


formatted output.
Integer Values
The type conversion character 'd' is used in the format specifier to
output integer type values using printf () function. For example, to print
the values of integer variables a and b the function is written as:

Printf("A=%d, B=%d", a, b);

If the values of the variables are a = 10 and b = 20, then the printf
function will print:

A = 10, B = 20

Program
Wire a program to declare and initialize data into an integer type variable
x and print the value of x in:
Decimal format.
The ASCII character of the integer value.
The hexadecimal value of the integer value.
The Octal value of the integer value.
#include<stdio.h>
Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252
‫ملک طیب عارف‬
Chapter 4: Input/Output

#include<conio.h>
main()
{
int x = 97;
clrscr ();
printf ("The decimal value of x = %d \n", x);
printf ("The ASCII value of x = %d \n", x);
printf ("The Hexadecimal value of x = %d \n", x);
printf ("The Octal value of x = %d \n", x);
getch ();
}
Output
The decimal value of x = 97
The ASCII value of x = a
The Hexadecimal value of x = 61
The Octal value of x = 141

Program:
Write a program to declare and initialize data to a variable x of float type
and print its value on the screen using the control characters %f, %e, %g
& %G.
#include<stdio.h>
#include<conio.h>
main()
{
float x = 6714.987696f;
clrscr ();
printf ("Value of x using control character f = %f \n", x);
printf ("Value of x using control character e = %e \n", x);
printf ("Value of x using control character E = %E \n", x);
printf ("Value of x using control character g = %g \n", x);
printf ("Value of x using control character G = %G \n", x);
getch();
}
Output
Value of x using control character f = 6714.987793
Value of x using control character e = 6.714988e+03
Value of x using control character E = 6.714988E+03
Value of x using control character g = 6714.99
Value of x using control character G = 6714.99

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

Q8: Describe the use of field-width specifier for formatted


output?
Field Width Specifier
The field width specifier is used to specify the min8imum number of
columns for the value. An integer value specifying the field width is
given with the format specifier.
• If the value requires more columns, the field is expanded.
• If the value is smaller than the specified columns, then the empty
spaces are filled with blanks. Its use is optional.
For example to print two integers a = 9 and b = 87 with field widths of
10 for each value, the print () function is written as:

Printf ("A=%10d, B=%10d", a, b);

In case of the floating type data, the number of decimal places can
also be specified. The syntax for a floating type value is:

w.d

w
specifies the field width including the decimal point and the
fractional part. Its use is optional.
d
specifies the number of decimal places (after the decimal point).
The value is rounded.

For example, to print the value a = 65.566 with only one decimal
place, the printf function is written as:

printf ("A=%10.1f", a);

The value will be rounded and the output will be:

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 6
A= 65.6

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

Similarly, to print left-justified values A = 65.566 and B = 316.87


with two decimal places, the printf function is written as:

Printf ("A=%-10.2f B=%-10.2f", a, b);

The value will be rounded and the output will be:


0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
A = 65.6 B = 316.87

Program:
Write a program to declare and initialize data to string type variables
name and city and print their contents on the screen in left-justified and in
right-justified format with field width 30.
#include<stdio.h>
#include<conio.h>
main()
{
char name[25] = "Marriam Ahmed Qureshi";
char city[25] = "Samanabad, Lahore";
clrscr ();
printf ("Left-justified\n");
printf ("%-30s \n", name);
printf ("%-30s \n", city);
printf ("Right-justified\n");
printf ("%30s \n", name);
printf ("%30s \n",city);
getch ();
}
Output
Left-justified
Marriam Ahmed Qureshi
Samanabad, Lahore
Right-justified
Marriam Ahmed Qureshi
Samanabad, Lahore

Program:
Write a program to declare and initialize the data into float type variables
x, y &z and print their values on the screen in left justified as well as in
right justified format having field width of 20 each and with only one

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

digit after decimal point.


#include<stdio.h>
#include<conio.h>
main()
{
float x=7122377.98776;
float y=18.98776;
float z=7568.98776;
clrscr();
printf ("Left-justified\n");
printf ("%-20.1f \n", x);
printf ("%-20.1f \n", y);
printf ("%-20.1f \n", z);

printf ("Right-justified\n");
printf ("%20.1f \n", x);
printf ("%20.1f \n", y);
printf ("%20.1f \n", z);
getch ();
}
Output
Left-justified
71.22.378.0
19.0
7569.0
Right-justified
7122378.0
19.0
7569.0

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

Q9: Explain escape sequences used in formatted statements?


Escape Sequence
Special characters are used to control printing on the output device.
These are called escape sequence. These characters are not printed. These
are used inside the control string.
An escape sequence is a combination of a backslash '\' and a code
character. The backslash is called the control character.
For example, to transfet the printing control to the next line, the
escape sequence is written as \n. for example, in the following statement
the control is transferred to next line after printing the message "I Love
Pakistan":
printf("I Love Pakistan\n");
In the above statement \n is an escape sequence character. It moves
the cursor to the start of the next line after the string is printed.
An escape sequence can be used anywhere in the control string. It
can be used at the beginning of the string, in the middle or at the end of
the string.
If it is used at the beginning of the string then the string will be
printed after printing a blank line. To print the above string in three line
the statement is written as:
printf("I\nLove\nPakistan");
The output will be:
I
Love
Pakistan
Other commonly used 4escape sequence character are given below:

\a "a" stands for alert or alarm. This character causes a


beep sound in the computer internal speaker. It is
usually used to draw attention during the execution of
a program
\b "b" stands for backspace. This escape sequence
causes the print control on the output device move
one space back. For example.
printf("Pakistan\b");
After printinf the string "Pakistan", the cursor will
move one space back and will be under the character
'n' of the word "Pakistan".

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

If "\b" is not used, then the cursor will be at the end


of string. If the above example, if another statement
is used after the above statement, its printing will
start from character 'n' and the last character 'n' will
be deleted. Thus in the following example;
printf("Pakistan\b");
printf("Punjab");
The output will be as under:
PakistaPunjab
\t "t" stands for tab. This escape sequence causes the
print control in the output device to move one tab
forward.
For example
printf ("A\tB\tC\tD");
The output of the above statement will be as under:
A B C D
\n "n" stands for new line, it moves the printing control
to the beginning of the next line.
\f "f" stands for form feed. The escape sequence
escapes one blank paper on the printer attached with
computer. It is normally used to print the output on
the printer after feeding a blank form.
\r "r" stands for carriage return. It moves the cursor to
the beginning of the current line.
\\ These are two backslash characters. One is used as
control character and second is used to print a
backslash '\' character. For example;
printf("A:\\xyz");
The output will be as under
A:\xyz
If a single backslash character is used, then there will
be no effect on the output for example;
printf ("A:\xyz");
The output will be as under
A:xyz
\" This escape sequence is similar to the "\\". It is used
to print double quotation marks ("). For example;
Printf ("\"PAK");
The output will be as under
"Pak
\' This escape sequence is same as "\" but it is used to
print a single quotation mark (').
For example, to print Pakistan as 'Pakistan', the
Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252
‫ملک طیب عارف‬
Chapter 4: Input/Output

output statement is written as:


printf ("\'Pakistan\'");

Program:
Write a program to implement the escape sequence character to print
"Pak" in different forms. Use "\n" in each string to insert a new line.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf ("\aPak\n");
printf ("\"Pak\");
printf ("\'Pak\'\n");
printf ("P\ta\t\k\n");
printf ("\\Pak\\");
getch();
}
Output
Pak
"Pak"
'Pak'
P a k
\Pak\

Program:
Write a program to exchange the values of two variables and to print their
actual and exchanged values.
#include<stdio.h>
#include<conio.h>
main()
{
int a, b, t;
a = 19;
b = 78;
clrscr();
printf("Value of a & b before exchange are %d and %d \n", a, b);
t = a;
a = b;
b = t;
printf ("value of a & b after exchange are %d and %d \n" a, b);
getch ();

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

}
Output
Value of a & b before exchange are 19 and 78
Value of a & b after exchange are 78 and 19

Program:
Write a program to computer the distance covered by a car in 50 seconds.
The initial velocity is 10m/sec and acceleration is 5 m/sec2. use the
formulas s=vit+1/2at2.
#include<stdio.h>
#include<conio.h>
main()
{
int t, vi, a;
double s;
t = 50;
vi = 10;
a = 5;
s = vi*t+0.5*a*t*t;
clrscr();
printf ("Distance covered = %f m", s);
getch();
}
Output:
Distance covered = 6750.00000 m

Program:
Write a program to input two numbers. Calculate the sum of the numbers
and print on the screen.
#include<stdio.h>
#include<conio.h>
main()
{
int n, m, s;
clrscr ();
printf ("Enter first number?");
scanf ("%d", &n);
printf ("Enter second number?");
scanf ("%d", &m);
s = n+m;
printf ("Sum of %d & %d is = %d", n, m, s);

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

getch();
}
Output
Enter first number? 12
Enter second number? 23
Sum of 12 & 23 is = 35

Program:
Write a program to input a number. Calculate the cube of the number and
print the result on the screen.
#include<stdio.h>
#include<conio.h>
main()
{
int n, res;
clrscr();
printf ("Enter any number?");
scanf ("%d", &n);
res = n*n*n;
printf ("The cube of %d is = %d", n, res);
getch ();
}
Output:
Enter any number ? 9
The cube of 9 is = 729

Program:
Write a program to input the marks obtained by a student in three
subjects. Calculate the total marks and their average and print the results
on the screen.
#include<stdio.h>
#include<conio.h>
main()
{
float total, s1, s2, s3, avg;
clrscr();
printf ("Enter marks of first subject?");
scanf ("%f", &s1);
printf ("Enter marks of seconf subject?");
scanf ("%f", &s2);
printf ("Enter marks of third subject?");

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

scanf ("%f", &s3);


total = s1+s2+s3;
avg = total/3.0;
printf ("\nTotal marks = %f", total);
printf ("\nAverage marks = %f", avg);
getch();
}
Output:
Enter marks of first subject? 92
Enter marks of second subject? 95
Enter marks of third subject? 98

Total marks = 285.000000


Average marks = 95.000000

Q10: Describe the scanf function?

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

scanf Function
The scanf function is used to get values into variables from the
keyboard during execution of the program. The value is input 9into a
variable in a specified format. Its syntax is:

scanf (control_string, list of variables);

control_string:
Specifies the format specifier. It is written within double quotes. A
format specifier specifies the format according to which a value is to be
entered. The control_string in scanf is different from the control string in
printf. Unlike printf control string, strings cannot be given here.

List of variables
Consists of a list of variables, separated by commas, into which the
values are to be entered. In the list of variables, the memory locations of
the variables that correspond to the specifiers in the control string, are
given, the memory location of a variable is indicated with the address
operator. The ampersand (&) sign is used to specify the address operator.
It is placed before the variable name.

For example to input a value from the keyboard into a variable age,
the scanf function is written as:

scanf ("%d", &d);

Values into more than one variables can be input with one scanf
function. The variables may be of different or same data type. For
example, to input values into three integer variables a, b and c, the scanf
function is written as:

scanf ("%d %d %d", &a, &b, &c);

When this statement is executed, the values may be entered in one of


the following two ways:

• Type value for the first variable and press Enter key. The program
will prompt to get the value for the second variable. Repeat the
process until values in all variables have been entered.
• Type the values of all the variables separated by at least one space
and press Enter key.

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

In order to prompt the user during execution of the program, a scanf


statement is usually preceded by a printf statement. The printf statement
is used to print the information that is to be entered from the keyboard.
For example:
printf ("Enter Velocity in m/s?\n");
scanf ("%f", &vel);
Before the program needs the value of velocity to be entered from the
keyboard, the printf statement informs the user as shown below:

Enter Velocity in m/s?


316.87
Program:
Write a program to input temperature in Fahrenheit. Convert the
temperature of Celsius degrees by using the formula C = 5/9(F-32).
#include<stdio.h>
#include<conio.h>
main()
{
float ct, ft;
clrscr ();
printf ("Enter temperature in Fahrenheit?");
scanf ("%f", &ft);
ct = (ft-32)*5.0/9.0;
printf ("The temperature in Celsius is = %f", ct);
getch();
}
Output:
Enter temperature in Fahrenheit? 125
The temperature in Celsius is = 51.666668

Program:
Write a program to input the radius of a sphere. Compute its volume and
surface area (Formula for surface area = 4πR3 and π=3.14).
#include<stdio.h>
#include<conio.h>
main()
{
float R, V, area;
clrscr();
printf ("Enter radius of shphere?");
scanf("%f", &R);
area = 4.0*3.14*R*R;
Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252
‫ملک طیب عارف‬
Chapter 4: Input/Output

V = (4.0/3.0)*3.14*R*R*R;
printf ("Volume of sphere = %f \n", V);
printf ("Area of sphere = %f \n", area);
getch();
}
Output:
Enter radius of sphere? 9.87
Volume of sphere = 4025.500000
Area of sphere = 1223.556274

Q11: Describe the gets function?


The "gets" Function
The function is used to get (input) data into a string variable from
the keyboard. Any type of character, including spaces and special
characters can be entered using this function. Enter key is pressed after
typing the string. When Enter key is pressed, a null character is appended
at the end of the string.
Its syntax is:

gets (strvar);

strvar
Represents the string type variable into which data is to be entered.

Q12: Describe the puts function?


The "puts" function
It is used to print a string on the computer screen. A new line is
inserted after printing the string. Its syntax is:

puts (string);

The "string" may be a string constant or string type variable. In


case of a string constant, the string is enclosed in double quotes. In case
of a string variable, it is written without using quotes.

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

Program:
Write a program to input a string by using "gets" function and then print it
on the screen by using the "puts" function.
#include<stdio.h>
#include<conio.h>
main()
{
char str[20];
clrscr();
puts ("Enter a string?");
gets(str);
puts("The string is:");
puts(str);
getch();
}
Output
Enter a string?
Marriam Ahmed
The string is:
Marriam Ahmed

Q13: Describe the getch function?


The "getch" Function
The "getch" stands for get chatacter. This function is used to get a
single character from the keyboard during program execution. When a
key is pressed
• The entered character is not displayed on the screen
• Pressing of the Enter key is not required to complete the input. The
control shifts to the next statement in the program as soon as a key
on the keyboard is pressed.
This function is normally used to pause the execution of program. Its
syntax is given below:

[var=] getch();

The use of [var=] is optional. It is used to store the value of the character
entered from the keyboard.

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

Q14: Describe the getche() function?


The "getche" Function
This function is similar to "getch". It is also used to get a single
character from the keyboard during the program execution. The main
difference between "getch" and "getche" is that the entered character is
displayed on the screen when "getche" function is used. Its syntax is:

[var =] getche();

The use of [var =] is optional. It is used to store the value of the character
entered from the keyboard.

Q15: Describe the clrscr function?


The "clrscr" Function
The "clrscr" stands for clear screen. This function is used to clear
the screen of the computer. When this function is executed, the screen is
cleared and the cursor shifts to upper left corner, i.e. to row 1 of column 1
on the screen. Its syntax is:

clrscr ();
Program:
Write a program to clear the screen of the computer monitor.
#include<stdio.h>
main()
{
clrscr ();
}

Q15: Describe the gotoxy function?


The "clrscr" Function
Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252
‫ملک طیب عارف‬
Chapter 4: Input/Output

The "gotoxy" function shifts cursor on the screen to a specified


location. This function is used when an output is to be printed on a
specified location on the screen. Its syntax is:

gotoxy (x, y);

Where
x:
The x co-ordinate (or column) on the screen. Its value may be from
0 to 79.
y:
The y co-ordinate (or row) on the screen. Its value may be from 0
to 24.

For example, to print output on the screen starting from row 6 and
column 10, the statement are written as:

gotoxy (9, 5);


printf ("Welcome to Pakistan");

Program:
Write a program to print "Welcome" in four corners of the screen and
print "Pakistan" at the center of the screen by using the gotoxy function.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr ();
gotoxy (0, 0);
printf ("Welcome");
gotoxy (70, 0);
printf ("Welcome");
gotoxy (0, 24);
printf ("Welcome");
gotoxy (70, 24);
printf ("Welcome");
gotoxy (35, 12);
printf ("Pakistan");
getch ();
}

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬
Chapter 4: Input/Output

Program:
Write a program to input the age of a person in years. Convert the age
into months and days and print the result in the center of the screen. (1
year = 365 days approx.)
#include<stdio.h>
#include<conio.h>
main()
{
long int age, mon, days;
clrscr ();
printf ("Enter age in years?");
scanf ("%1d", &age);
mon = age*12;
days = age*365;
gotoxy (35, 12);
printf ("Age in months = %1d", mon);
gotoxy (35, 13);
printf ("Age in days = %1d", days);
getch ();
}

Output:
Enter age in years ? 4

Age in months = 48
Age in days = 1460

Reference:
CM ASLAM
T A QURESHI
ARSHAD JAVED

Email: tayyab8632@yahoo.com, malik8632@yahoo.com, Mob: 03445064252


‫ملک طیب عارف‬

Potrebbero piacerti anche