Why Is a C++ Bool Var True by Default

Why is a C++ bool var true by default?

In fact, by default it's not initialized at all. The value you see is simply some trash values in the memory that have been used for allocation.

If you want to set a default value, you'll have to ask for it in the constructor :

class Foo{
public:
Foo() : bar() {} // default bool value == false
// OR to be clear:
Foo() : bar( false ) {}

void foo();
private:
bool bar;
}

UPDATE C++11:

If you can use a C++11 compiler, you can now default construct instead (most of the time):

class Foo{
public:
// The constructor will be generated automatically, except if you need to write it yourself.
void foo();
private:
bool bar = false; // Always false by default at construction, except if you change it manually in a constructor's initializer list.
}

What is the default boolean value in C#?

http://msdn.microsoft.com/en-us/library/83fhsxwc.aspx

Remember that using uninitialized variables in C# is not allowed.

With

bool foo = new bool();

foo will have the default value.

Boolean default is false

Using boolean values in C

From best to worse:

Option 1 (C99 and newer)

#include <stdbool.h>

Option 2

typedef enum { false, true } bool;

Option 3

typedef int bool;
enum { false, true };

Option 4

typedef int bool;
#define true 1
#define false 0

Explanation

  • Option 1 will work only if you use C99 (or newer) and it's the "standard way" to do it. Choose this if possible.
  • Options 2, 3 and 4 will have in practice the same identical behavior. #2 and #3 don't use #defines though, which in my opinion is better.

If you are undecided, go with #1!

Is C/C++ bool type always guaranteed to be 0 or 1 when typecast'ed to int?

Yes:

In C++ (§4.5/4):

An rvalue of type bool can be
converted to an rvalue of type int,
with false becoming zero and true
becoming one.

In C, when a value is converted to _Bool, it becomes 0 or 1 (§6.3.1.2/1):

When any scalar value is converted to
_Bool, the result is 0 if the value compares equal to 0; otherwise, the
result is 1.

When converting to int, it's pretty straight-forward. int can hold 0 and 1, so there's no change in value (§6.3.1.3).

How to assign true to a boolean variable in c language

You have to include the header file <stdbool.h>

In this file bool is defined as a macro and expands to the C standard unsigned integer type _Bool.

Any value that is not equal to 0 is converted to 1 and assigned to a variable of the type _Bool.

To set for example a variable to true You could just write

bool leapYear = 1;

The header file also contains macros for true and false.

Here is a demonstrative program

#include <stdio.h>
#include <stdbool.h>

int main(void)
{
bool leapYear = true;

printf( "leapYear = %u\n", leapYear );

return 0;
}

Its output is

leapYear = 1

If you do not want to include the header then you could just use the standard integer type _Bool.

For example

#include <stdio.h>

int main(void)
{
_Bool leapYear = 1;

printf( "leapYear = %u\n", leapYear );

return 0;
}

Or even the type int like

int leapYear = 1;

In C logical operations return integer 1 if an expression is true or 0 otherwise.

In C how much space does a bool (boolean) take up? Is it 1 bit, 1 byte or something else?

If you are referring to C99 _Bool try:

printf("%zu\n", sizeof(_Bool)); /* Typically 1. */

Note the standard says:

6.2.5

An object declared as type _Bool is large enough to store the values 0
and 1.

The size cannot be smaller than one byte. But it would be legal to be larger than one byte.

Is bool a native C type?

bool exists in the current C - C99, but not in C89/90.

In C99 the native type is actually called _Bool, while bool is a standard library macro defined in stdbool.h (which expectedly resolves to _Bool). Objects of type _Bool hold either 0 or 1, while true and false are also macros from stdbool.h.

Note, BTW, that this implies that C preprocessor will interpret #if true as #if 0 unless stdbool.h is included. Meanwhile, C++ preprocessor is required to natively recognize true as a language literal.

Why can bool and _Bool only store 0 or 1 if they occupy 1 byte in memory?

The C language limits what can be stored in a _Bool, even if it has the capacity to hold other values besides 0 and 1.

Section 6.3.1.2 of the C standard says the following regarding conversions to _Bool:

When any scalar value is converted to _Bool, the result is 0 if the value compares equal
to 0; otherwise, the result is 1.

The C++17 standard has similar language in section 7.14:

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a
prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false;
any other value is converted to true. For direct-initialization (11.6), a prvalue of type std::nullptr_t can
be converted to a prvalue of type bool; the resulting value is false.

So even if you attempt to assign some other value to a _Bool the language will convert the value to either 0 or 1 for C and to true or false for C++. If you attempt to bypass this by writing to a _Bool via a pointer to a different type, you invoke undefined behavior.

By Default, What Is A JavaScript Variables's Boolean Value?

Javascript is a dynamic language and there is nothing like a default boolean value.

When you define a variable without a value it's default value is always undefined:

var variable; // variable is undefined

So you have to set the value:

var variable = true;
// or
var variable = false;

If you want to toggle this boolean value, you can do the following:

variable = !variable;


Related Topics



Leave a reply



Submit