"C language" on variable parameters and printf function

Source: Internet
Author: User
Tags function prototype

I. What is a mutable parameter?

int printf (const char* format, ...);

This is the printf function prototype, which is familiar to people who have used the C language, and its parameters are fixed parameter format and variable parameters (denoted by "...").

And we can call printf in a variety of ways, such as:

printf ("%d", value);p rintf ("%s", str);p rintf ("The number is%d, string is:%s", value, str);
Two. Principle of implementation

The C language uses macros to handle these mutable parameters.

These macros look very complex, in fact the principle is very simple: it is based on the characteristics of the parameter into the stack from the closest to the first variable parameter of the fixed parameters, and then get the address of each variable parameter. Let's analyze these macros.

In the VC stdarg.h header file, for different platforms have different macro definitions, we choose the macro definition under the X86 platform:

typedef char *va_list; #define _INTSIZEOF (N) ((sizeof (n) + sizeof (int)-1) & ~ (sizeof (int)-1)) #define VA_START (AP, V) (AP = (va_list) &v + _intsizeof (v)) #define VA_ARG (Ap,t) (* (t *) (AP + = _intsizeof (t))-_intsizeof (t)) #define VA _end (AP) (AP = (va_list) 0)

Can be used to indicate:

In the majority of C compilers, such as VC, by default, the order of the parameters into the stack is right-to-left, so the memory model after the parameter stack is shown: The address of the last fixed parameter is below the first mutable parameter and is stored continuously.
| —————————————————————————— |
| The last variable parameter | High memory address at
| —————————————————————————— |
...................
| —————————————————————————— |
| nth variable Parameter | ->va_arg (Arg_ptr,int) after the arg_ptr point of the place,
| | That is, the address of the nth variable parameter.
| ——————————————— |
...............................
| —————————————————————————— |
| First variable parameter | ->va_start (Arg_ptr,start) after arg_ptr refers to the place
| | That is, the address of the first variable parameter
| ——————————————— |
| ———————————————————————— —— |
| |
| last fixed parameter | Start address of Start
| —————————————— — | .................
| —————————————————————————— |
| |
| ——————————————— |-> Low memory address

Three. printf Research

Here is a simple implementation of the printf function, referring to the 156-page example, the reader can combine the code of the book with this article reference.

#include <stdio.h> #include <stdlib.h>//a simple implementation similar to printf,//parameter must be int type void myprintf (char* fmt, ...) {char* parg=null;//equivalent to the original Va_listchar C;parg = (char*) &fmt;//note do not write P = fmt!! Because this is where the parameter is to be taken, not the value parg + = sizeof (FMT); Equivalent to the original Va_startdo{c =*fmt;if (c! = '% ') {Putchar (c);//output as-is character}else{//output data by format character switch (* (++FMT)) {case ' d ':p rintf ("%d" , * ((int*) parg)), break;case ' x ':p rintf ("% #x", * ((int*) parg)); break;default:break;} Parg + = sizeof (int); Equivalent to the original va_arg}++fmt;} while (*fmt! = ");p arg = NULL; equivalent to Va_endreturn;} int main () {int i = 1234;int j = 5678;myprintf ("The first test:i=%d", I); myprintf ("The Secend test:i=%d; %x;j=%d; ", i,0xabcd,j); System (" pause "); return 0;}

Output:

The first test:i=1234the secend test:i=1234; 0xabcd;j=5678;
Four. Application

Maximum value:

#include <stdarg.h>//variable number parameter required macro # include <stdio.h>int max (int n,int num, ...) {int m = Num;int i = 0;va_list x;//description variable Xva_start (x,num);//x is initialized to the first parameter to num after for (i=1; i<n; i++) {//assigns the value of the int type that the variable x points to Y, Also make x refer to the next parameter int y = va_arg (X,int); if (y>m) {m=y;}} Va_end (x);//clear variable xreturn m;} int main () {int ret1 = max (3,5,56,55), int ret2 = max (6,0,4,32,45,533,6565);p rintf ("%d,%d", Ret1,ret2); return 0;}

Output:

56,6565
      related recommendations
    1. The "C language" simulation implements the printf function (variable parameter)
    2. Functions, prototypes, usages and examples of printf functions

"C language" on variable parameters and printf function

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.