c - I am getting a value from a function without return -
this question has answer here:
i know happening in simple c code:
#include <stdio.h> int something(int a, int b) { int c = * 3 + 4 * b; } int main() { int res = something(12, 54); printf("%d\n", res); return 0; }
the function not have return, "res" stores value calculated in something.
following show compilation command, execution , output:
$ gcc main.c
$ ./a.out
252
edited
i have changed variables types int float, in relation @paul ogilvie comment, , seems return last int stored:
#include <stdio.h> float something(int a, int b) { float res = * 3 + 4 * b * 1.0; } int main() { float res = something(12, 54); printf("%d\n", res); return 0; }
$ gcc main.c
$ ./a.out
54
it undefined behavior, see here:
reaching end of other value-returning function undefined behavior, if result of function used in expression.
compiling code gcc -wreturn-type
gives appropriate warning:
ret.c:4:1: warning: control reaches end of non-void function [-wreturn-type]
Comments
Post a Comment