在本教程中,您将学习使用scanf()函数从用户那里获取输入,并使用printf()函数向用户显示输出。
C 输出
在 C 编程中,printf()是主要的输出函数之一。 该函数将格式化的输出发送到屏幕。 例如,
示例 1:C 输出
#include <stdio.h>int main(){// Displays the string inside quotationsprintf("C Programming");return 0;}
输出
C Programming
该程序如何工作?
- 所有有效的 C 程序都必须包含
main()函数。 从main()函数的开始开始执行代码。 printf()是一种库函数,用于将格式化的输出发送到屏幕。 该函数在引号内打印字符串。- 要在我们的程序中使用
printf(),我们需要使用#include <stdio.h>语句包含stdio.h头文件。 main()函数内的return 0;语句是程序的“退出状态”。 它是可选的。
示例 2:整数输出
#include <stdio.h>int main(){int testInteger = 5;printf("Number = %d", testInteger);return 0;}
输出
Number = 5
我们使用%d格式说明符来打印int类型。 此处,报价内的%d将由testInteger的值替换。
示例 3:浮点和双精度输出
#include <stdio.h>int main(){float number1 = 13.5;double number2 = 12.4;printf("number1 = %f\n", number1);printf("number2 = %lf", number2);return 0;}
输出
number1 = 13.500000number2 = 12.400000
要打印float,我们使用%f格式说明符。 同样,我们使用%lf打印double值。
示例 4:打印字符
#include <stdio.h>int main(){char chr = 'a';printf("character = %c.", chr);return 0;}
输出
character = a
要打印char,我们使用%c格式说明符。
C 输入
在 C 编程中,scanf()是从用户获取输入的常用函数之一。scanf()函数从标准输入(例如键盘)读取格式化的输入。
示例 5:整数输入/输出
#include <stdio.h>int main(){int testInteger;printf("Enter an integer: ");scanf("%d", &testInteger);printf("Number = %d",testInteger);return 0;}
输出
Enter an integer: 4Number = 4
在这里,我们使用了scanf()函数中的%d格式说明符来获取用户的int输入。 当用户输入整数时,它存储在testInteger变量中。
注意,我们在scanf()中使用了&testInteger。 这是因为& testInteger获得了testInteger的地址,并且用户输入的值存储在该地址中。
示例 6:浮点和双精度输入/输出
#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;}
输出
Enter a number: 12.523Enter another number: 10.2num1 = 12.523000num2 = 10.200000
我们分别对float和double使用%f和%lf格式说明符。
示例 7:C 字符 I/O
#include <stdio.h>int main(){char chr;printf("Enter a character: ");scanf("%c",&chr);printf("You entered %c.", chr);return 0;}
输出
Enter a character: gYou entered g.
当用户在上述程序中输入字符时,字符本身不会被存储。 而是存储一个整数值(ASCII 值)。
当我们使用%c文本格式显示该值时,将显示输入的字符。 如果我们使用%d显示字符,则会打印它的 ASCII 值。
示例 8:ASCII 值
#include <stdio.h>int main(){char chr;printf("Enter a character: ");scanf("%c", &chr);// When %c is used, a character is displayedprintf("You entered %c.\n",chr);// When %d is used, ASCII value is displayedprintf("ASCII value is % d.", chr);return 0;}
输出量
Enter a character: g
You entered g.
ASCII value is 103.
I/O 多个值
这是您可以从用户那里获取多个输入并显示它们的方法。
#include <stdio.h>
int main()
{
int a;
float b;
printf("Enter integer and then a float: ");
// Taking multiple inputs
scanf("%d%f", &a, &b);
printf("You entered %d and %f", a, b);
return 0;
}
输出
Enter integer and then a float: -3
3.4
You entered -3 and 3.400000
I/O 的格式说明符
从以上示例中可以看到,我们使用
int的%dfloat的%fdouble的%lfchar的%c
这是常用的 C 数据类型及其格式说明符的列表。
| 数据类型 | 格式说明符 |
|---|---|
int |
%d |
char |
%c |
float |
%f |
double |
%lf |
short int |
%hd |
unsigned int |
%u |
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 |
