As mentioned in this answer, printf
uses the format specifiers to know what to pop off the stack (%d
is sizeof(int)
, which is 4 bytes in my system).
(Edit: As pointed out by rici in the comments, it's not guaranteed to be the stack (on x86
it's in the CPU registers most of the time). The standard never talks about implementations, which is what this problem is all about.)
A small fun fact: in my Windows 10 box it was 0 and 1. In my Xubuntu VBox it was 1 and -2132712864. Therefore, printf
is poping only 4 bytes, and the endianess of your machine is screwing the result. From pow(3) manual:
#include <math.h>
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);
So, in order to get the correct result in both cases, make use of the correct printf
format specifier:
double a = pow(3546, 0);
And:
printf("%lf\n", pow(3546, 0));
If you want to know more about variadic functions (the ones like printf
, which take a variable amount of parameters), start by reading the API manual page.
#include <stdarg.h>
void va_start(va_list ap, last);
type va_arg(va_list ap, type);
void va_end(va_list ap);
void va_copy(va_list dest, va_list src);