When I write
int main()
{
int j;
}
The memory for j is allocated at the time of compilation, but when during compilation?
The compilation process doesn't allocate the memory. It generates the code that allocates the memory :)
In this case j would be a so-called stack variable and it would be allocated when execution enters the main() function. Global and static variables are allocated on the heap instead.
Here's a short explanation: http://www.costech.or.tz/cs231/websites/C%20Programming/www-ee.eng.hawaii.edu/Courses/ee150/Book/chap14/subsection2.1.1.8.html. I'll see if I can find a better one.