C - 用户输入
C 语言中对用户输入的需求
每个计算机应用程序都会从用户那里接受特定的数据,并对其进行预定义的处理以生成输出。C 语言中没有可以读取用户输入的关键字。
C 编译器附带的标准库包含 stdio.h 头文件,其库函数 scanf() 最常用于从标准输入流接受用户输入。此外,stdio.h 库还提供了其他用于接受输入的函数。
示例
为了理解对用户输入的需求,请考虑以下 C 程序 -
#include <stdio.h> int main(){ int price, qty, ttl; price = 100; qty = 5; ttl = price*qty; printf("Total: %d", ttl); return 0; }
输出
上述程序通过将客户购买的商品价格乘以数量来计算总购买金额。运行代码并检查其输出 -
Total:500
对于另一笔价格和数量值不同的交易,您需要编辑程序,输入值,然后重新编译并运行。每次都这样做非常繁琐。因此,必须提供在程序运行后将值赋给变量的功能。scanf() 函数会在运行时读取用户输入,并将值赋给变量。
C 用户输入函数:scanf()
C 语言将标准输入流识别为 stdin,并由标准输入设备(例如键盘)表示。 C 语言始终以字符形式从输入流读取数据。
scanf() 函数使用适当的格式说明符将输入转换为所需的数据类型。
Scanf() 的语法
以下是您在 C 语言中使用 scanf() 函数的方式 -
int scanf(const char *format, &var1, &var2, . . .);
scanf() 函数的第一个参数是一个格式字符串。它指示要解析用户输入的变量的数据类型。它后面跟着一个或多个指向变量的变量的指针。变量名以 & 为前缀。给出变量的地址。
用户输入格式说明符
格式字符串中使用以下格式说明符 -
格式说明符 | 类型 |
---|---|
%c | 字符 |
%d | 有符号整数 |
%f | 浮点值 |
%i | 无符号整数 |
%l 或 %ld 或%li | 长整型 |
%lf | 双精度型 |
%Lf | 长整型 |
%lu | 无符号整型或无符号长整型 |
%lli 或 %lld | 长整型 |
%llu | 无符号长整型 |
示例:C 语言中的用户输入
回到前面的示例,我们将使用 scanf() 函数来接受"price"和"qty"的值,而不是为它们分配任何固定值。
#include <stdio.h> int main(){ int price, qty, ttl; printf("Enter price and quantity: "); scanf("%d %d", &price, &qty); ttl = price * qty; printf("Total : %d", ttl); return 0; }
输出
当上述程序运行时,C 等待用户输入值 -
Enter price and quantity:
输入值并按下 Enter 键后,程序将继续执行后续步骤。
Enter price and quantity: 100 5 Total : 500
更重要的是,对于另一组值,您无需再次编辑和编译。只需运行代码,程序就会再次等待用户输入。这样,程序可以使用不同的输入运行任意次数。
Enter price and quantity: 150 10 Total : 1500
整数输入
已为有符号整数定义了%d格式说明符。以下程序读取用户输入并将其存储在整数变量num中。
示例:C语言中的整数输入
查看以下程序代码 -
#include <stdio.h> int main(){ int num; printf("Enter an integer: "); scanf("%d", &num); printf("You entered an integer: %d", num); return 0; }
输出
运行代码并检查其输出 −
Enter an integer: 234 You entered an integer: 234
如果输入非数字值,输出将为"0"。
scanf() 函数可以将值读取到一个或多个变量中。输入值时,必须使用空格、制表符或 Enter 键分隔连续的值。
示例:C 语言中的多个整数输入
#include <stdio.h> int main(){ int num1, num2; printf("Enter two integers: "); scanf("%d %d", &num1, &num2); printf("You entered two integers : %d and %d", num1, num2); return 0; }
输出
运行代码并检查其输出 −
Enter two integers: 45 57 You entered two integers : 45 and 57
或者
Enter two integers: 45 57
浮点数输入
对于浮点数输入,您需要使用 %f 格式说明符。
示例:C 语言浮点数输入
#include <stdio.h> int main(){ float num1; printf("Enter a number: "); scanf("%f", &num1); printf("You entered a floating-point number: %f", num1); return 0; }
输出
运行代码并检查其输出 −
Enter a number: 34.56 You entered a floating-point number: 34.560001
示例:C 语言中的整数和浮点数输入
scanf() 函数可以读取不同类型的变量的输入。在以下程序中,用户输入存储在一个整数变量和一个浮点数变量中 -
#include <stdio.h> int main(){ int num1; float num2; printf("Enter two numbers: "); scanf("%d %f", &num1, &num2); printf("You entered an integer: %d a floating-point number: %6.2f", num1, num2); return 0; }
输出
运行代码并检查其输出 −
Enter two numbers: 65 34.5678 You entered an integer: 65 a floating-point number: 34.57
字符输入
%c 格式说明符从键盘读取单个字符。但是,我们必须在格式字符串中的 %c 前添加一个空格。这是因为 %c 转换说明符不会自动跳过任何前导空格。
如果输入流中存在多余的换行符(例如,来自上一个条目),则 scanf() 调用将立即使用它。
scanf(" %c", &c);
格式字符串中的空格指示 scanf 跳过前导空格,并使用 %c 转换说明符读取第一个非空格字符。
示例:C 语言中的字符输入
请看以下示例 -
#include <stdio.h> int main(){ char ch; printf("Enter a single character: "); scanf(" %c", &ch); printf("You entered character : %c", ch); return 0; }
输出
运行代码并检查其输出 −
Enter a single character: x You entered character : x
示例:C 语言中的多个字符输入
以下程序读取两个 char 变量中以空格分隔的两个字符。
#include <stdio.h> int main(){ char ch1, ch2; printf("Enter two characters: "); scanf("%c %c", &ch1, &ch2); printf("You entered characters: %c and %c", ch1, ch2); return 0; }
输出
运行代码并检查其输出 −
Enter two characters: x y You entered characters: x and y
stdio.h 头文件还提供了 getchar() 函数。与 scanf() 不同,getchar() 没有格式字符串。此外,它读取单个按键,无需 Enter 键。
示例:使用 gets() 进行字符输入
以下程序将单个按键读入 char 变量 -
#include <stdio.h> int main(){ char ch; printf("Enter a character: "); ch = getchar(); puts("You entered: "); putchar(ch); printf(" You entered character: %c", ch); return 0; }
输出
运行代码并检查其输出 −
Enter a character: W You entered: W You entered character: W
您还可以使用未格式化的 putchar() 函数来打印单个字符。
示例:读取字符序列
以下程序展示了如何读取一系列字符,直到用户按下 Enter 键 -
#include <stdio.h> int main(){ char ch; char word[10]; int i = 0; printf("Enter characters. End by pressing the Enter key: "); while(1){ ch = getchar(); word[i] = ch; if (ch == ' ') break; i++; } printf(" You entered the word: %s", word); return 0; }
输出
运行代码并检查其输出 −
Enter characters. End by pressing the Enter key: Hello You entered the word: Hello
字符串输入
还有一个 %s 格式说明符,用于将一系列字符读入 char 数组。
示例:使用 scanf() 进行字符串输入
以下程序接受来自键盘的字符串输入 -
#include <stdio.h> int main(){ char name[20]; printf("Enter your name: "); scanf("%s", name); printf("You entered the name: %s", name); return 0; }
输出
运行代码并检查其输出 −
Enter your name: Ravikant You entered the name: Ravikant
C 使用空格作为分隔符。因此,如果您尝试输入包含空格的字符串,则只有空格之前的字符会被存储为值。
Enter your name: Ravikant Soni You entered the name: Ravikant
gets() 函数 克服了这一限制。它是一个非格式化字符串输入函数。按下 Enter 键之前的所有字符都存储在变量中。
示例:使用 gets() 进行字符串输入
请看以下示例 -
#include <stdio.h> #include <stdlib.h> int main(){ char name[20]; printf("Enter your name: "); gets(name); printf("You entered the name: %s", name); return 0; }
输出
运行代码并检查其输出 −
Enter your name: Ravikant Soni You entered the name: Ravikant Soni
用户输入是 C 语言编程中应用程序的一个重要方面。本章通过不同的示例讲解了格式化和非格式化控制台输入函数 scanf()、getchar() 和 gets() 的用法。