c After initializing the language string, tap for input/output of the rest of the file

Asked 2 years ago, Updated 2 years ago, 50 views

After initializing the string to '\0', I took the string and compared it, and I saw '\0' at the end of the string I received, and there are meaningless characters behind it. Is this meaningful? This task is to compare strings. I also read a string from the input and output of the file, where the meaningful part of the string comes after the tap, and then the meaningless characters. I wonder why this difference occurs. I want to compare the two strings through strcmp, but I don't get the result I want, so I'm asking.

c strcmp string

2022-09-22 13:17

1 Answers

$ cat main.c
#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[])
{
    char str1[128];
    char str2[128];

    str1[0] = '\0';
    str2[0] = '\0';
    printf("[%s]\n", str1);
    printf("[%d]\n", strcmp(str1, str2));

    return 0;
}
$ $ gcc main.c
$ $ ./a.out
[]
[0]

I don't quite understand the gist of the question. If you do it like this, it works well. You can also initialize as follows:

char str[128] = {0,};

I need to make sure you understand how to use strcmp clearly. Where did you get it from? It seems necessary to check if you finished it by inserting '\0' in length +1.


2022-09-22 13:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.