0

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?

explorer
  • 127
  • 2
  • 3
  • 13

2 Answers2

6

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.

explorer
  • 127
  • 2
  • 3
  • 13
ouah
  • 142,963
  • 15
  • 272
  • 331
  • The declaration can't be at file scope (assuming the actual code is reasonably similar to what's in the question). – Keith Thompson Feb 06 '15 at 20:40
  • 1
    The code in your question *will not compile*. Please update your question; copy-and-paste the exact code that you're feeding to the compiler. Don't retype it. If you're using ``, then the `#include` directive should also be in the code in the question. We can't possibly guess which errors are in your original code and which were introduced when you summarized or re-typed it. – Keith Thompson Feb 06 '15 at 20:41
  • The problem in this is, I am using a dynamic value for the array. So I can't do arr[x] = {false} – explorer Feb 06 '15 at 20:44
  • @ouah: The code in the question has been changed again. – Keith Thompson Feb 06 '15 at 20:49
  • @ouah thanks for the updated answer. But the problem is, if I use = operator, I would need a for loop for a long long int, which is taking a lot of time. – explorer Feb 06 '15 at 20:51
  • 1
    @explorer use `memset(count, 0, sizeof count)` to have all array elements to `false`. – ouah Feb 06 '15 at 20:53
  • 1
    @explorer: Why do you expect a `for` loop to take a lot of time? Have you measured it? Do you need to initialize the array, or can you write the code that uses it so that it never accesses an element that hasn't been initialized? – Keith Thompson Feb 06 '15 at 21:00
  • I have time restrictions for the program. Trying to reduce a nested for loop. @KeithThompson – explorer Feb 06 '15 at 21:02
  • @explorer: That doesn't answer my question -- but since timing is beyond the scope of you're question, I'll just drop it. – Keith Thompson Feb 06 '15 at 21:04
  • I need to initialize the array. @KeithThompson – explorer Feb 06 '15 at 21:11
1

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].

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261