Hello. The strcmp function does not work correctly for me. Code:

int Part1(int* GlobalDigit, int Count, int* sptr) { char LocalChar[30]; char Temp[2]; char TempLocalChar[8]; char* enqueue = "enqueue"; char *dequeque = "dequeque"; char *done = "done"; //TempLocalChar[7] = '/0'; int LocalDigit; int LocalCount; int FuncAnsw; int Check = 0; rewind(stdin); gets(LocalChar); for (int i = 0; i < 7; ++i) { TempLocalChar[i] = LocalChar[i]; } if (strcmp(TempLocalChar, done) == 1) { //Бла-бла-бла } if (strcmp(TempLocalChar, enqueue) == 1 ) { //Бла-бла-бла } if (strcmp(TempLocalChar, dequeque) == 1) { //Бла-бла-бла } if (Check == 0) { //Бла-бла-бла } } 

If you enter "enqueue 7" strcmp(TempLocalChar, enqueue) returns 1. (the rest - 0)

If you enter "dequeque" strcmp(TempLocalChar, dequeque) returns 1. (the rest - 0)

When you enter done, strcmp(TempLocalChar, done) returns ZERO. ( strcmp(TempLocalChar, enqueue) returns 0. strcmp(TempLocalChar, dequeque) returns ONE.)

Do not tell me how to fix?

  • Fixing is very simple: you need to look at what is actually in the TempLocalChar variable at the time of strcmp() call. And, judging by the code, it is clearly not what it seems. By the way, the value 1 indicates that the lines are not equal , and 0 - are equal . - PinkTux
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

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'; 
     gets(LocalChar); for (int i = 0; i < 7; ++i) { TempLocalChar[i] = LocalChar[i]; } 

    Let's see what happens in the TempLocalChar line when entering "enqueue 7" :

     "enqueueКАКОЙ-ТО-МУСОР" 

    So, strcmp(TempLocalChar, "enqueue") will return 1 (or maybe -1 ), which is correct.

    Entering "dequeque" :

     "dequequКАКОЙ-ТО-МУСОР" 

    So, strcmp(TempLocalChar, "dequeque") will return 1 (or maybe -1 ), which is also correct.

    Enter "done" :

     "done\0КАКОЙ-ТО-МУСОР" 

    And strcmp(TempLocalChar, "done") will return 0 , this is also correct.

    Thus, not the strcmp() operation is strcmp() , but this is a statement:

    Incorrect operation of the strcmp function