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("\nYou 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 == '\n')
         break;
      i++;
   }
   printf("\nYou entered the word: %s", word);
   
   return 0;
}

输出

运行代码并检查其输出 -

Enter characters. End by pressing the Enter key: Hello

You entered the word: Hello

字符串输入

还有一个%s格式说明符,用于将一系列字符读入字符数组。

示例:使用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()的用法。

广告

© . All rights reserved.