c语言input函数怎么调用
时间: 2024-05-14 17:15:22 浏览: 14
在C语言中,可以使用标准库函数`scanf()`或`fgets()`来读取输入。`scanf()`函数用于读取格式化的输入,而`fgets()`函数用于读取一行文本输入。以下是使用`scanf()`函数获取整数输入的示例代码:
```c
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered: %d", num);
return 0;
}
```
在上述代码中,`scanf()`函数使用`%d`格式说明符来读取整数输入,并将读取的值存储在变量`num`中。`&`符号用于获取变量`num`的地址,以便`scanf()`函数可以将读取的值存储在该地址中。
另一方面,以下是使用`fgets()`函数获取一行文本输入的示例代码:
```c
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("You entered: %s", str);
return 0;
}
```
在上述代码中,`fgets()`函数使用`stdin`作为输入流,并将读取的输入存储在字符串变量`str`中。`sizeof()`函数用于获取字符串数组`str`的大小,以确保不会超出其边界。