English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use the scanf() function to obtain input from the user and use the printf() function to display output to the user.
In C language programming, printf() is one of the main output functions. This function sends formatted output to the screen. For example,
#include <stdio.h> int main() { //Print the string inside the quotes printf("C Programming"); return 0; }
Output result
C Programming
How does this program work?
All valid C programs must include the main() function. The code starts executing from the beginning of the main() function.
printf() is a library function to format output sent to the screen. The function will print the string inside the quotes.
To use the printf() function in our program, we need to include the stdio.h header file using the #include <stdio.h> statement.
return 0; The statement in the main() function is the program's 'exit status'. It is optional.
#include <stdio.h> int main() { int testInteger = 5; printf("Number = %d", testInteger); return 0; }
Output result
Number = 5
We use the %d format specifier to print int type. Here, the %d inside the quotes will be replaced by the value of testInteger.
#include <stdio.h> int main() { float number1 = 13.5; double number2 = 12.4; printf("number1 = %f\n", number1); printf("number2 = %lf", number2); return 0; }
Output result
number1 = 13.500000 number2 = 12.400000
To print float, we use the %f format specifier. Similarly, we use %lf to print double values.
#include <stdio.h> int main() { char chr = 'a'; printf("character = %c.", chr); return 0; }
Output result
character = a
To print char, we use the %c format specifier.
In C programming, scanf() is one of the commonly used functions to accept input from the user. This scanf() function reads formatted input from the standard input (such as the keyboard).
#include <stdio.h> int main() { int testInteger; printf("Enter a number: "); scanf("%d", &testInteger); printf("Number = %d",testInteger); return 0; }
Output result
Enter a number: 4 Number = 4
Here, we use the format specifier %d inside the scanf() function to receive the user's input integer value. When the user enters an integer, it will be stored in the testInteger variable.
Note that scanf() uses &testInteger inside. This is because &testInteger gets the address testInteger, and the user input value is stored at that address.
#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 result
Enter a number: 12.523 Enter another number: 10.2 num1 = 12.523000 num2 = 10.200000
Float and double are represented by %f and %lf format specifiers.
#include <stdio.h> int main() { char chr; printf("Enter a character:"); scanf("%c", &chr); printf("You entered %c.", chr); return 0; }
Output result
Enter a character: g You entered g.
When the user enters a character in the above program, the character itself is not stored. Instead, an integer value (ASCII value) is stored.
When we use %c text format to display the value, the entered character will be displayed. If %d is used to display a character, its ASCII value will be printed out.
#include <stdio.h> int main() { char chr; printf("Enter a character:"); scanf("%c", &chr); //When using %c, a character will be displayed printf("You entered %c.\n", chr); //When using %d, display the ASCII value printf("ASCII value %d.", chr); return 0; }
Output result
Enter a character: g You entered g. ASCII value 103.
This is an example of how to get multiple inputs from the user and display them.
#include <stdio.h> int main() { int a; float b; printf("Enter an integer, then enter a floating-point number:"); //Accept multiple inputs scanf("%d%f", &a, &b); printf("You entered %d and %f", a, b); return 0; }
Output result
Enter an integer, then enter a floating-point number: -3 3.4 You entered -3 and 3.400000
From the above examples, we can see that we use
%d corresponds to int
%f corresponds to float
%lf corresponds to double
%c corresponds to char
This is a list of commonly used C data types and their format specifiers.
Data type | Format specifier |
---|---|
int | %d |
char | %c |
float | %f |
double | %lf |
short int | %hd |
㩵n | |
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 |