How to declare a boolean array in C++?

I want to create a boolean array whose size is 31623 and all values assigned to 0. However for this code when i print some values are different than 1 or 0. Am I wrong anywhere in declaration or printing???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{
     bool arr[31623] = {false};
     for (int i = 0; i < 31623; i++) {
         cout << arr[i] << endl;
     }

     return 0;
}

Last edited on
a bool is defined as
false: zero.
true: not-zero.

Depending on your compiler you may get some non-1 values in there because bool is not necessarily 1. Here is an idea if you want only 1 or 0:

cout << (arr[i] ? 1 : 0) << endl;

I tried this out with VS2010 and it works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{
     bool arr[31623] = {false};
	 for (int i = 0; i < 31623; i++)
		 arr[i] = (i%2==0);

     for (int i = 0; i < 31623; i++)
         cout << arr[i]?1:0;

     return 0;
}
Last edited on
Topic archived. No new replies allowed.