When you enter the string "enqueue 7" in a character array, the trailing zero is also added.
In this dam
for (int i = 0; i < 7; ++i) { TempLocalChar[i] = LocalChar[i]; }
The substring "enqueue" is copied without a descriptive zero. Therefore, the data if statements with strcmp call
if (strcmp(TempLocalChar, enqueue) == 1 ) { //Бла-бла-бла }
have undefined behavior, since the strcmp function compares only strings with a terminating zero (by the way, this is the definition of the string, that is, there must be a terminating zero)
You should after copying the substring will add a trailing zero. For example,
for (int i = 0; i < 7; ++i) { TempLocalChar[i] = LocalChar[i]; } TempLocalChar[7] = '\0';
Or you could compare strings using another standard function, namely memcmp , which allows you to explicitly specify how many characters are compared.
For example,
if ( memcmp( TempLocalChar, enqueue, 7 ) == 0 ) { //Бла-бла-бла }
By the way, if you want to check strings for equality, you need to write
if (strcmp(TempLocalChar, enqueue) == 0 ) { ^^^^ //Бла-бла-бла }
Keep in mind that the C standard does not guarantee that if the strings are not equal, then exactly 1 or -1 will be returned. The function can return any positive or negative value. Why such a comparison
if (strcmp(TempLocalChar, enqueue) == 1 ) { //Бла-бла-бла }
in any case is not correct. It would be correct to write, for example
if (strcmp(TempLocalChar, enqueue) > 0 ) { //Бла-бла-бла }
or
if (strcmp(TempLocalChar, enqueue) < 0 ) { //Бла-бла-бла }
Only returning 0 if the rows are equal is guaranteed.
Generally speaking, the use of magic numbers such as 7, used in a cycle, makes programs error prone, since it is not known what the user actually entered into the character array, whether there are more than 7 characters or less.
Also note that the gets function is not reliable and is no longer supported by the C standard. Instead, use the standard function fgets , which allows you to tell how much you can enter into an array of characters.
You can write
fgets( LocalChar, sizeof( localChar ), stdin );
This function adds the array also a newline character. You can remove it from the array as follows.
localChar[ strcspn( localChar, "\n" ) ] = '\0';
TempLocalCharvariable at the time ofstrcmp()call. And, judging by the code, it is clearly not what it seems. By the way, the value1indicates that the lines are not equal , and0- are equal . - PinkTux