English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Neste tutorial, você aprenderá a usar a função scanf() para obter entrada do usuário e a função printf() para exibir saída para o usuário.
No programa de linguagem C, printf() é uma das funções de saída principais. A função envia dados formatados para a tela. Por exemplo,
#include <stdio.h> int main() { //Print output 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 statements in the main() function are 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("Input number: "); scanf("%d", &testInteger); printf("Number = %d",testInteger); return 0; }
Output result
Input 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 of testInteger, and the user's 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.
#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 types | format specifiers |
---|---|
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 |