Introduction to Variable Parameter ellipsis, variable parameter ellipsis

Source: Internet
Author: User

Introduction to Variable Parameter ellipsis, variable parameter ellipsis

Introduction to Variable Parameter ellipsis

C allows you to define the number of parameters and functions with uncertain types. For example, the standard function printf in C uses this mechanism. When declaring a function with an uncertain parameter, you can use the ellipsis (...) in the parameter section. . "…" Tells the compiler to check whether the parameter type is the same as the real parameter type or the number of parameters when calling a function.

For example:

[Root @ centos-64-min exercise] # vi chang. c
# Include <stdio. h>
Int mult (int x, int y, const char * fmt ,...)
{
Int result = x * y;
If (fmt)
{
Printf ("% s:", fmt );
}
Return result;
}

The compiler only checks whether the first parameter and the second parameter are integers and whether the third parameter is a string. It processes all the subsequent parameters, no matter what they are, as ellipsis, no matter.

[Root @ centos-64-min exercise] # vi chang. c
# Include <stdio. h>
Int mult (int x, int y, const char * fmt ,...)
{
Int result = x * y;
If (fmt)
{
Printf ("% s", fmt );
}
Return result;
}
Int main (void)
{
Int s = 0;
S = mult (1, 2, "1*2 = ");
Printf ("% d \ n", s );

S = mult (2, 3, "2*3 =", "hahaha", 12345,666 66); // ignore the following parameters: "Hahahaha", 12345, and 66666
Printf ("% d \ n", s );
Return 0;
}

"Chang. c" 20L, 314C written
[Root @ centos-64-min exercise] # gcc-o chang. c
[Root @ centos-64-min exercise] #./chang
1*2 = 2
2*3 = 6

Use of variable parameters in macro definition

Example:

[Root @ centos-64-min exercise] # vi chang. c
S = rectangle_area (3, 4, "this rectangle's area is ");
# Include <stdio. h>
Int mult (int x, int y, const char * fmt ,...)
{
Int result = x * y;
If (fmt)
{
Printf ("% s", fmt );
}
Return result;
}

# Define rectangle_area (x, y, args...) mult (x, y, # args)
# Define square_area (x, args...) rectangle_area (x, x, # args)
# Define myprintf (args...) square_area (1, # args)

Int main (void)
{
Int s = 0;
S = rectangle_area (3, 4, "this rectangle's area is ");
Printf ("% d \ n", s );

S = rectangle_area (3, 4, "this rectangle's area is", 1, "hahaha ");
Printf ("% d \ n", s );

S = square_area (3, "this square's area is ");
Printf ("% d \ n", s );

S = square_area (3, "this squre's area is", "66666", "Hahahaha", 12345 );
Printf ("% d \ n", s );

Myprintf ("6666666666666666666666666 \ n ");
Myprintf ("hahahahahahahahaha \ n", "66666666666666", 12345 );
Return 0;
}
~
~
~
~
"Chang. c" 34L, 819C written
[Root @ centos-64-min exercise] # gcc-o chang. c
[Root @ centos-64-min exercise] #./chang
This rectangle's area is 12
This rectangle's area is 12
This square's area is 9
This squre's area is 9
6666666666666666666666666
Hahahahahahahahahaha

 

Simple analysis:

Myprintf ("hahahahahahahahaha \ n", "66666666666666", 12345 );

Equivalent:

Square_area (1, "hahahahahahahahaha \ n", "66666666666666", 12345 );

Equivalent:

Rectangle_area (1, 1, "hahahahahahahahaha \ n", "66666666666666", 12345 );

Equivalent:

Mult (1, 1, "hahahahahahahahaha \ n", "66666666666666", 12345 );

Mult only reads mult (1, 1, "hahahahahahahahaha \ n ");

Here: The # args values defined by define at each layer are passed to args ..., top-level # args <=> const char * fmt ,... so in the end, mult only reads the variable of the first string of args, followed by the variable... processed

 

Related Article

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.