vsprintf, vswprintf與printf與函數的可變參數編程

來源:互聯網
上載者:User

在C語言編程中,我們不可避免的要接觸到可變參數函數,對於不支援函數多態的C語言來講,使用可變參數和宏定義函數是變通的實現函數多態的好方法。在進一步涉及到可變參數函數之前,我們先來看看常用到的兩個可變參數的典型,分別是vsprintf和sprintf。

一、vsprintf函數

Header File

stdio.h

Category

Memory and String Manipulation Routines

Prototype

int vsprintf(char *buffer, const char *format, va_list arglist);

int vswprintf(wchar_t *buffer, const wchar_t *format, va_list arglist);

Description

Writes formatted output to a string.

The v...printf functions are known as alternate entry points for the ...printf functions. They behave exactly like their ...printf counterparts, but they accept a pointer to a list of arguments instead of an argument list.

vsprintf accepts a pointer to a series of arguments, applies to each a format specifier contained in the format string pointed to by format, and outputs the formatted data to a string. There must be the same number of format specifiers as arguments.

Return Value

vsprintf returns the number of bytes output. In the event of error, vsprintf returns EOF.

--對照翻譯

標頭檔
stdio.h

分類
記憶體和字串操作

函數原型
int vsprintf(char *buffer, const char *format, va_list arglist);

int vswprintf(wchar_t *buffer, const wchar_t *format, va_list arglist);

描述
寫格式化後的輸出到一個字串

v..printf函數族是..print函數族的可替代函數,他們像..printf函數族一樣操作,但是他們接受指向參數列表的指標而不是參數列表。
vsprintf接受一個指向一系列可變參數的指標,提供給每一個參數一個包含在form中的格式化定義,並且輸出格式化後的資料到一個字串中,格式定義和參數數量必須相等。

傳回值
vsprintf返回輸出的位元組數目,出錯時返回EOF

二、sprintf函數

Header File

stdio.h

Category

Memory and String Manipulation Routines

Prototype

int sprintf(char *buffer, const char *format[, argument, ...]);

int swprintf(wchar_t *buffer, const wchar_t *format[, argument, ...]);

Description

Writes formatted output to a string.

Note: For details on format specifiers, see printf.

sprintf accepts a series of arguments, applies to each a format specifier contained in the format string pointed to by format, and outputs the formatted data to a string.

sprintf applies the first format specifier to the first argument, the second to the second, and so on. There must be the same number of format specifiers as arguments.

Return Value

On success, sprintf returns the number of bytes output. The return value does not include the terminating null byte in the count.

On error, sprintf returns EOF.

--對照翻譯

標頭檔:

stdio.h

標頭檔
stdio.h

分類
記憶體和字串操作

函數原型
int sprintf(char *buffer, const char *format[, argument, ...]);

int swprintf(wchar_t *buffer, const wchar_t *format[, argument, ...]);

描述
寫格式化後的輸出到一個字串
注意:對于格式化定義規範,參看printf
sprintf接受一系列參數,提供給每一個參數一個格式化定義,並且輸出格式化資料到字串
sprintf提供給首個參數第一個格式化定義,第二個賦予次個格式化定義,格式化定義數量必須和參數數量一致

傳回值
成功,返回輸出的位元組數量,傳回值不包含終止null位元組的位元組數量
錯誤,返回EOF

為了便於比較這兩個函數的使用,下面給出一個程式片段:

    char szBuffer[256];
    sprintf(szBuffer, "welcome %d, %s", 1, "hi");
    ShowMessage(szBuffer);
    vsprintf(szBuffer, "welcome %d, %s", 1, "hi"); //<-提示[C++ Error] Unit1.cpp(24): E2034 Cannot convert 'int' to 'void *'
    ShowMessage(szBuffer);

 

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/9527/archive/2008/05/19/2457816.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.