C - strcmp related to an if statement

20,440

Solution 1

Because strcmp() will return a negative integer in this case.

So change this:

if (strcmp(one, two) == 1) {

to this:

if (strcmp(one, two) != 0) {

to take into account all the cases that the strings differ.

Notice that you could have spotted that yourself by either reading the ref or by printing what the functions returns, like this:

printf("%d\n", strcmp(one, two));
// prints -23

Solution 2

According to the C Standard (7.23.4.2 The strcmp function)

3 The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.

So what you need is to write the if statement like

if ( strcmp(one, two) != 0 ) {

or

if ( !( strcmp(one, two) == 0 ) ) {

Solution 3

strcmp returns zero when both strings are equal, it returns something other than zero when they differs, so you need to change your if in your code to something like this

if ( strcmp(one, two) != 0 ) {
    printf("hello world\n");
}

Solution 4

The correct behaviour is:

if (strcmp(one, two) != 0) {
    printf("hello world\n");
}

Actually, this function returns the difference between two strings:

  • < 0: the first character that does not match has a lower value in ptr1 than in ptr2.
  • 0: the contents of both strings are equal
  • > 0: the first character that does not match has a greater value in ptr1 than in ptr2.

This is an example of how strcmp could be implemented

Solution 5

You misunderstood how strcmp works. To test if strings are different use

if(strcmp(one, two))
Share:
20,440

Related videos on Youtube

scugn1zz0
Author by

scugn1zz0

I'm a self learner, I've started from python 3.5 basic and as soon as I will be able I would like to learn web penetration testing.

Updated on March 28, 2020

Comments

  • scugn1zz0
    scugn1zz0 about 4 years

    In the code below I use strcmp to compare two strings and make this comparison the condition of an if statement. With the code below, the output will be hello world, because string "one" is equal to string "two".

    #include <stdio.h>
    #include <string.h>
    
    char one[4] = "abc";
    char two[4] = "abc";
    
    int main() {
    
        if (strcmp(one, two) == 0) {
            printf("hello world\n");
        }
    }
    

    Now I want to change the program, and make it print hello world if the two string are different so I change the program that way:

    #include <stdio.h>
    #include <string.h>
    
    char one[4] = "abc";
    char two[4] = "xyz";
    
    int main() {
    
        if (strcmp(one, two) == 1) {
            printf("hello world\n");
        }
    }
    

    I dont understand the reason why it does not print out anything.

    • AntonH
      AntonH about 7 years
      Change if (strcmp(one, two) == 1) to if (strcmp(one, two) != 0).
    • scugn1zz0
      scugn1zz0 about 7 years
      @EdHeal you are perfectly right, I was so sure that it returns 1 if strings are not equal, sorry, next time it will be the first thing i will do