evaulate strcmp true/false

This is a discussion on evaulate strcmp true/false within the C Programming forums, part of the General Programming Boards category; Please help. When I learned beginner C, I learned that while ( strcmp ( name, "xxx") != 0) { //do ...

  1. #1
    Unregistered
    Guest

    Cool evaulate strcmp true/false

    Please help. When I learned beginner C, I learned that

    while ( strcmp ( name, "xxx") != 0)
    {

    //do something

    }

    meant that as long as name is not equal to "xxx" it will return a
    number other than zero...meaning nonzero!=0 evaluating to true.
    Then entering the loop.


    Could someone explain how this code below evaluates the same way without using the !=0 ? I don't see how it is the same thing because it has to evaluate to true to enter the loop and it will evaluate to false as long as name doesn't equal "xxx" thus not entering the loop. Could someone help me uderstand how this example works the same as the first without using the != 0
    I would appreciate it. Thanks


    while ( strcmp ( name, "xxx" ))
    {

    //do something

    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    In C, 'true' is any non-0 integer value, and 'false' is 0.

    It might help to try something like this...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    // Untested
    int main (void)
    {
     char name[20];
     printf ("Boolean expression program\n");
    
     printf ("Gimme a word less than 20 chars long.\n");
     printf ("To quit, type in 'quit'\n");
     
     do {
      scanf ("%s", name);
    
      printf ("strcmp(name, 'quit') != 0  : %d\n", strcmp(name, "quit") != 0);
      printf ("strcmp(name, 'quit')  :  %d\n", strcmp(name, "quit"));
     } while (strcmp (name, "quit") != 0);
    
     return 0;
    }
    Last edited by QuestionC; 04-13-2002 at 04:13 PM.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    The while loop will execute as long as the expression evaluates TRUE (anything non-zero). strcmp() returns zero only if the two strings match, and non-zero if they are different.
    Code:
    while ( strcmp( "Hello", "World" ) )
    {
      /* This executes because strcmp returns non-zero (TRUE) */
    }
    There is no need to test if TRUE != FALSE :)
    Jason Deckard

  4. #4
    Unregistered
    Guest
    It makes perfect sense now. Thank you both.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 05:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 01:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21