The use of v...printf class functions in C language

Source: Internet
Author: User

C Language Self-study gradually came to an end, today learned the standard library of the Stdarg.h head, which is associated with the stdio.h head inside a class of functions: v...printf function, the example of the inside to see after still do not understand, Google a bit still not very understand, so self-test a bit, Here are the procedures:

This type of function book lists four, the prototypes are:

int Const Char * restrict format, va_list arg); int vprintf (constChar * restrict format, va_list arg); int vsnprintf (charconstChar * restrict format, va_list arg); int vsprintf (charconstchar * restrict format, va_list Arg);

The book uses the VFPRINTF function example:

Let's say that the program needs to display the error message periodically, and we want each message to start with a prefix in the following format:

* * Error N:

The n here is 1 when the first error message is displayed, and an error message is incremented by 1 for each subsequent display. To make it easier to generate error messages, we will write a function called Errorf. This function is similar to the printf function, but it always adds * * Error N: At the beginning of the output, and always outputs to stderr instead of stdout. The Errorf function will call the VFPRINTF function to do most of the actual output work. Here is the possible notation for the Errorf function:

intErrorf (Const Char*format, ...) {  Static intNum_errors =0; intN;  Va_list ap; Num_errors++; fprintf (stderr,"* * Error%d:", num_errors);  Va_start (AP, format); N=vfprintf (stderr, format, AP);  Va_end (AP); fprintf (stderr,"\ n"); returnN;}

After reading the example I still do not know what this function really does, so I Google a bit, and found a website to cite such an example:

/*vfprintf Example*/#include<stdio.h>#include<stdlib.h>#include<stdarg.h>/*functions*/voidInputerror (Char*function,Char*format, ...); /*Main*/intMainvoid) {    intsex, age; /*Enter Gender*/puts ("Please enter sex: (1: Male 2: female)"); scanf ("%d", &sex); if(Sex! =1&& Sex! =2) {Inputerror ("Main","sex is 1 or 2! (Input value:%d) \ n", Sex); returnexit_failure; }     /*Enter Age*/puts ("Please enter your age:"); scanf ("%d", &Age ); if(Age <0|| Age > Max) {Inputerror ("Main","age Range [0,150]! (Input value:%d) \ n", age); returnexit_failure; }     returnexit_success;} /** * @brief error handling * @param [in] function name * @param [in] format string * @param [in] ... Variable length parameters*/voidInputerror (Char*function,Char*format, ...)    {va_list arg;     Va_start (ARG, format); fprintf (stderr,"function where error occurred:%s\n", function); /*Display error message*/vfprintf (stderr, format, ARG); Va_end (ARG);}

Execution Result:

Please enter sex: (1: Male 2: female)

3

function where the error occurred: main

Sex is 1 or 2! (Input value: 3)

After I read the example, I was a little hazy, do I say that this kind of function is converting the variable argument list of the function that called them into its own mutable argument list, and then outputting these parameters to the stream through a format match?

So I wrote a little function test for myself:

#include <stdio.h>#include<stdlib.h>#include<stdarg.h>intFuncChar*str, ...) {  intN;    Va_list ap;  Va_start (AP, str); N=vfprintf (STDOUT,STR,AP);  Va_end (AP); returnN;}intMainvoid) {func ("%d%d\n",2,1); return 0;}

The result of the execution is:

21st

Sure enough, it seems that the use of this kind of function is to use va_list to record the variable parameter position, the variable parameter list of the parent function is converted to the v...printf function by the va_list variable, that is to say ... the function of the printf class is the same, but The variable argument list of the printf class function is omitted and replaced with a call from the va_list variable!

The use of v...printf class functions in C language

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.