c - 打印字符串的问题

标签 c string

我们今天在类里面做了这个练习,但不幸的是,我的代码运行不正常。它不会打印string1。我的老师也无法弄清楚为什么会发生这种情况。

#include <stdio.h>
#include <stdlib.h>

void main()
{
char string1[20];
char string2[] = "It is almost end of semester!";
size_t idx;
int size;

printf("Enter string1:\n");
scanf_s("%s", string1);
size = sizeof(string2) / sizeof(char);
printf("\nString 1 is : %s\n\n", string1);
for (idx = 0; idx < size; idx++)
{
    printf("%c ", string2[idx]);
}
puts("");
system("pause");;
}

最佳答案

scanf_s需要一个额外的参数。

scanf_s("%s", string1, _countof(string1));

关于c - 打印字符串的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26918615/

相关文章:

python - 在多词字符串列表中,提取第二个词

c++ - exit() 和 abort() 有什么区别?

c - 如何使用gdb进入while循环?

Javascript 字符串换行

Python 对字符串进行哈希处理

javascript - 从字符串中取出大写字母并打印它们

c 结构变量赋值中的编译错误

c - 在哪里可以找到 gcc 扩展属性的引用

c - 从 Postgresql 数据库检索图像文件返回两倍大的插入数据(通过 libpq 库)

python - 在 Python 3 中输入 int 列表