[C Language] function calls that support named Parameters

Source: Internet
Author: User
Tags define function

For functions with many parameters, such as UI library functions, it is difficult to remember the parameter types and meanings at each location, especially in the development environment where your ide is relatively simple, you may need to query documents frequently.

For languages like python, native supports named parameters, such

Def func (name, age): Print name, agefunc ('aaa', 1) func (age = 2, name = 'bbb ')

Maybe your function has 10 parameters, most of which can have default values. Therefore, if you need to specify the values of 3rd and 5th parameters, when other parameters use the default behavior, It is very convenient to call "func (arg3 = 3, arg5 = 5.

 

To implement the features of named parameters in c89 (My compiler is still GCC 4.7.2), you can use the techniques mentioned in this article.

The following is the macro I wrote:

# Include <stdio. h> # define function (Ret, funcname ,...) struct _ ARGs _ # funcname {__ va_args __}; RET funcname (struct _ ARGs _ # funcname ARGs) # define call (funcname ,...) func (struct _ ARGs _ # funcname) {__ va_args __}) function (void, func, const char * Name; int age;) {printf ("Name: % s \ n age: % d \ n ", argS. name, argS. age);} int main () {call (func, "aa123", 10); Call (func ,. age = 5 ,. name = "abc456"); Call (func ,. name = "def789"); Call (func ,. age = 11 );}

Moreover, compared with common functions, functions defined in this way are basically non-destructive in performance.

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.