4 votes 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 asked Sep 23, 2017 rahul sharma 5 642 views answer comment Share Follow See all 11 Comments See all 11 11 Comments reply joshi_nitish commented Sep 23, 2017 reply Follow Share x and y will be allocated memory during compile time itself, if heap is involved then memory would be allocated at run time.. 0 votes 0 votes rahul sharma 5 commented Sep 23, 2017 reply Follow Share These are created in stack.So stack is also initialized during compile time? 0 votes 0 votes joshi_nitish commented Sep 23, 2017 reply Follow Share x and y are part of code segment and allocated memory during compile time. 0 votes 0 votes Arjun commented Sep 23, 2017 reply Follow Share All local variables in C go to stack. Stack memory is created during runtime and never initialized implicitly. 2 votes 2 votes gari commented Sep 23, 2017 reply Follow Share 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 votes 0 votes Manu Thakur commented Sep 23, 2017 reply Follow Share @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 votes 0 votes Arjun commented Sep 23, 2017 reply Follow Share yes, static and global variables go to the Data segment and this is part of the "executable" file. 1 votes 1 votes joshi_nitish commented Sep 23, 2017 reply Follow Share @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 votes 0 votes Manu Thakur commented Sep 23, 2017 i edited Sep 23, 2017 reply Follow Share @joshi memory for an array will be allocated at compile time, isn't it? 0 votes 0 votes joshi_nitish commented Sep 24, 2017 reply Follow Share here array is also local variable, then why memory will be given to it at compile time. 0 votes 0 votes Arjun commented Sep 24, 2017 reply Follow Share @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 votes 1 votes Please log in or register to add a comment.