Standard Library Input and Output functions in C

Standard input and output functions

C programming language libraries that allow input and output in a program. the  Stdio.h or Standard input-output library in C that has a method for input and output.

Standard Input output

Input:  

when we talking about the input, so it means to feed some data into a program. an input can be given in the form the command line. C programming has a set of built-in functions to read the given input and feed it to the program as per requirement. scanf() is a function which is used to take input.

Output:

When we talking about the output, so it means to display something in the screen. The data we want to print in the screen that is print as it is. C programming has a set of built-in function to output the data on the computer screen. printf() is a function which is used for output.

Standard IO

Formate Specifier for I/O

Here’s the list of commonly used data type and their formate specifier. which is used in printf() and scanf() function for take input and print the value of any variable of data type.

    Data type

Formate  Specifier

            int              %d
            char               %c
            double               %lf
            float               %f
            short int               %hd
            unsigned int               %u
            long int               %li
            long long int               %lli
            unsigned long int               %lu
            unsigned long long int               %llu
            signed char                %c
            unsigned char                 %c
            long double                 %Lf

 

C Output

As we see the printf() is an output function in C. this function sends formatted output to the screen.

To use the printf() in our program, we need to include stdio.h header fil using #include<stdio.h> statement. 

the return 0; statement inside main() function is the Exit status of the program.

example

#include<stdio.h>

int main()
{
    printf("learn C programming from Prepinsta");
    return 0;
}

Output

learn C programming from Prepinsta.

Formate Specifier in C output for data type 

In this section, we see commonly used data type. we see how to print data type value.

#include <stdio.h>
int main()
{
int testInteger = 5;
float testFloat = 13.5;
double testDouble = 12.4;
char testChar = 'a';
printf("int value = %d \n", testInteger);
printf("float value = %f\n", testFloat);
printf("double value = %lf\n", testDouble);
printf("char value = %c", testChar);
return 0;
}


Output

int value = 5 
float value = 13.500000
double value = 12.400000
char value = a

Here we use formate specifier to print data type. here, the %d,%f,%c,%lf inside the quotation will be replaced by the value of testInt, testFloat, testDouble and testChar.

C input

As we see the scanf() is an input function in C this function used to take input from the user. the scanf() function reads formatted input from the standard input such as keyboards.

Example

here we will use commonly used, formate specifier in c input.

#include <stdio.h>

int main()
{
int testInteger;
float testFloat;
double testDouble;
char testChar='9';

printf("enter a character value ");
scanf("%c",&testChar);
printf("\nyour entered char value = %c", testChar);

printf("\nenter an Integer value ");
scanf("%d",&testInteger);
printf("\nyour entered integer value = %d ", testInteger);

printf("\nenter a float value ");
scanf("%f",&testFloat);
printf("\nyour entered float value = %f", testFloat);

printf("\nenter a double value ");
scanf("%lf",&testDouble);
printf("\nyour entered ndouble value = %lf", testDouble);

return 0;
}

Output

enter a character value g

your entered char value = g
enter an Integer value 45

your entered integer value = 45
enter a float value 4.4

your entered float value = 4.400000
enter a double value 45.89

your entered ndouble value = 45.890000

here we have used %d,%f,%lf and %c  formate inside the scanf() function to take int,float, double and character input respectively from user. when user input integer value, it stored in the testInterger variable. 

Notice, that we have used &testInteger inside scanf(). It is because &testInteger gets the address of testInteger, and the value entered by the user is stored in that address.