比较相同字符长度的字符串时出现意外输出

Unexpected output when strings of same character length are compared

我的目标是创建一个程序,通过将用户输入与已声明的字符串进行比较来验证凭据(使用嵌套 ifs)。该程序按预期工作,直到我将传递更改为“12345”或任何 5 位数的字符串。然后输出正确的凭据是 Incorrect user ID and password。我假设这个语义错误是由于字符串的存储方式而发生的,但由于我缺乏基础知识,我仍然不确定。这可能是一个多余的问题,但由于这是我在 Whosebug 上的第一个真正的问题,我希望每个人都会 lenient/tolerant。如果我的问题不清楚,我很乐意提供澄清。如果我无法正确构图 title/question,我也深表歉意。

#include <stdio.h>
#include <string.h>

int main() {
    
    char name[10] = "k", pass[5] = "1234", input_id[10], input_pass[5];
    printf("Enter user ID: ");
    scanf("%s", input_id);
    printf("Enter user pass: ");
    scanf("%s", input_pass);
    
    if (strcmp(name, input_id) == 0) {
        if (strcmp(pass, input_pass) == 0) {
            printf("Accept");
        } else {
            printf("Incorrect User ID or Password");
        }
    } else {
        printf("Incorrect User ID or Password");
    }
    return 0;
}

字符串终止符号还需要一个字符。我相应地调整了代码。还要注意使用更安全的功能来限制输入长度。还要检查是否有输入。

用户名和密码的编程处理是一个非常高级的主题。你可以从中学到很多东西。您已经迈出了第一步,祝您旅途愉快。 :-)

#include <stdio.h>
#include <string.h>

int main(int argc, char** argv) {
    
    char user_id[11] = "k", pass[6], input_user_id[20], input_pass[20];
    // next line is same as: pass[6] = "12345"
    pass[0] = '1'; pass[1] = '2'; pass[2] = '3'; pass[3] = '4'; pass[4] = '5'; pass[5] = '[=10=]';
    int len_user_id = strlen(user_id);
    int len_pass = strlen(pass);

    printf("Enter user ID (max. 10 characters): ");
    char* user_id_value_string = fgets(input_user_id, sizeof input_user_id, stdin);
    if (user_id_value_string == NULL) {
        printf("Error: No user ID entered!\n");
        return 1;
    }
    unsigned long len_user_id_input = strlen(user_id_value_string);
    if (user_id_value_string[len_user_id_input - 1] == '\n') user_id_value_string[len_user_id_input - 1] = 0;
    printf("user ID: %s\n", user_id_value_string);
    printf("user ID length: %lu\n", strlen(user_id_value_string));

    printf("Enter user pass (max. 5 characters): ");
    char* pass_value_string = fgets(input_pass, sizeof input_pass, stdin);
    if (pass_value_string == NULL) {
        printf("Error: No password entered!\n");
        return 1;
    }
    unsigned long len_pass_input = strlen(pass_value_string);
    if(pass_value_string[len_pass_input - 1] == '\n') pass_value_string[len_pass_input - 1] = 0;
    printf("pass: %s\n", pass_value_string);
    printf("pass length: %lu\n", strlen(pass_value_string));
    
    if (strlen(user_id_value_string)  == len_user_id
        && strncmp(user_id, input_user_id, len_user_id) == 0 ) {
        if (strlen(pass_value_string) == len_pass
            && strncmp(pass, input_pass, len_pass) == 0) {
            printf("Accept\n");
        }
        else {
            printf("Incorrect User ID or Password\n");
        }
    }
    else{
        printf("Incorrect User ID or Password\n");
    }
    return 0;
}
$ gcc passcheck.c
$ ./a.out        
Enter user ID (max. 10 characters): k
user ID: k
user ID length: 1
Enter user pass (max. 5 characters): 12345
pass: 12345
pass length: 5
Accept
$ ./a.out        
Enter user ID (max. 10 characters): kevin
user ID: kevin
user ID length: 5
Enter user pass (max. 5 characters): 12345
pass: 12345
pass length: 5
Incorrect User ID or Password
$