我们今天在类里面做了这个练习,但不幸的是,我的代码运行不正常。它不会打印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/