C Boolean

This is a discussion on C Boolean within the C Programming forums, part of the General Programming Boards category; Hi, i am kind of new to c programming. So, i here want to get a clarification about boolean. here ...

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    C Boolean

    Hi, i am kind of new to c programming. So, i here want to get a clarification about boolean.

    here is the part of the code:
    Code:
    scanf ("%s\n", name);
    if (strcmp(name, "David") == 1)
    printf (" You are David");
    this code seem to be wrong according to the tutorial i learnt. What it wrote was (strcmp(name, "David") == 0). please explain to me. I understood that =0 means false. Therefore, it supposed to be 1.


    Here is my second question :
    Code:
    char name[10] = "David";
    
               OR
    
    char name[10];
    strcpy(name, "David");
    which is the correct way to assign a string to an array?

    by the way, my programming tutorial taught me to use the second one. It did not mention about the first one.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,639
    >> I understood that =0 means false.

    Zero doesn't always (and in fact, quite often does not) mean false, as non-zero results are useful for returning error codes, and such. But more importantly, you should *always* look up the documentation for a function before ever using it. That way, you know what it returns, how to interpret it, what sort of format it expects for it's input parameters, and so forth.

    >> which is the correct way to assign a string to an array?

    Either, but the second is error-prone, as it is susceptible to buffer overflows. Look up 'strncpy' - it's much safer.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    493
    1)
    Code:
    strcmp(name, "David") == 0
    is correct. you can check what strcmp returns here.
    2)Both are correct.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by valthyx View Post
    Hi, i am kind of new to c programming. So, i here want to get a clarification about boolean.

    here is the part of the code:
    Code:
    scanf ("%s\n", name);
    if (strcmp(name, "David") == 1)
    printf (" You are David");
    this code seem to be wrong according to the tutorial i learnt. What it wrote was (strcmp(name, "David") == 0). please explain to me. I understood that =0 means false. Therefore, it supposed to be 1.


    Here is my second question :
    Code:
    char name[10] = "David";
    
               OR
    
    char name[10];
    strcpy(name, "David");
    which is the correct way to assign a string to an array?

    by the way, my programming tutorial taught me to use the second one. It did not mention about the first one.
    1. strcmp(name, "David") == 0 simply means that both strings are equal. Here 0 is not taken in the true-false sense. Actually the string stored in "name" and "David" are compared character by character until there is a mismatch or end of one of the strings is reached, whichever occurs first. Ifthe two strings are identical strcmp() returns a value zero. If they're not , it returns the numeric difference between the ASCII values of the first non-matching pairof characters.

    2. Both of them are same. But the use of first one is advisable as the other one is error-prone as Sebastiani pointed out.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    1337
    Join Date
    Jul 2008
    Posts
    135
    Well, ok.. Let me give you an example
    Here is what you said:
    s
    scanf ("%s\n", name);
    if (strcmp(name, "David") == 0)
    printf (" You are David");
    I have read the link and i knew that it is some kind of a rule for strcmp().

    anyway, look at this
    scanf ("%s\n", name);
    if (name == "David")
    printf ("You are David");
    else
    printf (" you are not David");
    in order to get the if() statement to be printed, the value has to be equal to an integer which is not 0. Therefore, if (name == "David") is definitely not 0.

    I do not understand why strcmp() function is such.



    AND

    why the second assignment of a string is error-prone since I have initialised the name to 10 characters (name[10]). Or it does not work that way? Sorry, i am new to C, thanks for helping me.
    Last edited by valthyx; 05-31-2009 at 11:29 PM.

  6. #6
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    493
    anyway, look at this
    Quote:
    scanf ("%s\n", name);
    if (name == "David")
    printf ("You are David");
    else
    printf (" you are not David");
    in order to get the if() statement to be printed, the value has to be equal to an integer which is not 0. Therefore, if (name == "David") is definitely not 0.

    I do not understand why strcmp() function is such.
    Well, == operator cannot be used for comparison of strings. you should use a function like strcmp() to check for equality between strings.
    Edit:
    The second assignment is error prone if the source string(2nd arg of strcpy()) has a larger size than that of destination string(1st arg) which results in a security risk known as "buffer overflow vulnerability".
    Last edited by stevesmithx; 05-31-2009 at 11:43 PM.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by valthyx View Post
    AND

    why the second assignment of a string is error-prone since I have initialised the name to 10 characters (name[10]). Or it does not work that way? Sorry, i am new to C, thanks for helping me.
    On supplying the base addresses, strcpy() goes on copying the characters in source string into the target string till it encounters the end of source string('\0'). It is thus our responsibility to see to it that the target string's dimension is big enough to hold the string being copied into it. In your case it is perfectly alright but at other times it may not.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,639
    >> if (name == "David")

    That isn't going to work as you expect it to. You are basically comparing the addresses of the two variables.

    >> I do not understand why strcmp() function is such.

    strcmp reports the lexical ordering of the strings, comparing each string, character by character. It might be implemented like so:

    Code:
    int strcmp( const char* lhs, const char* rhs )
    {
    	for( ; *lhs != 0 && *rhs != 0; ++lhs, ++rhs)
    	{
    		if( *rhs < *lhs )
    			return 1;
    		if( *lhs < *rhs )
    			return -1;
    	}
    	if( !!*lhs ^ !!*rhs )
    		return *lhs ? 1 : -1; 
    /*
    	Equal
    */
    	return 0;
    }
    Not exactly the most readable example (and it may not be 100% correct), but the point is, it's real purpose is ordering, and so testing for equality is just a side-effect.
    Last edited by Sebastiani; 06-01-2009 at 12:09 AM. Reason: forgot increment, simplification
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  9. #9
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    493
    >> if (name == "David")

    That isn't going to work as you expect it to. You are basically comparing the addresses of the two variables.
    two variables?
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,639
    Ok, well the variable and the invariable.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by valthyx View Post
    in order to get the if() statement to be printed, the value has to be equal to an integer which is not 0. Therefore, if (name == "David") is definitely not 0.

    I do not understand why strcmp() function is such.
    You're thinking as though the function were named "strequ", which would logically return 1 if they were equal and 0 otherwise.
    That is not what strcmp does and not what it is for. the cmp bit means, tell me how these items compare logically. You need to read the documentation for it on the web. It typically says something like this:
    • If it returns < 0 then string a is less than string b
    • If it returns 0 then string a is equal to string b
    • If it returns > 0 then string a is greater than string b
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    1337
    Join Date
    Jul 2008
    Posts
    135
    Owh..Thanks a lot guys, I understand now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  2. Casting boolean as string
    By doofusboy in forum C Programming
    Replies: 11
    Last Post: 11-10-2005, 11:24 AM
  3. Replies: 4
    Last Post: 04-02-2004, 06:30 PM
  4. Working with boolean...
    By CompiledMonkey in forum C Programming
    Replies: 4
    Last Post: 11-03-2003, 09:39 AM
  5. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM

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