crazy doubt!

This is a discussion on crazy doubt! within the C++ Programming forums, part of the General Programming Boards category; I was wondering tht the normal way of doing things is, if some test passes then the value is true. ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    41

    crazy doubt!

    I was wondering tht the normal way of doing things is, if some test passes then the value is true. Say for e.g if u use functions like isalpha() or isdigit() etc.. then the value returned is true if the character is an alphabet or a digit...

    Y then string functions like strcmp(s1, s2) return true if the strings are not equal? I keep wondering y they designed the string functions like that?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Why? I don't know, but I think with strcmp(), the 'true' value is 0, so it may imply that there are 0 differences between the first and second string, but they're the only ones I know of which do that.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Y then string functions like strcmp(s1, s2) return true if the strings are not equal?
    They don't. They return one of three values depending on the relationship between the two strings. The value is negative if the first string is lexographically less than the second, 0 if the strings are equal, and positive if the first string is lexographically greater than the second. The result does not represent a boolean value, so thinking in terms of true and false will only confuse you.
    My best code is written with the delete key.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Mind you, strcmp() returns one of 3 possible values. It doesn't test only for one of two.

    int result = strcmp(string str1, string str2);

    if str1 less than str2, returns a negative value
    if str1 = str2, returns 0
    if str1 > str2, returns a positive value

    Now, if you look at similar functions that return one of three possible values, you will find that the same convention follows. And also, they don't return bool, for the obvious reasons.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    41
    Thanks . solves my problem...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 01:28 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Doubt abt Storage!
    By kalamram in forum C Programming
    Replies: 1
    Last Post: 04-21-2006, 05:30 AM
  4. crazy
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 08-31-2002, 08:54 PM
  5. Greatest C++ Doubt
    By vasanth in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2002, 03:41 AM

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