Boolean returns in C

This is a discussion on Boolean returns in C within the C Programming forums, part of the General Programming Boards category; Hi hi, I'm writing a program that tests data we input then decides whether to proceed or stop with the ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    Boolean returns in C

    Hi hi, I'm writing a program that tests data we input then decides whether to proceed or stop with the program. This is a programming assignment that I've been working on for about a week, but I'm stuck on something :'(.

    Basically, I have three functions that must proceed and check out as true before any calculations are done.

    Code:
    bool getBounds(int lowB, int uppB)
    {
       if (lowB >=-25)
          if (lowB <=-1)
             printf("\nLB works");
               // This is where I want it to be known as TRUE
    
          else
             printf("\nLB doesnt work");
               // This is where it is false
    
       else
          printf("\nLB doesnt work");
              // This is where it is false
    
       if (uppB >=1)
          if (lowB <=25)
             printf("\nUB works");
               // TRUE\
       
          else
             printf("\nUB doesnt work");
                // This is where it is false
             
       else
          printf("\nLB doesnt work");
                // This is where it is false
    
    }
    Is what I have so far, I've tried returning 1 as True and 0 as False, but I recieve a syntax error when trying to compile. I cannot use exit() or abort().

    Does anyone have any possible tips that might point me in the right direction?

    Can I just return either a 1 or a 0?

    Thank you for any tips you can provide.

    I read through the two stickies (including the homework) and I do not believe I am violating anything. If I am I will be more than happy to rewrite it.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Use lot's more braces to make your intentions obvious.
    Like
    Code:
       if (lowB >=-25) {
          if (lowB <=-1) {
             printf("\nLB works");
          }
       }
    Also, your function needs a return statement if it's to return a value

    > Can I just return either a 1 or a 0?
    Yes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Prior to C99, there was no boolean type in C. You can get around it various ways. If you're using C99, you can include <stdbool.h> and use _Bool, or its macro bool. If you don't have a C99 compiler...
    Code:
    typedef enum { false, true } bool;
    /* perhaps... */
    #define bool int
    #define false 0
    #define true 1
    /* or maybe... */
    typedef int bool;
    /* etc... */
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    Thanks much, I'm going to give it another go soon. Cheers

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Is something like this the desired end result?
    Code:
    bool getBounds(int lowB, int uppB)
    {
       return lowB >= -25 && lowB <= -1 && uppB >=1 && lowB <= 25;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by quzah
    Prior to C99, there was no boolean type in C. You can get around it various ways. If you're using C99, you can include <stdbool.h> and use _Bool, or its macro bool. If you don't have a C99 compiler...
    Code:
    typedef enum { false, true } bool;
    /* perhaps... */
    #define bool int
    #define false 0
    #define true 1
    /* or maybe... */
    typedef int bool;
    /* etc... */
    Quzah.
    And if you'd like to know why none of these options are perfect solutions (hence the real need to add bool to the language) read this

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    We use the enum method where I work and indeed the following code will not pass code reviewing.

    Code:
    BOOL b = ( i == j );
    What we have to do is
    Code:
    BOOL b = ( i == j ) ? TRUE : FALSE;
    or with a helper macro

    Code:
    BOOL b = BOOL_VAL( i == j );
    Those solutions are rather annoying

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by iMalc
    And if you'd like to know why none of these options are perfect solutions (hence the real need to add bool to the language) read this
    There is a real bool. It's called _Bool, and was added in C99. You know, just like I mentioned in the first sentence of the text you quoted.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    I'm still having a little trouble figuring out what I'm supposed to do once I actually return either a true false statement. A false statement is supposed to let the program quit through main, while a true statement continues with all the checkpoints before donig calculations.

    I'll be at work on it, thanks for the help so far, I'm slowly being able to figure it out

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You should be able to do what you've described with an if statement:
    Code:
    if(!function()) return 1;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Laserve
    We use the enum method where I work and indeed the following code will not pass code reviewing.

    Code:
    BOOL b = ( i == j );
    What we have to do is
    Code:
    BOOL b = ( i == j ) ? TRUE : FALSE;
    or with a helper macro

    Code:
    BOOL b = BOOL_VAL( i == j );
    Those solutions are rather annoying
    Aye. I prefer plain 1 or 0, as opposed to silly macros.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    Thanks for the help y'all. I have this portion figured out now and working on another. Y'all have been a great help!

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. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  3. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  4. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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