Thread: What is the difference between int and bool and when do I use bool instead of int.

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    16

    What is the difference between int and bool and when do I use bool instead of int.

    Hi. I'm new to c++ and bought the Jumping in C++ ebook. I read about the bool type but was confused about what it is and when I used it.Please help me.

  2. #2
    Registered User
    Join Date
    Mar 2014
    Location
    Zagreb, Croatia, Croatia
    Posts
    15
    I am also new to C++, just crossing over from C and basically how I understood it bool is just true / false values instead of 1 / 0.
    In practice so far I never used it because you can implement simple integers and check for the value through if statement.

    Here is an example how you could do use it, in C you got srand() function which is used to initialize a semi-random number generator and you only need to do it once within your whole program.
    If you got that function within your own function something like "int getnumber(....);" and you need to initialize it only once else stuff my not go your way so to speak :P
    So you setup an external variable and set it to 0, first time you run srand() you set the variable to 1 and make an if statement that if it's "1" then it means generator has already been initialized and you just continue with
    your function/program.
    Code:
    external int initialized = 0 ;
    int getnumber(....) 
    {
    if(initialized != 1)
    {
    srand(time(NULL));
    initialized = 1;
    }
    /*continue your function*/
    }
    
    int main()
    {
    }
    Now you could do the same with bool just using true / false values instead of 1 / 0 but I never used it in practice so some1 else might explain it better :P

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Quote Originally Posted by etricity
    I read about the bool type but was confused about what it is and when I used it.
    A definition is given in Stroustrup's C++ glossary:
    bool - the built-in Boolean type. A bool can have the values true and false.

    So, if you have a scenario in which a boolean type makes sense, e.g., a flag or return value to denote yes/no or success/failure, you would consider using bool.

    Quote Originally Posted by Jablan
    In practice so far I never used it because you can implement simple integers and check for the value through if statement.
    You could, but then if you have multiple such values, the use of an enumerated type (or since C++11, an enum class) would be more appropriate. bool would not be appropriate because the values involved should be restricted to true/false.

    Quote Originally Posted by Jablan
    Here is an example how you could do use it, in C you got srand() function which is used to initialize a semi-random number generator and you only need to do it once within your whole program.
    If you got that function within your own function something like "int getnumber(....);" and you need to initialize it only once else stuff my not go your way so to speak :P
    So you setup an external variable and set it to 0, first time you run srand() you set the variable to 1 and make an if statement that if it's "1" then it means generator has already been initialized and you just continue with
    your function/program.
    This is a bad idea: it may be the case that the use of the function wants to seed with a particular value in order for repeatability in testing. Hence, getnumber should not seed, but rather it is up to the caller to seed, e.g., near the start of the global main function.

    Furthermore, if you really do want to do this, then it is precisely a case where the use of bool is better than your use of int because it is clear that initialized is either true or false, rather than say, 2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    16
    So I should use bool if there are only two possible values, like yes/no or success/fail and I should use int if there are more than two possible values. But what other values are there? And if you are using an if statement aren't there always only two possible values true/false? And can you please give me an example in coding in c++. Thank you for your help guys! I appreciate it!
    Last edited by etricity; 03-11-2014 at 03:12 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Like you've been told: if there is a situation which can be described with only true or false, you use a bool.
    Did we succeed? Yes or no.
    Is the individual female? Yes or no.
    Does this computer have more than 8 GB of ram? Yes or no.

    Example of where not to use bool:
    How much ram does this computer have?
    What is the gender of that individual?
    What is the status of that function?

    An int is an integer - a number. Use it whenever you have a number you must store. It's not more difficult than that.
    An if statement branches (i.e. chooses a path) depending on whether the expression is true or false.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2014
    Posts
    16
    Thank You so much guys!! I now understand the difference. Thanks!! You guys are the best!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOL bool ? unresolved external symbol
    By xwielder in forum C Programming
    Replies: 6
    Last Post: 05-20-2008, 08:39 AM
  2. How get bool's from std::vector<bool>
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2008, 05:24 AM
  3. bool
    By Scarvenger in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2005, 06:02 PM
  4. bool vs BOOL
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 04-10-2004, 09:55 AM
  5. Interesting: bool != BOOL
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-09-2002, 11:09 AM

Tags for this Thread