📄 faqcat744e.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:46 2005 by faqproc version 2.7 --><!-- from source file varargs.sgml dated Sun Nov 25 08:09:42 2001 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=varargs by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:20 GMT --><head><base ><meta name=GENERATOR content="faqproc"><title>Variable-Length Argument Lists</title></head><body bgcolor="#ffffff"><H1>15. Variable-Length Argument Lists</H1><a name="protos"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../varargs/protos.html"><!-- qtag -->Question 15.1</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I heard that you have to <TT>#include</TT> <TT><stdio.h></TT>before calling <TT>printf</TT>.Why?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>So that a proper prototype for <TT>printf</TT> will be in scope.</p><p>A compiler may use a differentcalling sequence for functions which accept variable-length argument lists.(It might do so if calls using variable-length argument listswere less efficient than those using fixed-length.)Therefore,a prototype(indicating,using the ellipsis notation``<TT>...</TT>'',thatthe argument listis of variable length)must be in scope whenever a varargs function is called,so that the compiler knows to use the varargs calling mechanism.</p><p>References:ISO Sec. 6.3.2.2, Sec. 7.1.7<br>Rationale Sec. 3.3.2.2, Sec. 4.1.6<br>H&S Sec. 9.2.4 pp. 268-9, Sec. 9.6 pp. 275-6<hr><hr><hr><a name="promos"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../varargs/promos.html"><!-- qtag -->Question 15.2</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can <TT>%f</TT> be usedfor both <TT>float</TT> and <TT>double</TT> argumentsin <TT>printf</TT>?Aren't they different types?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Inthe variable-length part of avariable-length argument list,the ``default argument promotions'' apply:types <TT>char</TT> and <TT>short int</TT> are promoted to <TT>int</TT>,and <TT>float</TT> is promoted to <TT>double</TT>.(These are the same promotions that apply tofunction calls without a prototype in scope,also known as ``old style'' function calls;see question <a href="faqcat7d4b.html?sec=ansi#argpromos">11.3</a>.)Therefore, <TT>printf</TT>'s <TT>%f</TT> formatalways sees a <TT>double</TT>.(Similarly, <TT>%c</TT> always sees an <TT>int</TT>,as does <TT>%hd</TT>.)See also questions<a href="faqcat1d60.html?sec=stdio#scanfvsprintf">12.9</a>and<a href="faqcat1d60.html?sec=stdio#scanf2">12.13</a>.</p><p>References:ISO Sec. 6.3.2.2<br>H&S Sec. 6.3.5 p. 177, Sec. 9.4 pp. 272-3<hr><hr><hr><a name="proto2"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../varargs/proto2.html"><!-- qtag -->Question 15.3</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I had a frustrating problem which turned out to be caused by the line<pre> printf("%d", n);</pre>where <TT>n</TT> was actually a <TT>long int</TT>.I thought that ANSI function prototypeswere supposed to guard against argument type mismatches like this.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Whena functionacceptsa variable number of arguments,itsprototype does not(and cannot)provide any informationabout the number and types ofthosevariable arguments.Therefore,the usual protections do <em>not</em> applyin the variable-length part of variable-length argument lists:the compiler cannotperform implicit conversions or(in general)warn about mismatches.The programmer must make sure that arguments match,or must manually insert explicit casts.</p><p>In the case of <TT>printf</TT>-like functions,some compilers(including <TT>gcc</TT>)and some versions of <TT>lint</TT>are able to check the actual arguments against the format string,as long as the format string isan immediatestring literal.</p><p>See also questions<a href="faqcat1f1a.html?sec=null#null2">5.2</a>,<a href="faqcat7d4b.html?sec=ansi#argpromos">11.3</a>,<a href="faqcat1d60.html?sec=stdio#scanfvsprintf">12.9</a>,and<a href="faqcat744e.html?sec=varargs#promos">15.2</a>.<hr><hr><hr><a name="varargs1"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../varargs/varargs1.html"><!-- qtag -->Question 15.4</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I write a function that takes a variable number ofarguments?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Usethe facilities ofthe <TT><stdarg.h></TT>header.</p><p>Here is a function which concatenates an arbitrary number ofstrings into <TT>malloc</TT>'ed memory:</p><pre>#include <stdlib.h> /* for malloc, NULL, size_t */#include <stdarg.h> /* for va_ stuff */#include <string.h> /* for strcat et al. */char *vstrcat(const char *first, ...){ size_t len; char *retbuf; va_list argp; char *p; if(first == NULL) return NULL; len = strlen(first); va_start(argp, first); while((p = va_arg(argp, char *)) != NULL) len += strlen(p); va_end(argp); retbuf = malloc(len + 1); /* +1 for trailing \0 */ if(retbuf == NULL) return NULL; /* error */ (void)strcpy(retbuf, first); va_start(argp, first); /* restart; 2nd scan */ while((p = va_arg(argp, char *)) != NULL) (void)strcat(retbuf, p); va_end(argp); return retbuf;}</pre>(Note thata second call to <TT>va_start</TT> is neededto re-start the scanwhen the argument list is processed a second time.Note the calls to <TT>va_end</TT>:they're important for portability,even if they don't seem to do anything.)<p>A call to <TT>vstrcat</TT> looks something like<pre> char *str = vstrcat("Hello, ", "world!", (char *)NULL);</pre>Note the cast on the last argument;see questions <a href="faqcat1f1a.html?sec=null#null2">5.2</a> and <a href="faqcat744e.html?sec=varargs#proto2">15.3</a>.(Also note that the caller must free the returned,<TT>malloc</TT>'ed storage.)</p><p><TT>vstrcat</TT> acceptsa variable number of arguments,all of type <TT>char *</TT>.Here is an example which accepts a variable number of arguments of different types;it is a stripped-down versionof the familiar <TT>printf</TT> function.Note that each invocation of <TT>va_arg()</TT> specifies the type of the argumentbeingretrievedfrom the argument list.</p><p>(The <TT>miniprintf</TT> function here uses<TT>baseconv</TT>from question <a href="faqcat38c2.html?sec=misc#hexio">20.10</a> to formatnumbers.Itissignificantlyimperfectin that it will not usually be able to printthe smallest integer, <TT>INT_MIN</TT>, properly.)<pre>#include <stdio.h>#include <stdarg.h>#ifdef MAINvoid miniprintf(const char *, ...);main(){ miniprintf("Hello, world!\n"); miniprintf("%c %d %s\n", '1', 2, "three"); miniprintf("%o %d %x\n", 10, 10, 10); miniprintf("%u\n", 0xffff); return 0;}#endifextern char *baseconv(unsigned int, int);voidminiprintf(const char *fmt, ...){ const char *p; int i; unsigned u; char *s; va_list argp; va_start(argp, fmt); for(p = fmt; *p != '\0'; p++) { if(*p != '%') { putchar(*p); continue; } switch(*++p) { case 'c': i = va_arg(argp, int); /* *not* va_arg(argp, char); see Q <a href="faqcat744e.html?sec=varargs#float">15.10</a> */ putchar(i); break; case 'd': i = va_arg(argp, int); if(i < 0) { /* XXX won't handle INT_MIN */ i = -i; putchar('-'); } fputs(baseconv(i, 10), stdout); break; case 'o': u = va_arg(argp, unsigned int); fputs(baseconv(u, 8), stdout); break; case 's': s = va_arg(argp, char *); fputs(s, stdout); break; case 'u': u = va_arg(argp, unsigned int); fputs(baseconv(u, 10), stdout); break; case 'x': u = va_arg(argp, unsigned int); fputs(baseconv(u, 16), stdout); break; case '%': putchar('%'); break; } } va_end(argp);}</pre></p><p>See also question <a href="faqcat744e.html?sec=varargs#oldvarargs">15.7</a>.</p><p>References:K&R2 Sec. 7.3 p. 155, Sec. B7 p. 254<br>ISO Sec. 7.8<br>Rationale Sec. 4.8<br>H&S Sec. 11.4 pp. 296-9<br>CT&P Sec. A.3 pp. 139-141<br>PCS Sec. 11 pp. 184-5, Sec. 13 p. 242<hr><hr><hr><a name="vprintf"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../varargs/vprintf.html"><!-- qtag -->Question 15.5</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I write a function that takes a format string and avariable number of arguments, like <TT>printf</TT>, and passes them to<TT>printf</TT> to do most of the work?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Use <TT>vprintf</TT>, <TT>vfprintf</TT>,or <TT>vsprintf</TT>.These routines are like their counterparts<TT>printf</TT>, <TT>fprintf</TT>, and <TT>sprintf</TT>,except that instead of a variable-length argument list,they accept a single <TT>va_list</TT> pointer.</p><p>As an example,hereis an <TT>error</TT>function which prints an error message,preceded by the string ``error: '' and terminated with a newline:</p><pre>#include <stdio.h>#include <stdarg.h>void error(const char *fmt, ...){ va_list argp; fprintf(stderr, "error: "); va_start(argp, fmt); vfprintf(stderr, fmt, argp); va_end(argp); fprintf(stderr, "\n");}</pre><p>See also question <a href="faqcat744e.html?sec=varargs#oldvarargs">15.7</a>.</p><p>References:K&R2 Sec. 8.3 p. 174, Sec. B1.2 p. 245<br>ISO Secs. 7.9.6.7,7.9.6.8,7.9.6.9<br>H&S Sec. 15.12 pp. 379-80<br>PCS Sec. 11 pp. 186-7<hr><hr><hr><a name="vscanf"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../varargs/vscanf.html"><!-- qtag -->Question 15.6</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I write a function analogous to <TT>scanf</TT>,i.e. that accepts similar arguments,andcalls <TT>scanf</TT> to do most of the work?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>C99(but <em>not</em> any earlier C Standard)supports<TT>vscanf</TT>,<TT>vfscanf</TT>,and<TT>vsscanf</TT>.</p><p>References:C9X Secs. 7.3.6.12-14<hr><hr><hr><a name="oldvarargs"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../varargs/oldvarargs.html"><!-- qtag -->Question 15.7</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I havea pre-ANSIcompiler,without <TT><stdarg.h></TT>.What can I do?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>There's an older header, <TT><varargs.h></TT>,which offers about the same functionality.</p><p>Here isthe <TT>vstrcat</TT> function from question <a href="faqcat744e.html?sec=varargs#varargs1">15.4</a>,rewritten to use <TT><varargs.h></TT>:<pre>#include <stdio.h>#include <varargs.h>#include <string.h>extern char *malloc();char *vstrcat(va_alist)va_dcl /* no semicolon */{ int len = 0; char *retbuf; va_list argp; char *p; va_start(argp); while((p = va_arg(argp, char *)) != NULL) /* includes first */ len += strlen(p); va_end(argp); retbuf = malloc(len + 1); /* +1 for trailing \0 */ if(retbuf == NULL) return NULL; /* error */ retbuf[0] = '\0'; va_start(argp); /* restart for second scan */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -