# Thread: What is size of array

1. ## What is size of array

What would be size of int a[4];

Code:
```#include<stdio.h>int main ()
{
int a[4];
int n = sizeof(a);
printf (" Size of array %d ", n);
return 0;
}```
integer take 4 byte in my machine so each element take 4 byte to store data. This array can store five element a[0] a[1] a[2] a[3] a[4]

I think the size of array should be 4 *5 = 20 bytes but program show size of array is 16 bytes

What would be size of int a[4];

2. No, this array can store four elements, a[0], a[1], a[2] and a[3]. When you say "int a[4];", you're asking for an integer array with 4 elements, not with a maximum index of 4.

3. Originally Posted by GReaper
No, this array can store four elements, a[0], a[1], a[2] and a[3]. When you say "int a[4];", you're asking for an integer array with 4 elements, not with a maximum index of 4.
What happen in this case when int a[4];

How much size array take 16 bytes because int take one byte to one one integer value so this array will store 4 integer that mans it will take 16 bytes

What happen in this case when
Code:
` int a[4] = {1,2,3,4};`

I think it also take 16 bytes

what would be the size of array for
Code:
` int a[4] = {1,2,3,4}`

what would be the size of array for int a[4];
what would be the size of array for int a[4] = {1,2,3,4};

4. 4 bytes times 4 variables is 16. This is basic arithmetic you are confused about.

int a[5]; would be 20 bytes for the same reasoning, but we are talking about int a[4];

Please remember that the highest subscript (or index) is one less than the declared size of the dimension, such that int a[4] has a high subscript of 3, int a[5] has a high subscript of 4, and so on and so forth.

5. Originally Posted by whiteflags
4 bytes times 4 variables is 16. This is basic arithmetic you are confused about..
I am confused on two line's what happen when they run on machine

1.What happen when this line execute a[4]; ?
what would be the size of array for int a[4]; i think it would be 16

2.What happen when this line execute int a[4] = {1,2,3,4}; ?

what would be the size of array for int a[4] = {1,2,3,4}; i think it would be 16

6. What you seem to be asking is would the size of an uninitialized array be different from the size of an initialized one. Try this program, and see what it prints out:
Code:
```#include <stdio.h>

int main(void)
{
int a[4];
int b[4] = {1, 2, 3, 4};
printf("sizeof(a) = %zd\n", sizeof(a));
printf("sizeof(b) = %zd\n", sizeof(b));
return 0;
}```

7. Originally Posted by anduril462
What you seem to be asking is would the size of an uninitialized array be different from the size of an initialized one. Try this program, and see what it prints out:
Both initialized and uninitialized aray print the same value zd. Working size for both will same

8. Hey there,
The thing with arrays is that the moment you type something like int[4] a; what the compiler really does is that it just does the initialization itself by the default value of the given data type of that array if it is a primitive type. So, in this case it becomes something like
Code:
` int[4] a={0,0,0,0}`
IMPLICITLY. but when you choose to initialize the array yourself then you just write your values in the curly braces. But the size remains the same in both the cases.

9. so when we don't initialize array it will store garbage value

Code:
```#include<stdio.h>int main(){

int array[4];

printf("array element     : %d \n", array[0]);
printf("addaress of array : %d \n", &array[0]);
printf("array element     : %d \n", array[1]);
printf("addaress of array : %d \n", &array[1]);
printf("array element     : %d \n", array[2]);
printf("addaress of array : %d \n", &array[2]);
printf("array element     : %d \n", array[3]);
printf("addaress of array : %d \n", &array[3]);
printf("array element     : %d \n", array[4]);
printf("addaress of array : %d \n", &array[4]);
return 0;
}```
array element : 4200912
array element : 0
array element : 3141632
array element : 4194432
array element : 6422300

When initialize array

Code:
```#include<stdio.h>
int main(){

int array[4] = {4,2,3,5};

printf("array element     : %d \n", array[0]);
printf("addaress of array : %d \n", &array[0]);
printf("array element     : %d \n", array[1]);
printf("addaress of array : %d \n", &array[1]);
printf("array element     : %d \n", array[2]);
printf("addaress of array : %d \n", &array[2]);
printf("array element     : %d \n", array[3]);
printf("addaress of array : %d \n", &array[3]);

return 0;
}```
array element : 4
array element : 2
array element : 3
array element : 5

10. Originally Posted by vishaljamdagni
Hey there,
The thing with arrays is that the moment you type something like int[4] a; what the compiler really does is that it just does the initialization itself by the default value of the given data type of that array if it is a primitive type. So, in this case it becomes something like
Code:
` int[4] a={0,0,0,0}`
IMPLICITLY. but when you choose to initialize the array yourself then you just write your values in the curly braces. But the size remains the same in both the cases.
No! If it's not global and it's not static then there is NO implicit initialisation; there is no initialisation at all

so when we don't initialize array it will store garbage value
That's correct because unless you initialise the values to something they remain uninitialised; i.e. garbage as you call it