The variable parameter problem of C language

Source: Internet
Author: User
Tags printf

Overview

C language has an indeterminate length of the parameters, such as: "...", which is mainly used in the parameters of the uncertainty of the function, our most easily think of the example is the printf function.

Prototype:

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

Use Example:

printf ("Enjoy yourself everyday!\n");

printf ("The value is%d!\n", value);

This variable parameter can be said to be a more difficult part of the C language, there will be several problems caused by some analysis of it.

Note: There is a function overload (overload) in C + + that can be used to distinguish calls to different function parameters, but it cannot represent any number of function arguments.

Problem: the implementation of printf

Excuse me, how to implement printf function, how to deal with the variable parameter problem? Answer and Analysis:

The standard C language defines a header file <stdarg.h> is designed to deal with a variable parameter list, which contains a set of macros, and a va_list typedef declaration. A typical implementation is as follows:

typedef char* va_list;
#define va_start(list) list = (char*)&va_alist
#define va_end(list)
#define va_arg(list, mode)\
((mode*) (list += sizeof(mode)))[-1]
自己实现printf:
#include <stdarg.h>
int printf(char* format, …)
{
va_list ap;
va_start(ap, format);
int n = vprintf(format, ap);
va_end(ap);
return n;
}

Problem: Parameters that are not determined at run time

Is there a way to write a function where the exact form of the function parameter can be determined at runtime?

Answer and Analysis:

There is no "formal" solution, but there is one, because a function has already given us an example of this, that is main (), its prototype is:

int main (int argc,char *argv[]);

The parameters of the function are argc and argv.

Think about it, "you can only determine the parameter form at run time," which means you can't see the accepted arguments from the declaration, which means there is no fixed form of the parameter at all. The common approach is that you can use it to point to the actual parameter area by defining a parameter of type void, and then interpret the meaning of the function according to your needs. This is the meaning of the argv in the main function, and the ARGC is used to indicate the actual number of arguments, which provides further convenience for us to use, of course, this parameter is not required.

Although the parameters are not in a fixed form, but we are bound to parse the meaning of the parameters in the function, so, of course, there is a requirement that the caller and the callee should agree on the format, size, and validity of the contents of the parameter area, otherwise it would be miserable to say the opposite.

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.