• 11
• 27
• 9
• 20
• 31

# Bool and GCC.

This topic is 3967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello!. I recently got Ubuntu installed as I wanted to try something different than Windows (still have a Windows installation in another partition)... Without the knowledge of the system to find (and install :P) a decent programming IDE I went a gave a try to the GCC. I noticed that some things are different here (maybe even more correct?) like no variable declarations into the for statements and such but well, there's something going different from what I expected. Take the code:
#include <stdio.h>

bool probando();

int main()
{
printf("%d", probando());
}
bool probando()
{
return true;
}


When compiling I get problems regarding bool and true... I tried and changed "Bool" for "int" and just worked but... Shouldn't bool be working on gcc?. What about true and false?. Should I use 1 and 0 for them?. I wish that there aren't a lot of differences more regarding gcc. Coming from DevC++ I can perfectly cope with using any text editor (wouldn't mind to get an IDE though) but I would find hard to re-learn lots and lots of things. Thanks.

##### Share on other sites
"bool" is a C++ construct. I believe you must either invoke gcc as g++ or use a command-line option (that I don't know offhand) to force it to compile C++, especially if the file in question ends with .c. The other things you've noticed further suggest you're letting the program compile according to C rules (such as the for loop issue).

Your code is a mishmash of C and C++ anyway (cstudio, no .h, is the C++ equivalent of the C stdio.h header).

##### Share on other sites
You're probably compiling it as C code.

What's your command-line to compile this?

##### Share on other sites
If you're using C (which that would certainly compile as), you should be aware that the built-in type bool (as opposed to a typedefed int named BOOL) was only introduced in C99. To enable this, pass the -std=c99 argument to GCC.

##### Share on other sites
You must use g++ instead of gcc to compile C++ applications. gcc is for C.

##### Share on other sites
Thanks a lot for all the really quick replies!.

In fact, my code is a mashup of C++ and C... Nothing I can help since that's the way they're teaching it to me: something about "This is plain C, no classes, no C++ exclusives". It seems that my teachers are plain wrong with that (either that or they just got used to work with "mixed" code).

The file was a C file (.c) extension, hence the stdio, time or stdlib libraries, the command line is just "gcc -o blah blah.c" so it should compile as C. Everything should be fine but if Bool is a C++ "exclusive" that would easily explain why the code just won't compile as C.

I'll just take a second look at the code and will start fixing things that aren't compilant with the C standard (that I know of, that is, what you just told me)... Maybe I'll try the C99 thing later but, in all honesty, I think that I'm not that much into programming so I can understand what everything means.

Well, it's almost like a second start ;). Thanks a lot!.

PS: Now that I think of it... I had a function that would pass an integer to an array of 8 booleans (to simulate binary flags)... I see that I'll need to change these booleans to integers to stay "C" but, will that affect the performance of my program?. I designed the algorithm like that to save memory (8 bools vs 8 ints) but I think now it is a good moment to ask if that's a good practice.

##### Share on other sites
No, the bool is valid C.

Where you were not adhering to a standard would be, if this was C++ code, then you should #include<cstdio> rather than stdio.h. However, since you say this is C, then it is not a problem.

To enable the 'bool' type, just pass in the command line argument to GCC that I pointed out earlier. This will switch GCC into C99 (as opposed to C89, which is what I believe it defaults to) mode. C99 contains much goodness -- like not having to declare variables at the start of a block!

However, if you do stick with C, and don't want to use C99, then you might as well #include<stdbool.h> and use the BOOL type contained in there. It's just a typedefed int, but you gain a great deal of context from switching to it.

EDIT: Oh, and 8 bools and 8 ints will almost certainly use the same amount of memory (a bool array is not treated as a bit array). If you're starting out, it's not necessarily worth explaining in detail, but you should be aware that the compiler will fiddle with the sizes of types to attempt to pad them to a 4-byte width for performance reasons. In general: micro-optimisations like this are wholly unnecessary.

##### Share on other sites
Quote:
 Original post by The_Marlboro_ManPS: Now that I think of it... I had a function that would pass an integer to an array of 8 booleans (to simulate binary flags)... I see that I'll need to change these booleans to integers to stay "C" but, will that affect the performance of my program?.

No, for two several reasons.
First, since an int is usually 4 bytes wide, we're talking about a maximal waste of memory of 31 byte. Please show me a computer where 31 bytes of memory usage is unacceptable. [lol]

Second, bools aren't 1 bit. They may be 1 byte or 4 byte or anything in between, I believe.

Third, a PC is *more* efficient when working with 32 bit values than it is with smaller ones (1 byte, for example)

Oh, and fourth, this is premature optimization. You don't know yet if a) you're going to have a problem with memory consumption or performance, and b) if these changes to the code would even help resolve that hypothetical problem.

Finally, the "good practice" is to pick the datatype that closest matches what you want to do.
If your variable represents a boolean value (true/false), then use a boolean. If it represents an integer number, use an int.

And if the language in question doesn't have a bool? Use whatever else you can think of instead. :D

##### Share on other sites
Just tried g++ and the programs would compile without a problem (didn't get to try and see if they worked as expected but I assume they do)... It seems that I have been doing C++ this whole time (C++ without classes, that is) and that I have been taught a mix of C++ syntax and old C functionalities... Or whatever, now this is a bit confusing but I wouldn't like to bore anyone with that (not to mention that it is pointless).

It is never too late to turn back but I guess I could always take a few steps ahead and start embracing C++. For now I will just try and finish the assignment the way I've been told :P.

And as for the bools, padding, memory consumption and miscellaneous things, thanks a lot for the tips. I took the habit of doing that kind of thing (wrapping to the closest value) when I started programming but never knew how computers would pad them and take larger bits of memory... I think that I will keep the method though, it seems clear enough to me.

Again, thanks a lot. I will make sure to look at what C89 and 99 means later.

##### Share on other sites
Quote:
 Original post by The_Marlboro_ManI will make sure to look at what C89 and 99 means later.

Nothing magical this time, I'm afraid. ;-) C89 is the 1989 revision of the C standard, C99 is the 1999 revision -- which contained significant modernising upgrades to the language.