0

I found this code on the codegolf.stackexchange site.

#include <stdio.h>
#define function int
#define var int
struct { int (*log)(const char *,...); } console = { printf };

/* From here on only JavaScript! */

function fac(x){
    if(x < 2) return 1;
    return x * fac(x - 1);
}

function main(){
    console.log("Hello world!\n");

    for(var i = 0; i < 10; i++){
        console.log("%i! = %i\n", i, fac(i));
    }

    return 0;
}

// *Should* we export the main function of this library??/
exports.main = main;

My question is, how is he able to run variadic function without including stdarg.h?

1 Answer 1

4

Because he is not manipulating the ... parameter, but simply passing pointer to a function which internally manipulates the ... parameter, which in the case is printf:

int __cdecl printf(const char *_Format, ...);

Note: not all compilers support the __cdecl calling convention.

Aswell, the macros he has defined are pointless and should not be used under any circumstance, as it is not C.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.