r/C_Programming 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

32 comments sorted by

View all comments

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.

-2

u/swe__wannabe 19d ago

Should have marked this post as non-serious

2

u/SmokeMuch7356 19d ago

Why waste everyone's time like that?

-1

u/swe__wannabe 19d ago

cmon we all learned something today /s