In this tutorial, you will learn about the C printf() function to display output on the screen and the C scanf() function to take input from the user.
C Output
In C programming, printf() is one of the most commonly used functions to print the output to the console. This function sends the formatted output to the screen. For example,
Example 1: C Output
#include <stdio.h>
int main()
{
// Prints the string inside quotations to the console
printf("C Output");
return 0;
}
Output
How does the above program work?
- Every valid C program should have
main()function which works as the entry point of a program. - The
printf()is a library function which prints the formatted output to the screen. It prints the string which is specified inside the double quotation. - In order to use printf() function, we need to include one header file called
stdio.h. We have included it in the very first line of the program as#include<stdio.h>. - The
return 0;statement inside the main() function is the “Exit status” of the program. It’s optional to use.
Example 2: Integer Output
#include <stdio.h>
int main()
{
int intvalue = 10;
printf("Integer Output = %d", intvalue);
return 0;
}
Output
In the above example, we have used %d format specifier to print int (integer) type value. Here, the %d inside the double quotation is replaced by the value of intvalue.
Example 3: Print Character
#include <stdio.h>
int main()
{
char chr = 'c';
printf("Character Output = %c", chr);
return 0;
}
Output
In order to print character value, we use %c format specifier.
Example 4: Print float and double
#include <stdio.h>
int main()
{
float value1 = 10.5;
double value2 = 15.4;
printf("Float Output = %f\n", value1);
printf("Double Output = %lf", value2);
return 0;
}
Output
Double Output = 15.4
To print float value, we use %f format specifier. Similarly to print double value, we use %lf format specifier.
C Input
In C programming, scanf() if the most commonly used function which helps to take input from the user. This function reads formatted input from the standard input device such as keyboards. For example:
Example 1: Integer Input/Output
#include <stdio.h>
int main()
{
int intvalue;
printf("Enter an integer: ");
scanf("%d", &intvalue);
printf("Integer you have entered is = %d", intvalue);
return 0;
}
Output
Integer you have entered is = 10
In the above example, we have used %d format specifier inside the scanf() function to take the int (integer) type value from the user.
Example 2: Character I/O
#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c",&chr);
printf("You have entered = %c", chr);
return 0;
}
Output
You have entered = x
In the above program, we have used %c format specifier to get the character value from the user. However internally, C does not store the value as a character, it converts the character to its corresponding ASCII value which is an integer and store.
While printing the output using the %c format specifier, it converts the ASCII value to its corresponding character value and prints on the screen. By any chance, you provide %d format specifier, it’s ASCII value will be printed.
Example 3: ASCII Value
#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c", &chr);
// When %c is used, a character is displayed
printf("You have entered = %c\n",chr);
// When %d is used, ASCII value is displayed
printf("ASCII value is = %d", chr);
return 0;
}
Output
You have entered = a
ASCII value is = 065
Example 4: Float and Double I/O
#include <stdio.h>
int main()
{
float num1;
double num2;
printf("Enter a number: ");
scanf("%f", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);
printf("num1 = %f\n", num1);
printf("num2 = %lf", num2);
return 0;
}
Output
Enter another number: 15.8
num1 = 10.2
num2 = 15.8
We use %f and %lf format specifier to take input of float and double value respectively.
Example 5: Taking multiple values as input
#include <stdio.h>
int main()
{
int a;
float b;
printf("Enter integer and then a character: ");
// Taking multiple inputs
scanf("%d%c", &a, &chr);
printf("You have entered %d and %c", a, chr);
return 0;
}
Output
You have entered 10 and a