简体   繁体   中英

getting value from void function with printf

I am trying to understand following C program.

#include <stdio.h>
int main()
{
   int k = m();
    printf("%d", k);
}
void m()
{
    printf("hello");
}

Above code produces output of hello5 . I understand why it is printing hello , but I do not understand why there is 5 behind it. How the 5 is getting returned and stored into variable k ? How does the flow of this program work?

You are assigning return value of m() to k . Since m() has no return value undefined behaviour will be visible, so there will be "garbage" in k (in this case: 5).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM