Reputation: 127
I have something like,
#include <stdio.h>
#include <stdbool.h>
int main()
{
int t,answer;
bool count;
long long int L;
scanf("%d",&t);
while(t>0)
{
answer = 0;
scanf(" %lld",&L);
bool count[L];
// .....restofthecode. NDA Constraint.
What would be the default value of all the elements of arr[x]
?
Is it false
always? Or true
? Or any random value?
Upvotes: 0
Views: 13898
Reputation: 145829
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.
Upvotes: 6
Reputation: 134316
As per your code, in local scope
boolean arr[x];
itself is invalid. x
is used uninitialized.
Just FYI, in global [file] scope, all the variables are initialized to 0
. In local scope, they simply contain garbage, unless initialized explicitly.
EDIT:
[After the edit] All the variables in the arr
array will have garbage value. It is in local scope [auto].
Upvotes: 1