Please explain why sometimes return statement is not needed?
Function has a return type, but return statement missing. Meanwhile, program compiles and works fine.
Please help me understand this better
char* handleInput() {
fgets(buffer, 1024, stdin);
// return buffer; <---- COMMENTED RETURN
}
void main() {
char* ptr = handleInput();
int flag = atoi(ptr);
if (flag < 0) break;
printf("You entered: %s\n", ptr);
}
Basically what gets returned is dumb luck. You get what happens to be in the CPU register when it comes back. If, for example, the returned value would be in AX, and the
char*happens to be in AX, you lucked out. I believe this is an undefined behavior; i.e. the C language specifications don’t tell what you should so, so it is left to the compiler. I’m surprised a modern compiler wouldn’t at least throw a warning at you.