Data Input and Output in C

Managing input and output in any programming language is the fundamental demand according to the purposes of programming for developing project on system or applications. Applications which we use to reduce human efforts such as calculator to calculation the multiple digits or complex sum, we insert digit and the machine returns a result according to operation. The process to insert digit or value is called input and the result is called output.

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.

scanf() and printf()

The mostly used input function in C is scanf() and the mostly used output function in C is printf().
Using 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.
Focus on this statement:
  1. The %d is for defining that this function will take input integer number once and %d is enclosed with double quotation. The %d can 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 %c and for string %s.
  2. 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.
  3. 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: printf(“%d”,var1);
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 var1 in printf() like scanf(). Now run the following program and observe yourself.
#include <stdio.h>
int main()
{
    int var1; //Variable Declaration
    scanf("%d",&var1); //Input to var1
    printf("%d",var1); //Output of Input
    return 0;
}
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 scanf() function?
Ans: Yes. Follow as the given program to declare variable, input and output.
#include <stdio.h>
int main()
{
    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
    return 0;
}
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.
#include <stdio.h>
int main()
{
    int num1, num2;

    scanf("%2d %3d",&num1, &num2);

    printf("%d %d", num1, num2);

    return 0;
}
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.

    Example: printf("%6d\n", 52458);

    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.
    Example: printf("%-6d\n", 52458);
    #include <stdio.h>
    int main()
    {
    
        printf("%0d\n", 52458);
        printf("%0d\n", 52458);
        printf("%1d\n", 52458);
        printf("%2d\n", 52458);
        printf("%3d\n", 52458);
        printf("%4d\n", 52458);
        printf("%5d\n", 52458);
        printf("%6d\n", 52458);
    
        return 0;
    }
    
  • The following statement will print 052458

    printf("%06d\n", 52458);

    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. %w.j where w for total width and j for width to justify.

    Now run the following program and see.
    #include <stdio.h>
    int main()
    {
        float number;
        number = 55.258;
    
        printf("%7.3f\n",number);
        printf("%7.2f\n",number);
        printf("%-7.2f\n",number);
        printf("%-7.3f\n",number);
    
        return 0;
    }
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:

char variable_name[string_size];

string_size must be in integer.

Example: char ch[100];
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.
#include <stdio.h>
int main()
{
    char ch[10];

    scanf("%s",ch);
    printf("%s",ch);

    return 0;
}
Input: Hello
Output: Hello

In 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.
#include <stdio.h>
int main()
{
    char ch[100];
    scanf("%[^\n]",ch);
    printf("%s",ch);
    return 0;
}
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 %[a-z].


More Input and Output Function

1. getchar() and putchar()


getchar() and 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.
#include <stdio.h>
int main()
{
    char ch;
    ch = getchar();
    putchar(ch);
}
You can take input by getchar() function and print by printf() function regularly and also vice-versa.
#include <stdio.h>
int main()
{
    char ch;
    ch = getchar();//Input by getchar function
    printf("%c",ch); //Output by printf function
    return 0;
}
2. gets() and puts():
gets() and 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.
#include <stdio.h>
int main()
{
    char ch[100];

    gets(ch);
    puts(ch);
    return 0;
}
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.