-1

as much as i know the next code should not run.

The first problem is declaring 'int i' inside the loop and not at the start code.

The next problem is the array size is defined in run time and therefor we must use dynamiclly allocation.

I was expected to get "segment fualt".

I run it with the command:

gcc -Wall commandLineArgument.c -o ex1

int main(void){
  int size=0;
  printf("enter number:\n");
  scanf("%d",&size);
  printf("The size is: %d\n",size);
  int arr[size];
  for(int i= 0;i<size;i++)
  {
    arr[i] = 5;
  }

  for(int i= 0;i<size;i++)
  {
    printf("%d,",arr[i]);
  }  
  printf("\n");
  return 0;}

edit if variable length arrays have been supported in C since C99 then how the update code running?

gcc -Wall -std=c89 commandLineArgument.c -o ex1 

int main(void){
int i;
int size=0;
printf("enter number:\n");
scanf("%d",&size);
printf("The size is: %d\n",size);
int arr[size];
printf("The size of arr: %lu\n",sizeof(arr));
for(i= 0;i<size;i++)
{
    arr[i] = 5;
}

for(i= 0;i<size;i++)
{
    printf("%d,",arr[i]);
}    
printf("\n");
return 0;}
dids
  • 35
  • 5
  • 2
    Possible duplicate of [Store Array Values from User Defined Array Lengths](http://stackoverflow.com/questions/18951151/store-array-values-from-user-defined-array-lengths) – haider_kazal Dec 22 '16 at 09:30
  • 2
    and so what is your question? – artm Dec 22 '16 at 09:30
  • For the first part, it is allowed in C but not in C++. For the second part, each defined `i` has the scope of the corresponding loop. – A.S.H Dec 22 '16 at 09:30
  • For the second code, add `-pedantic` to the gcc command line and it will warn about variable length arrays and mixed declarations/code. – dbush Dec 22 '16 at 14:58

2 Answers2

1

This is anachronistic: your code has been valid C since and including C99.

Since at least C99 you are able to declare i locally in the for loop.

And variable length arrays have been supported in C since C99. (Note that this is not supported even in C++17).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • It has *automatic storage duration*; that's all the standard says. I guess you can think of it as being on the stack. – Bathsheba Dec 22 '16 at 09:38
  • 1
    The C standard doesn't have a notion of a *stack*. Which is why I'm being careful not to really talk about it. I'm not convinced it *must* be stored in the same place as a fixed size array of the same length as `size`. Perhaps ask that as a question, tagging it C and language-lawyer? – Bathsheba Dec 22 '16 at 09:39
  • so if c support this version from c99, why should we using the dynamcily allocated and worry allocation free? – dids Dec 22 '16 at 09:45
  • Because dynamic storage is still useful. Automatic variables are automatically scoped. – Bathsheba Dec 22 '16 at 09:58
1

It looks like you are using an old version of gcc.

The two features you are trying to use were added in C99. The default for gcc versions older than version 5 is to compile using the old C89 standard. To use features added in later standards you need to explicitly tell older versions of gcc to use a newer standard.

The compiler option -std=c11 instructs gcc to use the current C11 standard. The minimum you need is -std=99.

I would recommend that you tell gcc to use the current standard:

 gcc -Wall -std=c11 commandLineArgument.c -o ex1

Another way to solve your problem is to upgrade to gcc v5 or newer.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82