-6

I'm having problems using strncmp. As I read, theorically strncmp should return 0 if the characters compared of two strings are equal; however, when I do the comparation, the code misbehaves and makes a false positive (Not being equal the characters, still makes the if clause). Here the code:

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

int main(){
    char *frase1="some string";
    char *frase2="another string";
    char *frase3="some other string";

//Comparar frases desde inicio
    if(strncmp(frase1, frase2, 200))printf("1<->2, 200 characters\n");
    if(strncmp(frase1, frase3, 20))printf("1<->3, 20 characters\n");
    if(strncmp(frase1, frase3, 4))printf("1<->3, 4 characteres\n");

    return 0;
}

If the strings are equal (At least the compared characters), they should print the message; if not, do nothing; so I still don't understand why the first Condition becomes true.

Any ideas?

LihO
  • 41,190
  • 11
  • 99
  • 167
DaniWein
  • 43
  • 1
  • 5
  • 1
    "As I read, theorically strncmp should return 0 if the characters compared of two strings are equal." Indeed. Your problem is the belief that `0` is considered to be true. – David Heffernan Mar 26 '14 at 10:44
  • Please read this: http://ericlippert.com/2011/04/25/something-wrong-with-the-universe/ – Eric Lippert Mar 26 '14 at 15:16

5 Answers5

1

strcmp & strncmp functions return 0 if strings are equal. You should do:

if (strncmp(frase1, frase3, 4) == 0) ...

i.e.:

char *str1 = "Example 1";
char *str2 = "Example 2";
char *str3 = "Some string";
char *str4 = "Example 1";

if (strncmp(str1, str2, 7) == 0) printf("YES\n");  // "Example" <-> "Example"
else printf("NO\n");

if (strncmp(str1, str3, 2) == 0) printf("YES\n");  // "Ex" <-> "So"
else printf("NO\n");

if (strcmp(str1, str4) == 0) printf("YES\n");      // "Example 1" <-> "Example 2"
else printf("NO\n");

yields YES, NO, YES.

LihO
  • 41,190
  • 11
  • 99
  • 167
0

If the strings are equal (At least the compared characters), they should print the message

No, if the strings are equal they should NOT print the message since strncmp returns 0 on equality. Since the last check returns 0 it doesn't get printed while the other two are non-zero i.e. unequal thus you see them getting printed.

strncmp's documentation should give you more clarity. It returns

  • 0 when lhs == rhs
  • < 0 when lhs < rhs
  • > 0 when lhs > rhs
legends2k
  • 31,634
  • 25
  • 118
  • 222
0

The strncmp() function returns zero if the strings are equal, and a non-zero value otherwise. So if you compare two strings that are not similar you get a non-zero value, and non-zero values are TRUE as far as if statements are concerned. So what you need to do is:

   if(!strncmp(frase1, frase2, 200))printf("Mismo insulto no creo\n");
Bob
  • 2,586
  • 7
  • 20
  • 33
0

you should make the strncmp() == 0 as the expression to be evaluated

working code:

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

int main(){
char *frase1="some string";
char *frase2="another string";
char *frase3="some other string";

//Comparar frases desde inicio
if(strncmp(frase1, frase2, 200) == 0)
   printf("1<->2, 200 characters\n");
if(strncmp(frase1, frase3, 20) == 0)
   printf("1<->3, 20 characters\n");
if(strncmp(frase1, frase3, 4) == 0)
   printf("1<->3, 4 characteres\n");

return 0;
}
ChrisF
  • 134,786
  • 31
  • 255
  • 325
Ankit Kumar
  • 1,433
  • 1
  • 16
  • 24
0

One, it's considered good practice to keep strings and identifiers in English when posting example code. Readability trumps fun.

Two, if blocks get executed if the condition is true. A value of 0 is considered false, while any value other than 0 is considered true.

I.e., your problem is not strncmp(), but a confusion about true and false.

DevSolar
  • 67,862
  • 21
  • 134
  • 209