Wednesday, 15 July 2015

Why does this C++ function return a value? -



Why does this C++ function return a value? -

this question has reply here:

non-void function works fine without executing return 2 answers

why code print out n-100?

int hello(int n) { for(int = 0; < n-100; i++) { } } int main() { int h = hello(12); cout << hello(12) << " " << h << endl; }

yet, both of these function homecoming garbage (2665092 , 0 respectively)

int hello1(int n) { for(int = 0; < 12; i++); } int hello2(int n) { (n - 100); }

i compiled code using g++ in cygwin environment.

you seeing result of undefined behaviour.

always compile -wall -werror prevent kind of bug creeping code.

c++ function for-loop

No comments:

Post a Comment