Data Input and Output in C
We want to write a program what will calculate addition, subtraction, multiplication, division. Before we have learned to write a program to calculate this operation with given value. Now we don’t know the value of variables by which we want to write a calculation program. What will we do now? Obviously we have to put system to take input. After taking input we will write statements of calculation with variable and finally we will print the calculation results.
Yes. There has many input functions in C to take input different data type value. With this built in functions, we can take not only digits, but also character, string.
The mostly used input function in C is
scanf() and the mostly used output function in C is
scanf() function we can take input integer, float, double, character, string.
If we want to take input of a integer number, then we have to write a statement like below:
scanf(“%d”,&var1);Assume variable var1 has been declared as integer data type before.
%dis for defining that this function will take input integer number once and
%dis enclosed with double quotation. The
%dcan be called input data type specifier. Similarly, if we want to take float data type then the input data type specifier would
%f, for character
%cand for string
- The given
scanf()function has two parts: First is input specifier and second is variable address to input. After enclosing input type specifier the comma to separate two parts.
- We assumed variable var1 is declared as integer type. Including ampersand (‘&’) before variable name defines the location of input will be saved. As we want to integer input would save in var1 variable we have defined it with this variable name. Now if we want to print the value of variable var1, we have to write another statement like below:
Here, the %d is output data type specifier and var1 for defining which variable’s value we want to print.
Remember: Don’t put ampersand (‘&’) before
scanf(). Now run the following program and observe yourself.
int var1; //Variable Declaration
scanf("%d",&var1); //Input to var1
printf("%d",var1); //Output of Input
Hints: This program will print the input given by user. What you input, it will return the same output. Question: Is it possible to input multiple same or different data type in one
Ans: Yes. Follow as the given program to declare variable, input and output.
int var1; //Integer Variable Declaration
float var2; //Float Variable Declaration
char var3; //Character Variable Declaration
scanf("%d %f %c",&var1, &var2, &var3); //Input to var1, var2, var3 serially
printf("%d %f %c",var1, var2, var3); //Output of Input
Input: 10 5.75 A
Output: 10 5.750000 A
Formatted Input and Formatted Output
Formatted Input: It refers to input data in particular format. We input 4 digit of a number to variable. But if we want to specify the number of digit (2 digit) to read data by read function like
scanf(), then this required input is called formatted input.
To format input, we have to specify the number of digit we want to store in variable. If we want to take and only 3 digit of a integer variable number, then the statement will be:
scanf("%3d",&number); Now run the following program in your compiler.
int num1, num2;
scanf("%2d %3d",&num1, &num2);
printf("%d %d", num1, num2);
Input: 233445 767688
Output: 23 767
Formatted Output: Similarly formatted input it can possible to format of output.
- If the number of formatted output specifier be greater than the number of digit of variable, then the formatted output will be significant.
If number of formatted output specifier be positive, output will be right-justified. The width of right-justified will be the remaining width from total width as formatted output specifier and the width of the value of variable to print.
If number of formatted output specifier be negative, output will be left-justified.
The width of left-justified will be the remaining width from total width as formatted output specifier and the width of the value of variable to print.
- The following statement will print 052458
We have discussed formatted output for integer. If want to do formatted output for fractional and string, what will be?
For fractional and strings it has different rules.
wfor total width and
jfor width to justify.
Now run the following program and see.
number = 55.258;
Similarly before, Positive for right-justified and negative for left-justified. You can make formatted output for string as it.
Input String and line
We have learned how to take input a character. But if you try to take a string (a collection of character) what we have to:
First we have to declare a string.
The declaration of string is:
string_size must be in integer.
This type variable declaration is called character array in C.
Now run and observe the following program. This program will print string what we input.
scanf() function we didn’t put ampersand (‘&’) before variable name. Because in character array the name of variable is also the address of variable.
We can take input one word by this method. In character array whitespace and after the part of whitespace aren’t read by
scanf() function. So if we want to input “Hello World” and print it, then the program will print only Hello.
How to read and print a sentence with whitespace by
scanf() function in C?
We can do it by another specification in
scanf(). If we put
%[^n] instead of
%s, it will read all with whitespace till newline.
Now run the following program.
Input: Hello World
Output: Hello World
By this input specifier we can define what will be input. Example: If we want to input only a to z then the specifer will be
More Input and Output Function
putchar() are two built-in functions, the first one is for input a character and the last one is for print the character.
Now run the program.
ch = getchar();
You can take input by
getchar() function and print by
printf() function regularly and also vice-versa.
ch = getchar();//Input by getchar function
printf("%c",ch); //Output by printf function
puts() are two built-in functions, the first one is for input line of text and the last one is for print the line of text.
Though it is easy method to input a line of text, you should avoid this function.
Have got this article helpful or not working? Put your comment below.