我有以下代码:
#include <stdio.h>
#include <string.h>
int main(void)
{
char buff[50];
int pass = 0;
printf("\n Enter the password : \n");
gets(buff);
char str[80];
strcat(str, "nw");
strcat(str, "ww");
strcat(str, "io");
strcat(str, "oi");
char str2[22];
strcat(str2, "jm");
strcat(str2, "qw");
strcat(str2, "ef");
strcat(str2, "io");
strcat(str2, "nw");
strcat(str2, "ce");
strcat(str2, "or");
strcat(str2, "ww");
strcat(str2, "qf");
strcat(str2, "ej");
strcat(str2, "oi");
if(strcmp(buff, str))
{
/* we sf as df er fd xc yyu er we nm hj ui ty as asd qwe er t yu as sd df rt ty qw sd password */
printf ("\n Wrong Password \n");
}
else
{
printf ("\n Correct Password \n");
pass = 1;
}
if(pass)
{
printf ("\n\n\n\n\n\n\n%s", str2);
}
return 0;
}
当我运行它时,我得到:
root@images:~# ./root.flag
Enter the password :
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Wrong Password
Segmentation fault (core dumped)
它打印换行符,但不打印 str2 中存储的值。如何让 str2 打印到屏幕上?我对 C 相当陌生,所以我不知道为什么这不起作用。这可能是我忽略的一些简单的事情。
最佳答案
buff 只有 50 个字符,但你输入了 131 个“A”,所以你有未定义的行为。
另外,str2 只有 22 个字符,它需要是 23 个字符,并且应该以 '\0' NULL 结尾才能打印它。
事实上,您不应该在初始化之前对其进行 strcat'ing。尝试 str2[23] ={0};
函数 strcat(str, new) 查找字符串 str 的末尾以开始在 new 中添加字符。字符串的结尾定义为“\0”或零。您需要初始化局部变量以确保它们具有您想要的值。其代码为 str1[80]={0};
和 str2[23]={0};
关于c - 在 C 中用 printf 打印字符不会打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40311315/