Hi friends, in this tutorial, you will know the input output functions in C language. Basically, there are two functions mainly to read input from input devices and display the output to the output devices. Let’s discuss them one by one.
Also read, Constant in C with Example
Below are the two input output functions in C language
scanf function in C
scanf() function is used to read or input the values of the variables from the standard input device such as keyboard etc.
Syntax:-
scanf("format string", &V1, &V2……….&Vn);
where v1, v2…Vn is the variable whose values are to be read from the keyboard.
“format string” is the control string that represents the data type such as integer, float, character and double, etc.
Below is the table of format strings for various data types
Data Type | Format | Meaning |
int | %d %u %o %x | Represents a decimal integer value Represents an unsigned integer value Represents an unsigned octal value Represents an unsigned hexadecimal value |
float / double | %f %e | Represents a floating-point value Represents a floating-point value in decimal or exponential form |
char | %c %s | Represents a single character value Represents a string of characters of values. |
Some examples of scanf() function
1. scanf("%d %d", &a, &b);
This example is used to read values of two integer variables a and b respectively.
2. scanf("%f %d", &x, &n);
This example is used to read the float value of x and integer value of n respectively.
3. scanf("%c", &age);
This example is used to read the character value of variable age.
4. scanf("%s", sname)
This example is used to read the string of characters for the char variable name.
When these functions are executed, the computer will wait for the values of the variable listed in scanf() which are entered from the keyboard.
printf() function in c
printf() function prints or displays the values of the variables using the standard output device such as a monitor etc.
Syntax:-
printf("format string", v1, v2……………..Vn);
whereas v1,v2….vn are the variables whose values are to be displayed on the monitor.
“format string is the control string that represents the format specification.
Note:- \n in printf() is used to print the result in a new line. Also do not include a space after \n
Example:-
#include<stdio.h>
main(){
int age;
age = 35;
printf("My age is %d",age);
printf("\nI am fully employed");
}
Output:-
My age is 35
I am fully employed
Process returned 0 (0x0) execution time : 0.094 s
Press any key to continue.
Also read, Variable declaration in C with example
Conclusion:- I hope this tutorial will help you to understand the concept of input output function in C language. If there is any doubt then please leave a comment below.