#include<stdio.h>
int &abc();
int main()
{
int b=abc();
getch();
return 0;}
int &abc()
{
int a=5;
return a;
}
whats the output and why?
This looks like homework...
Did you try compiling it with any decent compiler? Take a look at
what the warning messages tell you (leaving aside the fact that you
didn't put any #include in that would have defined "getch()")
~/tmp % g++ -Wall x.cpp
x.cpp: In function ‘int main()’:
x.cpp:9: error: ‘getch’ was not declared in this scope
x.cpp:7: warning: unused variable ‘b’
x.cpp: In function ‘int& abc()’:
x.cpp:15: warning: reference to local variable ‘a’ returned
T:\tmp> cl.exe /EHsc x.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 15.00.30729.01 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
x.cpp
x.cpp(9) : error C3861: 'getch': identifier not found
x.cpp(16) : warning C4172: returning address of local variable or
temporary
- Kevin B. McCarty