Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use variable parameters in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today Xiaobian to share with you how to use variable parameters in C language related knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you read this article after some gains, let's learn about it together.

What are variable parameters?

In C programming, we sometimes encounter functions with variable number of parameters, such as printf() function, whose prototype is:

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

In addition to a fixed parameter format, the number and type of parameters followed by it are variable (with three dots "…" as parameter placeholders), and the actual call can have the following form:

printf("%d",i); printf("%s",s); printf("the number is %d ,string is:%s", i, s);

These things are familiar to everyone. But how exactly to write variable-argument C functions and how these variable-argument function compilers are implemented has been bothering me for a long time. This article carries on some discussions on this question, hoped that can some help to everybody.

Write a simple C function with variable parameters

Let's look at the example program first. The function has at least one integer argument followed by the placeholder…, indicating that the number of subsequent arguments is variable. In this example, all input arguments must be integers, and the function prints the values of all arguments.

The function code is as follows:

//Sample Code 1: Use of Variable Parameter Functions #include "stdio.h" #include "stdarg.h" void simple_va_fun(int start,...) { va_list arg_ptr; int nArgValue =start; int nArgCout=0; //The number of variable arguments va_start(arg_ptr,start); //Determines the memory start address of the variable argument starting from the address of the fixed argument. do { ++nArgCout; printf("the %d th arg: %d\n",nArgCout,nArgValue); //outputs the value of each parameter nArgValue = va_arg(arg_ptr,int); //gets the value of the next variable parameter} while(nArgValue != -1); return; } int main(int argc, char* argv[]) { simple_va_fun(100,-1); simple_va_fun(100,200,-1); return 0; }

From the implementation of this function, we can see that using variable parameters should have the following steps:

(1) The following macros will be used in the program:

void va_start( va_list arg_ptr, prev_param ); type va_arg( va_list arg_ptr, type ); void va_end( va_list arg_ptr );

va here means variable-argument.

These macros are defined in stdarg.h, so programs that use variable parameters should include this header file.

(2) The function defines a variable of type va_list, here arg_ptr, which is a pointer to the address of the parameter. Because after getting the address of the parameter, combined with the type of the parameter, you can get the value of the parameter.

(3) Then initialize the variable arg_ptr defined in (2) with the va_start macro. The second parameter of this macro is the previous parameter in the variable parameter list, that is, *** a fixed parameter.

(4) Then use va_arg macro in turn to make arg_ptr return the address of the variable parameter. After obtaining this address, combined with the type of the parameter, you can get the value of the parameter. Then output it.

(5) Set the end condition, where the condition is to judge whether the parameter value is-1. Note that the function being called does not know the correct number of variable arguments, and the programmer must specify the termination condition himself in the code.

The above is "C language variable parameters how to use" all the content of this article, thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report