There is no type named boolean
in C but there is _Bool
and in stdbool.h
a macro bool
that expands to _Bool
.
#include <stdbool.h>
#define X 42
bool arr[X];
arr
elements have an initial value of false
(that is 0
) if declared at file scope and indeterminate if declared at block scope.
At block scope, use an initializer to avoid the indeterminate value of the elements:
void foo(void)
{
bool arr[X] = {false}; // initialize all elements to `false`
}
EDIT:
Now the question is slightly different:
long long int x;
scanf("%lld",&x);
bool arr[x];
This means arr
is a variable length array. VLA can only have block scope, so like any object at block scope it means the array elements have an indeterminate value. You cannot initialize a VLA at declaration time. You can assign a value to the array elements for example with =
operator or using memset
function.