C 語言中的可變參數

高等 C 語言

簡介

字串

指標與陣列

函數

結構

物件導向

記憶體

檔案

錯誤處理

巨集處理

C 與組合語言

資料結構

動態字串

動態陣列

鏈結串列

雜湊表

開發環境

Make

Cygwin

MinGW

DevC++

wxDevC++

編譯器

gcc 編譯器

TinyCC 編譯器

LCC 編譯器

應用主題

CGI 程式

GNU 程式

視窗程式

影像處理

練習題

訊息

相關網站

參考文獻

最新修改

簡體版

English

範例:印出整數串列

#include <stdio.h>
#include <stdarg.h>

void printList(int head, ... ) {
  va_list va;
  va_start(va, head);
  int i;
  for(i=head ; i != -1; i=va_arg(va,int)) {
    printf("%d ", i);
  }
  va_end(va);
}

int main( void ) {
  printList(3, 7, 2, 5, 4, -1);
}

執行結果

D:\cp>gcc vaarg.c -o vaarg

D:\cp>vaarg
3 7 2 5 4

範例

#include <stdio.h>

int debug(const char *fmt, ...)
{
  va_list args;
  va_start(args, fmt);
  return vprintf(fmt, args);
}

int main() {
  debug("pi=%6.2f\n", 3.14159);
}

執行結果:

D:\cp>gcc debug.c -o debug

D:\cp>debug
pi=  3.14

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License