r/C_Programming • u/swe__wannabe • Aug 14 '26
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
25
u/L_uciferMorningstar Aug 14 '26
I'm baffled that this is valid syntax at all
18
u/HildartheDorf Aug 14 '26 edited Aug 14 '26
Why are you using the pre-C89 way of declaring functions? You are passing floats to a function that then states it uses ints.
Doylist answer: Undefined behaviour is undefined.
Watsonian answer: Assuming this is x86, floats are passed in different registers to ints. So the program is printing the sum of whatever happens to be in the first two integer registers when the program starts.
7
u/AKostur Aug 14 '26 edited Aug 14 '26
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.
5
u/torsten_dev Aug 14 '26
You're hiding the actual types of the parameters to print_sum declaration. That's what old K&R parameter declarations do. They let you define a function without having a function declaration with types.
The compiler doesn't stop you from doing this because until c23 this was legal. That doesn't mean you should do it, because this is bad code. It was bad code when ANSI C came out and it's become even less acceptable since.
Don't use things we got rid of for very good reasons. Use
-Wall -std=c23if you can, we can haz nice things.2
u/sciencekm Aug 14 '26
Any modern compiler would have flagged this with all sorts of warnings for conversion problems.
8
u/collectgarbage Aug 14 '26
To start. The defined function accepts integer numbers, but you called it with two floating point numbers.
6
u/SmokeMuch7356 Aug 14 '26
Several issues:
1.5and0.5aren'tints - they'redoubles. They don't have the same size or representation asint. If you try to interpret the bit pattern for adoublevalue as anintyou'll get some obnoxiously huge number.intanddoublearguments aren't pushed onto the stack; instead, they are passed via registers, and of course different registers are used for different type arguments. So yourprint_sumfunction may be looking for data in%rcxand%rdxwhen the arguments were actually passed in%xmm0and%xmm1.And this is where the K&R-style function definition is biting you in the ass, because it doesn't give the compiler the information it needs to catch this type mismatch during translation. There's a reason it's no longer supported, and you should write that function definition as
4
u/gnolex Aug 14 '26
K&R-style functions have very weird rules, they are effectively functions that take an arbitrary number of arguments, with some arguments being typed and named and none of them being required. Go ahead, put any number of arguments to the call for print_sum(), of any type, you'll see that the program accepts this. Misuse will lead to undefined behavior and you got exactly that.
To fix this you need to declare your function with correct types so that the program knows how to actually call it and check for argument count and types:
2
u/collectgarbage Aug 14 '26
Modern C function defn looks like:
int print_sum(int a, int b)
2
u/8d8n4mbo28026ulk Aug 14 '26 edited Aug 14 '26
I was completely baffled by this and thought it's a compiler bug. But, the standard gives an insightful example, citing it here in full (6.9.1p13):
(emphasis mine.)
No argument conversions in the K&R style! The compilers' behaviour here is still pretty crazy to me (and sanitizers don't spit out anything either).
EDIT: To elaborate, the compiler knows the type of every expression, so it knows that
print_sum(1.5, 0.5);can't possibly be correct since no argument conversions are done anda,bareints. This should be a compile-time error!0
u/swe__wannabe Aug 14 '26
I probably should have marked this post as /s