What you read from stdin
is being stored on the stack, in the stack frame of the function input_expression
. Once the function returns it's stack frame and thus the variable buf
are no longer valid. Their values are undefined. When you print your result you are referencing "old stack memory". An that's what you can see in the output.
You either have to pass a buffer as a variable to your function that has a longer lifetime or heap allocate the result and free it later.
If you are new to lower level programming languages maybe have a look at this too: What's the difference between Stack Memory and Heap Memory?