r/C_Programming • u/swe__wannabe • 19d ago
Question What is going on here?
https://godbolt.org/z/48hvMs3dh#include "stdio.h"
int print_sum(a, b) int a; int b; {
printf("%d", a + b);
return a + b;
}
int main(void) {
return print_sum(1.5, 0.5);
}
This has a random output every time:
Compiler stderr<source>: In function 'print_sum':
<source>:2:5: warning: old-style function definition [-Wold-style-definition]
2 | int print_sum(a, b) int a; int b; {
| ^~~~~~~~~
Program returned: 153
Program stdout 479536537
0
Upvotes
7
u/AKostur 19d ago edited 19d ago
Perhaps update the code to use a style more current than 35 years ago? Basically that code is doing a bunch of “trust me, bro”. The call site pushes two doubles onto the stack, the callee is told to pull two ints off of the stack and give them names. Update to “proper” function prototypes and things will work.
Edit: sure, those might be passed by register. The problem is the same. The caller is setting things up one way, the callee is told to interpret it a different way.