So I am trying to learn C and I stumble on stdarg and ellipses (...). I write the example:
#include <stdio.h>
#include <stdarg.h>
int please_work(int num_args, ...);
int main(void){
please_work(10, 1, 12, 345, 231431, 334341);
return 0;
}
int please_work(int num_args, ...){
va_list args;
va_start(args, num_args);
for (int i = 0; i < num_args; i++){
int x = va_arg(args, int);
printf("x: %d\n", x);
}
va_end(args);
return 0;
}
But except of printing:
x: 1
x: 12
x: 345
x: 231431
x: 334341
It prints those values but with additional garbage values which end up printing like this:
x: 1
x: 12
x: 345
x: 231431
x: 334341
x: 4198720
x: 1880629838
x: 1883666304
x: 4198720
x: 4194368
I have tried for like an hour, but nothing