使用printf打印C中的字符不打印

Chr*_*Man -2 c printf char

我有以下代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到:

root@images:~# ./root.flag

 Enter the password :
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

 Wrong Password







Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

它打印换行符但不打印str2中的值存储.如何让str2打印到屏幕上?我对C很新,所以我不知道为什么这不起作用.我很容易忽视它.

cle*_*anc 5

buff只有50个字符,但你输入131"A",所以你有不确定的行为.str2也只有22个字符,它需要为23,并且应该以'\ 0'NULL结束以打印它.事实上,在它初始化之前你不应该对它进行严格的处理.尝试str2[23] ={0};

函数strcat(str,new)查找字符串str的结尾,开始在new中添加字符.字符串的结尾定义为'\ 0'或零.您需要初始化局部变量以确保它们具有您想要的值.代码是str1[80]={0};str2[23]={0};