Variable parameters in C Language

Source: Internet
Author: User

I have not used many variable parameters in the past, but recently I found it brings a lot of convenience ....

Below is an example of C LanguageCode:

# Include <stdio. h> # Include <Stdarg. h> Int Print ( Char * FMT ,...){  Char Buffer [ 1024 ] = { 0  }; Va_list Va; va_start (va, FMT); vsnprintf (buffer, sizeof (buffer), FMT, VA); va_end (VA );  Return  Printf (buffer );} Int Main ( Int Argc, Char ** Argv) {print (  "  % S % d % C \ n  " , "  Girl does not cry  " , 100 , 65  );  Return   0  ;} 

Note that the variable parameter function is in the C language, so if you use it in C ++, You need to define the function as follows:

 
Int _ cdeclPrint (Char*FMT ,...)


This is also useful in Windows programming. For example, when an error is displayed:
My previous practices are as follows:

 
CharBuf [128] = {0}; Wsprintf (BUF,"Error: % 08x", Getlasterror (); MessageBox (null, Buf, null, mb_ OK| Mb_iconexclamation );

 

Now, you don't need to define the variables. You can simply do it in one sentence:
Msgbox (null, null, mb_ OK | mb_iconexclamation, "% S % d % C", "girl does not cry", 100, 'A ');
Girl don't cry (QQ: 191035066) @ 2012-05-10 22:41:13 @ http://www.cnblogs.com/nbsofer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.