![](/img/trans.png)
[英]Is there a portable way to implement variadic arguments in C without stdarg.h?
[英]C, variadic functions and stdarg.h
我在codegolf.stackexchange網站上找到了此代碼。
#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;
我的問題是,他如何能夠在不包含stdarg.h的情況下運行可變參數功能?
因為他不是在操縱...參數,而是簡單地將指針傳遞給內部操縱...參數的函數,在這種情況下,該函數是printf:
int __cdecl printf(const char *_Format, ...);
注意:並非所有編譯器都支持__cdecl調用約定。
同樣,他定義的宏是沒有意義的,在任何情況下都不應使用,因為它不是C。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.