CP Chapter 04 Input Output
CP Chapter 04 Input Output
CP Chapter 04 Input Output
Prepared by:
Vishal Koshti
1
Introduction
User interaction is one of the important properties of the higher level languages
which make it possible for the user to interact with the program while it is running.
This makes the live contact between the program and the user.
Whenever program needs any input it prints the appropriate message and then
waits for the user to give input values.
Similarly, program produces the output with the appropriate messages and labels.
This is known as interaction between program and user.
This require input/output statements to be supported by the language.
It is supported through the library functions provided in the header file “<stdio.h>”
(Standard Input Output Header File).
2
Input/Output with
scanf() and printf()
1. printf() function
The format of the “printf()” is as follow,
printf(“controlString”,argument1,argument2,….,argumentN);
Where “controlString” is a string containing three types of characters.
1. Normal “Printable characters”, which are printed in output without any
changes.
2. “Escape sequence”, which always start from character backslah \.
They are used for cursor movement and other output functions.
3. “Format specifier”, which starts from “%”, followed by format specifier
necessary to print the values.
For example, “d” for integer, “f” for float etc.
The different format specifiers are listed in following table.
3
Input/Output with
scanf() and printf()
Character Meaning
c Character
d, i Integer
f Float or double, six position after decimal point
E, e Double in scientific format
G, g Double in %e or %E format if it is too small to too big
o Integer in octal
X, x Integer in hexadecimal
u Unsigned integer
h Short integer
s String
% No argument, to print % itself
4
Input/Output with
scanf() and printf()
“controlString” shows the number of values to be printed and how they are
printed.
The argument “argument1,argument2,….,argumentN” are either constants,
variables or expressions whose values are printed as per the corresponding
formats specified by the format specifier in the “controlString”.
For example,
printf(“Area of circle = %f”, area);
“Area of circle = %f”, is the control string.
The variable “area” is the only one argument i.e. the format specifier for “area” is
“%f”.
Hence, in place of “%f”, the value of “area” is printed in floating point format.
Following are some programs to give the demonstration of “printf()” function.
5
Program – 001
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to print "AIM Computer Academy" on console.
Program number : 001
*/
1. #include<stdio.h>
2. // Standard Input Output Header file
3. #include<conio.h>
4. // Console Input Output Header file
5. // # : Pre-processor directive
6
Program – 001
6. void main()
7. {
8. clrscr();
9. /*
10. clrscr() stands for clear the screen.
11. This function reside in conio.h header file.
12. The job of this function is to clear the screen before run the current
13. program.
14. */
7
Program – 001
21. getch();
22. /*
23. getch() stands for get the character.
24. Thie function reside in conio.h header file.
25. The job of this function is to hold output window until user press any
26. key.
27. */
28. }
8
Program – 002
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to print 5 statement about yourself.
Program number : 002
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. clrscr();
9
Program – 002
10
Program – 003
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to print 5 statement about your college.
Program number : 003
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. clrscr();
11
Program – 003
12
Program – 004
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to print first letter of your name by using *. For example
to print first letter of Vishal.
* *
* *
* *
**
*
Program number : 004
*/
13
Program – 004
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. clrscr();
6. printf("\n First letter of Vishal is displayed as follow");
7. printf("\n * * ");
8. printf("\n * * ");
9. printf("\n * * ");
10. printf("\n * * ");
11. printf("\n * ");
12. getch();
13. }
14
Program – 005
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to print circle using @.
@@@@
@ @
@ @
@ @
@ @
@ @
@ @
@ @
@@@@
15
Program – 005
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. clrscr();
6. printf("\n @@@@ ");
7. printf("\n @ @ ");
8. printf("\n @ @ ");
9. printf("\n @ @ ");
16
Program – 005
17
Program – 006
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to demonstrate the working of comments. Both single
line comment and multiline comment.
Program number : 006
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
18
Program – 006
5. clrscr();
6. printf(" Below line is single line comment");
7. // I will not be complied by the compiler
8. // I do not have ending symbol
19
Program – 007
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare one character, one integer, one float and one
double variable, assign some values to all the variable and then display their values.
Program number : 007
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
20
Program – 007
5. char cvar;
6. int ivar;
7. float fvar;
8. double dvar;
9. clrscr();
10. cvar = 'A';
11. ivar = 25;
12. fvar = 31.7;
13. dvar = 17.000009;
14. printf("\n Value of character variable is : %c",cvar);
15. printf("\n Value of integer variable is : %d",ivar);
16. printf("\n Value of float variable is : %f",fvar);
17. printf("\n Value of double variable is : %lf",dvar);
21
Program – 007
18. getch();
19. }
22
Input/Output with
scanf() and printf()
2. scanf() function
The format of “scanf()” function is as below,
scanf(“controlString”,&argument1,&argument2,….argumentN);
“controlString” in “scanf()” shows the format specifier preceded by “%”.
The format specifiers are listed in the previous table.
The “&argument1,&argument2,….argumentN” are the addresses of the argument
variables.
The values given are stored at respective memory locations whose addresses are
provided as argument.
For example,
scanf(“%d %f”,&inum1,&fnum1);
Reads an integer and a floating point number which are stored in variable “inum1”
and “fnum1” respectively.
23
Input/Output with
scanf() and printf()
The “&” sign preceding the variable names “inum1” and “fnum1” denotes the
address of “inum1” and “fnum1” respectively.
The “&inum1” means the memory location, where “inum1” is allocated the space
in memory.
It is always advisable to take only one input at a time with the help of “scanf()”
function.
It is possible that we can take more than one input in a single “scanf()” statement,
in that case variable names must be separated by blank space.
Following program demonstrate the working of “scanf()” functions.
24
Program – 008
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare one character variable. Take the input from the
user and then display its value.
Program number : 008
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
25
Program – 008
5. char ch = '\0';
6. clrscr();
7. printf("Enter any character : ");
8. scanf("%c",&ch);
9. printf("Entered character is : %c",ch);
10. getch();
11. }
26
Program – 009
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare one integer variable. Take the input from the
user and then display its value.
Program number : 009
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
27
Program – 009
5. int var = 0;
6. clrscr();
7. printf("Enter any integer number : ");
8. scanf("%d",&var);
9. printf("Entered integer number is : %d",var);
10. getch();
11. }
28
Program – 010
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare one float variable. Take the input from the
user and then display its value.
Program number : 010
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
29
Program – 010
30
Program – 011
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare one double variable. Take the input from the
user and then display its value.
Program number : 011
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
31
Program – 011
32
Program – 012
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare two character variables. Take the input from
the user of both the characters. Then display value of both variable on console.
Program number : 012
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
33
Program – 012
5. char ch1,ch2;
6. ch1 = ch2 = '\0';
7. clrscr();
8. printf(" Enter the first character : ");
9. scanf("%c",&ch1);
10. fflush(stdin);
11. printf(" Enter the second character : ");
12. scanf("%c",&ch2);
13. printf("\n First character is : %c",ch1);
14. printf("\n Second character is : %c",ch2);
15. getch();
16. }
34
Program – 013
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare two integer variables. Take the input from the
user of both the integers. Then display value of both variable on console.
Program number : 013
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
35
Program – 013
5. int num1,num2;
6. num1 = num2 = 0;
7. clrscr();
8. printf(" Enter first integer number : ");
9. scanf("%d",&num1);
10. printf(" Enter second second number : ");
11. scanf("%d",&num2);
12. printf("\n First integer number is : %d",num1);
13. printf("\n Second integer number is : %d",num2);
14. getch();
15. }
36
Program – 014
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare two float variables. Take the input from the
user of both the float. Then display value of both variable on console.
Program number : 014
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
37
Program – 014
5. float num1,num2;
6. num1 = num2 = 0.0;
7. clrscr();
8. printf(" Enter first float number : ");
9. scanf("%f",&num1);
10. printf(" Enter second float number : ");
11. scanf("%f",&num2);
12. printf("\n First float number is : %f",num1);
13. printf("\n Second float number is : %f",num2);
14. getch();
15. }
38
Program – 015
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare two double variables. Take the input from the
user of both the double. Then display value of both variable on console.
Program number : 015
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
39
Program – 015
5. double num1,num2;
6. num1 = num2 = 0.0;
7. clrscr();
8. printf(" Enter first double number : ");
9. scanf("%lf",&num1);
10. printf(" Enter second double number : ");
11. scanf("%lf",&num2);
12. printf("\n First double number is : %lf",num1);
13. printf("\n Second double number is : %lf",num2);
14. getch();
15. }
40
Character Input/Output
The function “getchar()” and “putchar()” are used to read and write characters
respectively.
1. getchar() function
The “getchar()” uses the buffered input and reads the next character from the
buffer and returns its ASCII value.
The “getchar()” can be used as,
characterVariable = getchar();
Where “characterVariable” is a variable of type character.
Consider the following statement,
char ch;
ch = getchar();
In this case, “getchar()” waits for user to enter a character and once user presses a
character, it assigns the ASCII value of that character to variable “ch”.
41
Character Input/Output
2. putchar() function
The function “putchar()” is used to print the character whose ASCII value is given
as input.
It can be used as follows,
putchar(variable);
Where “variable” should contain the ASCII of character to be printed.
For example,
char ch = ‘A’;
putchar(ch);
In this case, character ‘A’ will be printed as output.
The following statement
putchar(65);
Will also print ‘A’ as ASCII of ‘A’ is 65.
42
Character Input/Output
43
Program – 016
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare one character variable and take the input in
the lowercase from the user using getchar() function. Display the entered character
using putchar() function, and also display its uppercase.
Program number : 016
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
44
Program – 016
4. {
5. char ch = '\0';
6. clrscr();
7. printf("Enter any lowercase character : ");
8. ch = getchar();
9. printf("\n Entered character is : ");
10. putchar(ch);
11. printf("\n Uppercase letter of entered character is : ");
12. putchar(ch - 32);
13. getch();
14. }
45
String Input/Output
The function “gets()” and “puts()” are used to read and print the string
respectively.
The C program defines a string as character array.
1. Gets() function
The function “gets()” is used to read a string from a keyboard.
It can be used as,
gets(stringVariable);
Where, “stringVariable” is defined as character array.
For example,
char name[10];
gets(name);
46
String Input/Output
When above statements executes, the string entered by the user is assigned to
“name”.
2. puts() function
The function “puts()” is used to display a string on the screen.
It will automatically move the cursor to the beginning of the next line after printing
the string.
The general format of “puts()” is as follows,
puts(stringVariable);
For example,
puts(“Enter your name”);
Prints,
Enter your name
47
String Input/Output
48
Program – 017
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare two strings to store your name and college
name. Take the input of name and college from the user using gets() function. Then
display both the values using puts() function.
Program number : 017
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
49
Program – 017
4. {
5. char firstName[20]={'\0'},collegeName[20]={'\0'};
6. clrscr();
7. printf("Enter your first name : ");
8. gets(firstName);
9. printf("Enter your college name : ");
10. gets(collegeName);
11. printf("First name is : ");
12. puts(firstName);
13. printf("College name is : ");
14. puts(collegeName);
15. getch();
16. }
50
Formatted Input/Output
Sometimes we want our output to printed in some particular format and/or read
the input in specified way, then formatted I/O is must.
The C supports the formatted I/O with the same function, we used for
unformatted I/O i.e. “scanf()” and “printf()”.
1. Formatted output
Formatted output means to print the output in some format.
The marksheet is an example of the formatted printing as the information printed
in marksheet is organized in specific format.
Similarly, the various reports prepared by the business application software are
also examples of formatted output.
Following are various options through which we can print different kinds of data
like integer, float, character, string etc.
All of them are explained below.
51
Formatted Input/Output
52
Formatted Input/Output
Format Output
printf(“%d”,123); 1 2 3
printf(“%5d”,123); 1 2 3
printf(“%2d”,123); 1 2 3
53
Formatted Input/Output
Following table shows the various flags that can be used with the format specifier,
followed by some more examples of it.
Note that to print the long integers, the specifier “ld” is used in place of “d”.
Flag Meaning
- Left justified, the remaining spaces are blank.
+ Print + or – before signed number.
0 Print leading zero
# (With 0 or X) Print 0 or 0x before octal and hexa decimal number
# (With e, f or g) To print real number with decimal part even if it is whole number.
Prints trailing zeros.
54
Formatted Input/Output
Format Output
printf(“%06d”,2531); 0 0 2 5 3 1
printf(“%+6d”,2531); + 2 5 3 1
printf(“%#6x”,2531); 0 x 9 E 3
printf(“%#6X”,2531); 0 X 9 E 3
printf(“%#-6x”,2531); 0 X 9 E 3
printf(“%#60”,2531); 0 4 7 4 3
Following program shows the working of format output with all the possible cases.
55
Program – 018
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare two integer numbers and take the input of
both the numbers from the user. One number should be four digit and another
number should five digit. Display both the numbers in different formats.
Program number : 018
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
56
Program – 018
4. {
5. int num1,num2;
6. num1 = num2 = 0;
7. clrscr();
8. printf("Enter any four digit integer number : ");
9. scanf("%d",&num1);
10. printf("Enter any five digit integer number : ");
11. scanf("%d",&num2);
12. printf("\n Display first number in different formats");
13. printf("\n %d -> d",num1);
14. printf("\n %+06d -> 06d",num1);
15. printf("\n %+6d -> +6",num1);
16. printf("\n %#6x -> 6x",num1);
57
Program – 018
58
Formatted Input/Output
59
Formatted Input/Output
Format Output
printf(“%f”,x); 5 8 . 3 6 8 4 0 1
printf(“%7.4f”,x); 5 8 . 3 6 8 4
printf(“%7.2f”,x); 5 8 . 3 7
printf(“%-7.2f”,x); 5 8 . 3 7
printf(“%11.2e”,x); 5 . 8 4 E + 0 1
printf(“%e”,x); 5 . 8 3 6 8 4 0 E + 0 1
60
Program – 019
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare one float variable and take the input of
variable from the user. Now display the float number in different format.
Program number : 019
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
61
Program – 019
62
Formatted Input/Output
C. Printing character
The format specification for printing a character is as follows.
%wc
The w denotes the field width and default value of w is 1.
The character is printed right justified in the field of width w.
The character can be printed also left justified with – (hyphen) flag.
Following are some example of it followed by program.
Format Output
printf(“%c”,’A’); A
printf(“%3c”,’A’); A
printf(“%-3c”,’A’); A
63
Program – 020
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare one character variable and take the input from
the user then display the character in different format.
Program number : 020
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
64
Program – 020
5. char ch = '\0';
6. clrscr();
7. printf("Enter any character : ");
8. scanf("%c",&ch);
9. printf("\n Display character in different formats");
10. printf("\n %c -> c",ch);
11. printf("\n %4c -> 4c",ch);
12. printf("\n %-4c -> -4c",ch);
13. getch();
14. }
65
Formatted Input/Output
D. Printing string
The format specification for printing a string is as follows.
%w.ps
The w denotes the total width in character position and p denotes the number of
character from a string from beginning which are to be printed.
The string is printed left justified by default.
Consider the following examples for a string “VISHAL KOSHTI” stored in variable
“str” having length 15.
Following are some example of it followed by program.
66
Formatted Input/Output
Format Output
printf(“%s”,str); V I S H A L K O S H T I
printf(“%20s”,str); V I S H A L K O S H T I
printf(“%20.8s”,str); V I S H A L K
printf(“%10s”,str); V I S H A L K O S H T I
printf(“%-20.8s”,str); V I S H A L K
printf(“%.8s”,str); V I S H A L K
67
Program – 021
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare one string variable. Assign value to it. Then
display the string in different formats.
Program number : 021
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
68
Program – 021
69
Formatted Input/Output
2. Formatted input
Formatted input means while reading the values entered from the keyboard we
follow some format.
For example,
25 Angel 6
Here, the student’s roll number, name and his age are given as input.
As per above value, the first value is to be read in integer variable, second in string
variable and third in integer variable.
To read above input values, the “scanf()” should be written as follows,
scanf(“%d %s %d”,&rollNumber, name, &age);
In previous programs, we have already study the working of “scanf()” function.
70
Formatted Input/Output
A. Reading integer
The format specification for the integer input is as follows,
%wd
Where w denotes the length of the integer number in number of digits and d
shows that it is integer.
For example,
scanf(“%3d %5d”,&num1,&num2);
Following program explained the working of “scanf()” function for integer
numbers.
71
Program – 022
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare two integer variable. Take the input of both
the numbers from the user. Take input using only one scanf() function. Display the
values of both the numbers at last.
Program number : 022
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
72
Program – 022
5. int num1,num2;
6. num1 = num2 = 0;
7. clrscr();
8. printf("Enter two integer numbers : ");
9. scanf("%3d %5d",&num1,&num2);
10. printf("\n Value of first integer number is : %d",num1);
11. printf("\n Value of second integer number is : %d",num2);
12. getch();
13. }
73
Formatted Input/Output
74
Formatted Input/Output
75
Formatted Input/Output
76
Program – 023
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare two float variable. Take the input of both the
numbers from the user. Take input using only one scanf() function. Display the values
of both the numbers at last.
Program number : 023
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
77
Program – 023
4. {
5. float num1,num2;
6. num1 = num2 = 0;
7. clrscr();
8. printf("Enter two integer numbers : ");
9. scanf("%f %f",&num1,&num2);
10. printf("\n Value of first float number is : %f",num1);
11. printf("\n Value of second float number is : %f",num2);
12. getch();
13. }
78
Formatted Input/Output
79
Formatted Input/Output
80
Formatted Input/Output
81
Formatted Input/Output
For example,
scanf(“%d %s %f”,&rollNumber, name, &grade);
If we enter values as follows,
25 Angle 9.9
The above “scanf()” reads it properly and assigns them to respective variables.
The “scanf()” after reading the values returns the number of values read
successfully as return value.
In above case it returns 3 as it reads all the three values successfully.
Following program explained the working of “scanf()” function for character and
string.
82
Program – 024
/*
AIM Computer Academy
Author name : Vishal Koshti
Program : Write a program to declare one character variable and one string variable.
Take the input of character from the user and display its value. Take the input of in
different ways from the user and display it.
Program number : 024
*/
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
83
Program – 024
4. {
5. char ch = '\0';
6. char str[50] = {'\0'};
7. clrscr();
8. printf(" Enter any character : ");
9. scanf("%c",&ch);
10. printf(" Entered character is : %c",ch);
11. printf("\n Enter your full name : ");
12. scanf("%s",str);
13. printf(" Your full name is : %s",str);
14. fflush(stdin);
15. printf("\n Enter your name only is lower alphabets : ");
16. scanf("%[a-z]s",str);
84
Program – 024
85
Questions…?