作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include <stdio.h>
int main() {
char a[1000];
int i = 0;
scanf("%s", &a);
while (a[i] != 0) {
printf("%c\n", a[i]);
i++;
}
printf("\n");
return 0;
}
+456
+
4
5
6
最佳答案
您的代码有两个问题:
1.
scanf("%s", &a);
参数
a
已经衰减到指向数组第一个元素的指针
a
-> 所以它作为参数的类型实际上是
char *
.因此,
&a
类型为
char (*)[1000]
.
char *
的预期参数存在类型不匹配为
%s
转换说明符。
-Wall
选项(或分别为
-Wformat=
)向您显示了关于此的警告:
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[1000]' [-Wformat=]
9 | scanf("%s",&a);
| ~^ ~~
| | |
| | char (*)[1000]
| char *
'\0'
):
char a[1001];
否则,如果用户输入 1000 位数字,则空字符将存储在数组边界之外,这会调用未定义的行为。
scanf("%1000s", a);
或使用
fgets()
这是默认情况下确保这一点的,因为它需要将字符数作为第二个参数写入。
fgets(a, sizeof(a), stdin);
sizeof(a)
适合作为
sizeof(char)
总是
1
.它获得元素的数量,
a
已。
#include <stdio.h>
int main (void) {
char a[1001];
int i = 0;
printf("Please enter an integral number up to 1000: ");
fgets(a, sizeof(a), stdin);
// scanf("%1000s", a); Alternative to previous fgets.
while (a[i] != 0) {
printf("%c\n", a[i]);
i++;
}
printf("\n");
return 0;
}
关于c - 我想在 C 中制作程序打印每个数字的完整数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61853222/
使用登录后,我想吐出用户名。 但是,当我尝试单击登录按钮时, 它给了我力量。 我看着logcat,但是什么也没显示。 这种编码是在说。 它将根据我在登录屏幕中输入的名称来烘烤用户名。 不会有任何密码。
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我是一名优秀的程序员,十分优秀!