C Programming - Memory Allocation - Discussion
Discussion Forum : Memory Allocation - Find Output of Program (Q.No. 3)
3.
What will be the output of the program?
#include<stdio.h>
#include<string.h>
int main()
{
char *s;
char *fun();
s = fun();
printf("%s\n", s);
return 0;
}
char *fun()
{
char buffer[30];
strcpy(buffer, "RAM");
return (buffer);
}
Answer: Option
Explanation:
The output is unpredictable since buffer is an auto array and will die when the control go back to main. Thus s will be pointing to an array , which not exists.
Discussion:
16 comments Page 2 of 2.
Rahul said:
1 decade ago
Take buffer variable as a globle to print RAM.
Mounika said:
6 years ago
Can anyone explain about buffer in C?
Amol said:
1 decade ago
To print RAM just return ("RAM") ;.
Deepak said:
1 decade ago
May also cause stack smashing
Jonty said:
1 decade ago
Why does static return RAM?
Primshapradheep said:
8 years ago
Thank you @Narendar.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers