2

I output of below code is undefined when I ran multiple times. I would like to know why the output is undefined and what will be implication when I try to assign value to an array of unknown bound.

#include <stdio.h>

int main()
{
  int a[]= {};
  int num_of_elements;
  int count = 0;

  printf("Enter the number of elements:\n");
  scanf("%d",&num_of_elements);

  printf("Enter the numbers:\n");
  for(count=0; count<num_of_elements; ++count)
  {
    scanf("%d", &a[count]);
  }
  printf("\n");

  for(count=0; count<num_of_elements; count++)
  {
    printf("%d,   ", a[count]);
  }
  printf("\n");

  return 0;
}

Output when run at different times:

Enter the number of elements:
2

Enter the numbers:
1 2

0,   0,

Enter the number of elements:
3

Enter the numbers:
1 2 3

0,   0,   2,

Enter the number of elements:
4

Enter the numbers:
1 2 3 4

0,   0,   2,   3,
Segmentation fault

Enter the number of elements:
5

Enter the numbers:
1 2 3 4 5

0,   0,   2,   3,   4,
Segmentation fault
Simon
  • 3,667
  • 1
  • 35
  • 49
Vasanth
  • 21
  • 2
  • 1
    `int a[]= {};` is no use - you must define the array *after* you know its size. – Weather Vane May 31 '17 at 09:31
  • https://stackoverflow.com/questions/1677157/can-size-of-array-be-determined-at-run-time-in-c – msc May 31 '17 at 09:33
  • 1
    the posted code does not compile! It causes the compiler to output two messages: 1) `5:12: warning: ISO C forbids empty initializer braces [-Wpedantic]` and 2) `5:7: error: zero or negative size array 'a'` When compiling always enable the warnings. Then fix those warnings. (for `gcc`, at a minimum use: `-Wall -Wextra -pedantic` ) – user3629249 May 31 '17 at 16:00

2 Answers2

1

I would like to know why the output is undefined and what will be implication when I try to assign value to an array of unknown bound

The size of variable a will be 0, as num_of_elements has not been scanf'ed at that point, so you can't store anything in it.

The solution is to declare the array after you have read its size from the user. This means :

#include <stdio.h>

int main()
{
    int num_of_elements;
    int count = 0;

    printf("Enter the number of elements:\n");
    scanf("%d", &num_of_elements);

    //define here your array
    int a[num_of_elements];

    ...

    return 0;
}
Marievi
  • 4,951
  • 1
  • 16
  • 33
  • Only if `__STDC_NO_VLA__` is *not* defined as a macro identifier. – DeiDei May 31 '17 at 09:34
  • @DeiDei the OP does not mention such a macro definition in his program... That's why I did not mention this. – Marievi May 31 '17 at 09:36
  • Hi Marievi and others...Thanks for your inputs. Its clear now and thanks for introducing the C11 standards which gave me some insight – Vasanth May 31 '17 at 12:29
  • compiling this answer results in: 1) `12:5: error: variable sized objct may not be initialized.` 2) `12:30: warning: ISO C forbids empty initializer braces [-Wpedantic]` – user3629249 May 31 '17 at 16:05
1

As a first hint, when you try to compile this with gccusing -pedantic, it will refuse to compile:

$ gcc -std=c11 -Wall -Wextra -pedantic -ocrap.exe crap.c
crap.c: In function 'main':
crap.c:5:12: warning: ISO C forbids empty initializer braces [-Wpedantic]
   int a[]= {};
            ^
crap.c:5:7: error: zero or negative size array 'a'
   int a[]= {};
       ^

And indeed, the size of such a variable is 0, so you can't store anything in it.

Still it is a valid syntax and has its uses, for example as a "flexible array member" of a structure:

struct foo
{
    int bar;
    int baz[];
};

[...]

struct foo *myfoo = malloc(sizeof(struct foo) + 5 * sizeof(int));
// myfoo->baz can now hold 5 integers