Why int a [5] = {1,2,3,4,5,6} gives a warning, while int a [5] = {1,2,3,4,5}; a [5] = 6; not?
The assignment gives you a warning because you know the size of the variable in the initialization statement and clearly violate the size of your declaration. You do not have the size of the array from a in line a[6] = 6 , so for the compiler this looks fine. Of course, the level of warnings varies from compiler to compiler, and for some compilers you can specify additional warnings.
For example, using gcc, you can use the -Wextra and -Wall flags to get a lot of warnings. Getting warnings is good, because the compiler helps you find possible caveats without requiring debugging your code. Of course, they are only good if you fix them :-)
Is this good practice when I initially declared an array of size 5?
It is never recommended to assign an integer to a place that you did not declare - you cannot be sure where this value is written, and it can rewrite another variable or, even worse, partially rewrite another variable or stack. Since this type of material differs from the compiler by the compiler, as @PascalCuoq pointed out, it is called undefined behavior , and this is what you want to avoid at all costs. Of course, since it is undefined, it may happen that your program will be executed only after this announcement, but this is a very bad practice.
However, there is nothing wrong with initializing a fixed-size array if it does not change. You should avoid magic numbers and use constants instead, such as MAX_NUMBER_OF_PERMUTATIONS or CURRENCIES_SIZE .
Can I declare it like this: int a []?
Declaring it as int a[] is shorthand when you initialize a fixed array, and the compiler can specify the number of elements. For instance:
int a[] = {1,2,3}; //this is good int b[3] = {1,2,3}; //same from above
In the past, int a[]; was usually declared int a[]; However, it does not work in every compiler, so it should be avoided. (Thanks @PascalCuoq for pointing this out)
What if i don't know the size of my array?
If you do not know the size of your array, you must declare it as a pointer, for example int * a , and independently manage the memory using malloc , realloc , calloc and similar system calls. Please do a good job and learn about free too, the world will thank you later. You should read pointers instead of arrays if you are looking for dynamic memory allocation.