main()
{
int n;
scanf("%d",&n);
char a[n];
}
In this case are we not allocating memory during runtime to 'a' then why use malloc??
main()
{
int n;
scanf("%d",&n);
char a[n];
}
In this case are we not allocating memory during runtime to 'a' then why use malloc??
char a[n]
is not allowed in the older C standard. It is permitted in C99, but only for automatic variables (i.e. on the stack, as in your example). If you want, say, a global variable, you will need to use malloc
et. al. to comply with the standard.
Edited to provide some evidence
There is a helpful series of articles about variable-length arrays in C. From the second article, "VLAs must be auto (as opposed to static or extern) variables in a block."