C语言中的输入函数有很多,以下是一些常见的输入函数:
1. scanf():用于从标准输入(键盘)读取数据。它接受一个格式化字符串作为参数,该字符串描述了要读取的数据类型和格式。例如:
```c
int a, b;
scanf("%d%d", &a, &b);
```
2. printf():用于向标准输出(显示器或终端)输出数据。它接受一个格式化字符串作为参数,该字符串描述了要输出的数据类型和格式。例如:
```c
printf("Hello, %d!
", 123);
```
3. getchar():用于获取下一个字符的ASCII码。如果输入为'n',则返回EOF(文件结束)。例如:
```c
char ch;
while (getchar() != 'n');
ch = getchar();
printf("You entered: %c
", ch);
```
4. fgets():用于从文件流中读取一行文本。它接受一个格式化字符串作为参数,该字符串描述了要读取的数据类型和格式。例如:
```c
FILE *file;
file = fopen("example.txt", "r");
if (file == NULL) {
perror("Failed to open file");
exit(1);
}
fgets(buffer, 100, file);
fclose(file);
printf("You entered: %s
", buffer);
```
5. cin >>:用于从标准输入读取多个整数值。例如:
```c
int a, b, c;
cin >> a >> b >> c;
```
6. cin >> noskipws >> str1 >> str2 >> str3 >> str4 >> str5;:用于从标准输入读取多个字符串。其中,noskipws表示忽略换行符;str1、str2、str3、str4、str5表示要读取的字符串。例如:
```c
string str1, str2, str3, str4, str5;
cin >> str1 >> str2 >> str3 >> str4 >> str5;
```
