You are on page 1of 86

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. */

15. printf("AIM Computer Academy");


16. /*
17. printf() stands for print formatted.

7
Program – 001

18. This function reside in stdio.h header file.


19. The job of this function is to print the message on console window.
20. */

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

6. printf(" Hello Aimers, Myself Vishal Koshti");


7. printf("\n I have done Master in Computer Application from GLS");
8. printf("\n I am programmer and professor");
9. printf("\n I am Founder and Director of AIM Computer Academy");
10. printf("\n My academy situated at Science city road");
11. getch();
12. }

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

6. printf(" I have done my Master of Computer Application from GLS");


7. printf("\n The full of form of my college is Gujarat Law Society");
8. printf("\n GLS is situated at Law Garden");
9. printf("\n GLS has total four campus and provide the education for many
education streams");
10. printf("\n GLS is now become GLS University");
11. getch();
12. }

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

Program number : 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

10. printf("\n @ @ ");


11. printf("\n @ @ ");
12. printf("\n @ @ ");
13. printf("\n @ @ ");
14. printf("\n @@@@ ");
15. getch();
16. }

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

9. printf("\n Following is multiline comment");


10. /*
11. I will not be complied by the compiler
12. I have starting symbol as well as ending symbol
13. Any number of lines can be added in multiline comment
14. */
15. getch();
16. }

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

5. float var = 0.0;


6. clrscr();
7. printf("Enter any float number : ");
8. scanf("%f",&var);
9. printf("Entered float number is : %f",var);
10. getch();
11. }

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

5. double var = 0.0;


6. clrscr();
7. printf("Enter any double number : ");
8. scanf("%lf",&var);
9. printf("Entered double number is : %lf",var);
10. getch();
11. }

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

The following statement,


putchar(‘\n’);
Prints the newline means it sends cursor to the beginning of next line.
Following program demonstrate the working of “getchar()” and “putchar()”.

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

Hence, “puts()” is same as “printf()” with newline.


The above “puts()” is same as,
printf(“\n Enter your name”);
Following program demonstrate the working of “gets()” and “puts()” function.

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

A. Printing integer number


The format specifier used to print the integer number is “d”.
To print the integer in formatted manner, the control string should contain the
specifier as,
%wd
Where, “w” denotes the width of the field in number of character positions to
print the integer.
The integers are printed by default right justified and hence if the field width is
larger than the number of digits in an integer to be printed, the blank spaces are
printed on left side.
If the field width is less than the number of digits, no digits are lost but it takes
more space after the field size and it may disturb the subsequent printing is same
line.
Check the following examples.

52
Formatted Input/Output

Format Output
printf(“%d”,123); 1 2 3
printf(“%5d”,123); 1 2 3
printf(“%2d”,123); 1 2 3

In above examples, the number by default comes right justified.


It is also possible to print the numbers with left justification using – (hyphen) flag
as follows.
printf(“%-6d”,5431);
The output produced for above is,
5 4 3 1

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

17. printf("\n %#6X -> 6X",num1);


18. printf("\n %#-6x -> -6x",num1);
19. printf("\n %#6o -> 6o",num1);
20. printf("\n\n Display second number in different formats");
21. printf("\n %d -> d",num2);
22. printf("\n %+010d -> 010d",num2);
23. printf("\n %+10d -> +10",num2);
24. printf("\n %#10x -> 10x",num2);
25. printf("\n %#10X -> 10X",num2);
26. printf("\n %#-10x -> -10x",num2);
27. printf("\n %#10o -> 10o",num2);
28. getch();
29. }

58
Formatted Input/Output

B. Printing floating point number


The format specification for printing floating point numbers is as follows,
%w.pf
The “w” denotes the field width and “p” denotes the number of digits printed to
the right of the decimal point.
The value printed is rounded to the “p” decimal point and default justification is
right.
The default value of “p” is 6.
If “f” is replaced by “e” in above specification, then numbers are printed in
scientific format.
Following are some example of it followed by program.
Consider the value of variable “x” is “58.3684” for the following example.

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

5. float num = 0.0;


6. clrscr();
7. printf("Enter any float number : ");
8. scanf("%f",&num);
9. printf("\n Display number in different formats");
10. printf("\n %f -> f",num);
11. printf("\n %7.4f -> 7.4f",num);
12. printf("\n %7.2f -> 7.2f",num);
13. printf("\n %-7.2f -> -7.2f",num);
14. printf("\n %11.2e -> 11.2e",num);
15. printf("\n %e -> e",num);
16. getch();
17. }

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

5. char str[20]={"VISHAL KOSHTI"};


6. clrscr();
7. printf("\n Display string in different formats");
8. printf("\n %s -> s",str);
9. printf("\n %20s -> 20s",str);
10. printf("\n %20.8s -> 20.8s",str);
11. printf("\n %10s -> 10s",str);
12. printf("\n %-20.8s -> -20.8s",str);
13. printf("\n %8s -> 8s",str);
14. getch();
15. }

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

Now if input is given is,


123 11223
Then “num1” is assigned “123” and “num2” is assigned “11223”.
But if input is given as,
11223 123
Then “num1” is assigned only first three digits of “11223” i.e. “112” and “num2” is
assigned the remaining digits of “11223” i.e. “23”.
Hence, “num1=112” and “num2=23”
For “num2” the “scanf()” reads only two digits even if the format specifies width 5.
The reason for that is when “scanf()” reads the integer number it reads either the
number of digits specified or upto the first non-digit character or white space
(blank, tab or newline).
The above does not read “123” at all and is read by the next “scanf()” that comes
in program.

74
Formatted Input/Output

So it is always advisable to have separate “scanf()” function for separate inputs.


The “scanf()” reads either required number of digits or up to the first non-digit
character.
If floating point value is entered while reading the integer, scanf() reads only upto
the decimal point reads required number of digits whoever is earlier.

75
Formatted Input/Output

B. Reading floating point number


While reading float or double, the field width is not necessary.
For example,
scanf(“%f %f”,&num1,&num2);
Following program explained the working of “scanf()” function for integer
numbers.

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

If we give the following for the above statement,


317.18 2.54e-2
Then, “num1” is assigned “317.18” and “num2” is assigned “0.0254”.
If number is to be read as double instead of float, then “%lf” is used in place of
“%f”.

79
Formatted Input/Output

C. Reading character or string


“getchar()” and “scanf()” is used to read single character and string respectively.
The “scanf()” can also be used to read a single character.
String is a sequence of characters and the field specification for the string input as
follows.
%ws or %wc
The important thing to be noted here is that when “%s” is used to read a string,
then “scanf()” reads only up to first blank but leaves rest of the part in the buffer
which is supplied to next “scanf()” in program.
For example,
scanf(“%s”,name);
When reads the input entered as
Vishal Koshti
Then only “Vishal” word is assigned to “name” variable.

80
Formatted Input/Output

The “scanf()” in many compilers also accepts the following formats.


%[characters] and %[^character]
Where “%[character]” all the characters in “[]” are accepted, but other characters
are not accepted.
The string is considered as complete when first character not in “[]” encounters.
While in “%[^character]” only character which are not there in “[]” are accepted.
The string is considered as complete when it encounters the first character in “[]”.
When single “scanf()” is used to read many items for different types, the care must
be taken to give the appropriate type of values in order.
Whatever order is specified in “scanf()”, should be followed when values are
entered.
If this rule is violated, “scanf()” reads only values which are in proper order and
returns with number of values is could read properly.

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

17. printf(" You name is : %s",str);


18. fflush(stdin);
19. printf("\n Enter your name only is upper alphabets : ");
20. scanf("%[^a-z]s",str);
21. printf(" You name is : %s",str);
22. getch();
23. }

85
Questions…?

AIM Computer Academy


: 7600 332 003, 7600 332 008
: vishalkoshti.aim@gmail.com
86
: www.aimacademy.co.in

You might also like