Thread: return !(s == 1000); // returning some condition?

  1. #1
    Banned
    Join Date
    Mar 2008
    Posts
    78

    return !(s == 1000); // returning some condition?

    Can i do this?

    return !(s == 1000);

    I tested it and if it is false it returns 0, but if it is true it returns nothing... Why doesn't it return 1?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It does return 1. It will return 0 when s is 1000 and 1 otherwise.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    In this instance it would be vastly more readable to say return s != 1000;.

  4. #4
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by zx-1 View Post
    In this instance it would be vastly more readable to say return s != 1000;.
    lol, good point. What was i thinking when i did !( s==1000) ?? xD

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    a return statment will ALWAYS return something. The code you have above will return either 0 or 1 by definition of the C "boolean" values - even in standard C that doesn't have the "bool" type.

    Perhaps you can produce a small compilable example - but I guarantee you that the function returns and will return a value, whatever you do in a return statement [1] - the return value may be RUBBISH, but it will return SOMETHING that is compatible with the return type of the function.

    [1] For the pedants - it is of course also possible to write a return-statement that calls a function that never returns - in this case, the return statement doesn't complete, because the called function never returns. This is something other than what Milhas seems to indicate, which is that the function returns but it doesn't give the correct value back.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by tabstop View Post
    It does return 1. It will return 0 when s is 1000 and 1 otherwise.
    if i make printf it doesn't, but if i use a IF function that uses that return it works well.
    strange, printf bug?

  7. #7
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by matsp View Post
    a return statment will ALWAYS return something. The code you have above will return either 0 or 1 by definition of the C "boolean" values - even in standard C that doesn't have the "bool" type.

    Perhaps you can produce a small compilable example - but I guarantee you that the function returns and will return a value, whatever you do in a return statement [1] - the return value may be RUBBISH, but it will return SOMETHING that is compatible with the return type of the function.

    [1] For the pedants - it is of course also possible to write a return-statement that calls a function that never returns - in this case, the return statement doesn't complete, because the called function never returns. This is something other than what Milhas seems to indicate, which is that the function returns but it doesn't give the correct value back.

    --
    Mats
    It returns a value that is seen as "true" by any IF condition, but if i use printf to show that return as %i, it only shows when it is 0, it never shows 1.
    perhabs null = true?
    strange.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int test(int s) {
    	return !(s==1000);
    }
    
    int main(void) {
        printf("%i\n", test(999));
    	printf("%i\n", test(1000));
    }
    Code:
    silverlx:~$ ./temp2
    1
    0

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Please post a small compilable program, and no, it's not returning NULL for true - it will return the integer value 1 (assuming the return type is int).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Your right, it returns 1.

    Thanks for showing it to me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. Looking for feedback on program segment
    By avron in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 04:38 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  5. Need Help with a Bowling Score Program
    By oobootsy1 in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2005, 10:04 AM