4 4 votes void main() { int x,y; y=10; scanf("%d", &x); } When memory wil be allocated to the x and y?Compile time or run time? Programming in C programming-in-c + – rahul sharma 5 1.5k views answer comment Share Follow Print See all 11 Comments 11 11 Comments reply joshi_nitish commented Sep 23, 2017 reply Follow flag x and y will be allocated memory during compile time itself, if heap is involved then memory would be allocated at run time.. 0 0 replyShare rahul sharma 5 commented Sep 23, 2017 reply Follow flag These are created in stack.So stack is also initialized during compile time? 0 0 replyShare joshi_nitish commented Sep 23, 2017 reply Follow flag x and y are part of code segment and allocated memory during compile time. 0 0 replyShare Arjun commented Sep 23, 2017 reply Follow flag All local variables in C go to stack. Stack memory is created during runtime and never initialized implicitly. 2 2 replyShare gari commented Sep 23, 2017 reply Follow flag isn't this true that for static and global variables only the memory is allocated at compile time ? and at run time x and y are allocated memory. 0 0 replyShare Manu Thakur commented Sep 23, 2017 reply Follow flag @Arjun If I am not wrong, then static/global variables will be allocated memory during compile time and initialized implicitly to 0. while in this above mentioned case, variables will be allocated memory during run time. 0 0 replyShare Arjun commented Sep 23, 2017 reply Follow flag yes, static and global variables go to the Data segment and this is part of the "executable" file. 1 1 replyShare joshi_nitish commented Sep 23, 2017 reply Follow flag @Arjun sir, why it is giving compiler error? #include <stdio.h> int main(void) { int a[1000000000000000000000]; return 0; } since memory will be allocated in runtime...why still it gives error in compile time? is compiler itself assume that anyway this much memory will never be available at runtime no matter how big is main memory?? 0 0 replyShare Manu Thakur commented Sep 23, 2017 i edited Sep 23, 2017 reply Follow flag @joshi memory for an array will be allocated at compile time, isn't it? 0 0 replyShare joshi_nitish commented Sep 24, 2017 reply Follow flag here array is also local variable, then why memory will be given to it at compile time. 0 0 replyShare Arjun commented Sep 24, 2017 reply Follow flag @Joshi Yes, by definition "allocation" means setting aside of memory. And this happens at compile time for all static memory allocation -- which includes stack in C. But the actual memory being created for stack -- not called allocation -- is at runtime. For example, at compile time we can know how much memory a function stack takes. But only during runtime, we can know how many times, that stack is being used by the function like during a recursive call. So, for this exact question answer would be x and y are allocated memory during compile time on stack and hence not initialized. 1 1 replyShare Please log in or register to add a comment.