C programming Input-Output (I / O): printf () and scanf ()

In this chapter we will focus on the two built-in functions of C programming: printf () and scanf () which are used for performing input and output tasks. Also in this chapter you will learn to write a valid C program.

In order to perform input and output functions, there are some built-in library functions in C programming.

The two most commonly used functions for input-output (I / O) are printf () and scanf ().

The scanf () function is used to get the formatted input from the user through the standard input device. On the other hand, the printf () function is used to send the formatted output at the standard output device (screen).


Input's syntax
scanf ("% c", & var_name); // Character for input
scanf ("% d", & var_name); // for integer input
scanf ("% f", & var_name); // float input is taken
scanf ("% lf", & var_name); // double input is taken
scanf ("% s", & var_name); // string input is taken

Output's syntax
printf ("% c", & var_name); // to get the output in the character
printf ("% d", & var_name); // to get the output in the integer
printf ("% f", & var_name); // to get the output at float
printf ("% lf", & var_name); // to get output at double
printf ("% s", & var_name);  to get the // string output

Example: C output
#include <stdio.h> // printf () It is necessary to run the function.
int main ()
{
    printf ("C Programming"); // The contents of the quotes are displayed.
    return 0;
}

Output

C Programming
The explanation of how the program works is given below

Must be a main () function in all C programs. The code execution starts from the first of the main () function.
 printf () is a library function that sends formatted output to the screen. The printf () function is declared in "stdio.h" header file.
Here stdio.h is a standard input-output header file and #include preprocessor which combines source code with source code. When the compiler executes the printf () function and finds the stdio.h header file, the compiler shows error.
 return 0; The statement goes out of the program. Ends the program in simple language.

For example: C character input / output
#include <stdio.h>
int main ()
{
    char character;
    printf ("Enter a character:");
    scanf ("% c", & character); The% c format string is used for the // character
    printf ("you entered% c.", character);
    return 0;
}


Output

Enter a character: t
You entered
"% C" format string is used for receiving formatted character input and sending output.


Example: C Integer Output
#include <stdio.h>
int main ()
{
    int integerNumber = 5;
    // The% d format string is used for the integer
    printf ("number =% d", integerNumber);
    return 0;
}

Output

Number = 5
There is a formatted string "% d" for the integer in the printf () function intensity in the quotation mark. In this case, if the format string matches the integerNumber argument, it shows the output on the screen.


For example, C Ingestion Input / Output
#include <stdio.h>
int main ()
{
    int IntegerNumber;
    printf ("Enter a integer number:");
    scanf ("% d", & IntegerNumber);
    printf ("number =% d", integerNumber);
    return 0;
}
Output

Enter a integer number: 4
Number = 4
The scanf () function takes formated input from the keyboard. When user intersection number inputs, it is stored in the IntegerNumber variable.

Notice that there is a '&' symbol before IntegerNumber. By IntegerNumber, point to IntegerNumber's address and the input value is credited to that address.


Example: C Float Input / Output
#include <stdio.h>
int main ()
{
    float floatNumber;
    printf ("Enter a number:");
    The% f format string is used for // float
    scanf ("% f", & floatNumber);
    printf ("Value =% f", floatNumber);
    return 0;
}

Output

Enter a number: 23.45
Value = 23.450000
"% F" format string is used to get formatted input for float and send output


ASCII code
When a character is inserted in the above program, the character is not saved in its own format. Rather, the number is stored in value (askey value) format.

When we print the "% c" text format, it shows in the character format on the screen.


For example, the ASKI code
#include <stdio.h>
int main ()
{
    char character;
    printf ("Enter a character:");
    scanf ("% c", & character);

    // When we print using "% c" text format, it shows in a character format in the screen.
    printf ("You entered% c. \ n", character);

    // When we print using the "% d" text format, it shows in the integer format on the screen.
    return 0;
}

Output

Enter a character: b
You entered b
ASCII value of g is 96.
The ASCII value of character 'b' is 96. When 'b' is entered, then the variable 'V' is filled with 96 instead of 'b'.

If you know the ASKY code of a character, you can take the output as a character. It is shown in the following example:


Examples:
#include <stdio.h>
int main ()
{
     int character = 70;
     printf ("Character of ASCII value 70 is % c.", character);
     return 0;
}

Output

Character of ASCII value 70 is F.