2

code link

#include <stdio.h>

int main(void) {
    // your code goes here
    int a = 2;
    int b = 3;
    int c;
    c = a + b;
    int arr[c];
    arr[5] = 0;
    printf("%d",arr[5]);
    return 0;
}

Output is 0

How is it that at runtime it is taking the array number ? Is it a new feature ?

2
  • 2
    Is a feature added last century "new" ? Commented Jan 24, 2017 at 9:20
  • Is it because you already initialized it? because if you were doing something like printf( "%d" ,arr[ 4 ]); you would get an error Commented Jan 24, 2017 at 9:23

3 Answers 3

4

This is a variable length array. They were introduced in the 1999 revision of the C standard.

Sadly support for them came in slowly, so much that the 2011 revision made them an optional feature (but they are still standardized) 1.

Despite looking cool, they have a major caveat. They can cause you to overflow the call stack if the size is "too big". As such, care needs to be taken when using them.


1 Some compiler vendors were resistant, so it was made optional to appease them. Microsoft is an entire case study of this.

Sign up to request clarification or add additional context in comments.

2 Comments

The reason for making it optional is because the "support for them came in slowly"? Any reference?
@P.P. - That's the feeling I get. Some compiler vendors were resistant, so it was made optional to appease them. Microsoft is an entire case study of this.
2

This feature (Variable length array) has been introduced in C99. But currently this still is a compiler-dependent behavior. Some compiler(like gcc) supports it. Some(like msvc) doesn't.

BTW, arr[5] in your code, is out of range. Last element should be arr[4].

2 Comments

This is standard C. Not an extension (although it is compiler dependent since Microsoft were very slow ti implement C99)
@StoryTeller I was not aware of that. I did some digging after see your comment, and update link in my answer. :-)
-1

don't be confused in (static/fixed memory allocation) & (dynamic memory allocation) concepts :)

Let me clear your concept bro.

Relevant to following question,

                           C supports two type of array. 

1.Static Array - are allocated memory at "COMPILE TIME".

2.Dynamic Array - are allocated memory at "RUN TIME".

Ques. how to determine if an Array is static or dynamic?

Ans. Array declaration syntax:-

         int array_Name[size];     //size defines the size of block of memory for an array;

So, coming to the point-->

Point 1. if size is given at compile time to array, it's a "Static Memory Allocation". It is also called "fixed size memory allocation" because size is never changed. It's the LIMITATION of ARRAY in C.

ex. int arr[10]; //10 is size of arr which is staticly defined int brr[] = {1000, 2, 37, 755, 3}; //size is equal to the no. of values initilizes with.

point 2. If size is given at compile time to array, it's a Dynamic Memory Allocation. It is achieved by malloc() function defined in stdlib.h .

Now, its's the clarification of your code :-

 #include <stdio.h>

    int main(void) {
        // your code goes here
        int a = 2;
        int b = 3;
        int c;
        c = a + b;             //c is calculated at run time
        int arr[c];           //Compilor awaiting for the value of c which is given at run time but,
        arr[5] = 0;           //here arr is allocated the size of 5 at static(compile) time which is never be change further whether it is compile time in next statements or run time.
        printf("%d",arr[5]);  
        return 0;
    }

So, array(of size 5) holds value 0 at arr[5].
and ,other array indexes still show Garbage Values.

Hoping, you'll be satisfy with this solution to your problem :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.