從printf談可變參數函數的實現

1.使用情形

`int a =10;double b = 20.0;char *str = "Hello world";printf("begin print\n");printf("a=%d, b=%.3f, str=%s\n", a, b, str);...`

2.printf 函數的實現

`//acenv.htypedef char *va_list;#define　_AUPBND　　　　(sizeof (acpi_native_int) - 1)#define　_ADNBND　　　　(sizeof (acpi_native_int) - 1)#define _bnd(X, bnd)　　(((sizeof (X)) + (bnd)) & (~(bnd)))#define va_arg(ap, T)　 (*(T *)(((ap) += (_bnd (T, _AUPBND))) - (_bnd (T,_ADNBND))))#define va_end(ap)　　　(void) 0#define va_start(ap, A) (void) ((ap) = (((char *) &(A)) + (_bnd (A,_AUPBND))))//start.cstatic char sprint_buf[1024];int printf(char *fmt, ...){　　va_list args;　　int n;　　va_start(args, fmt);　　n = vsprintf(sprint_buf, fmt, args);　　va_end(args);　　write(stdout, sprint_buf, n);　　return n;}//unistd.hstatic inline long write(int fd, const char *buf, off_t count){　　return sys_write(fd, buf, count);}`

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD