Implementation of variable parameters and printf functions in C Language

Source: Internet
Author: User
Tags switch case

Implementation of variable parameters and printf functions in C Language
Today, I learned about variable parameters in C language. The variable parameters in C language mainly depend on <strarg. h> This header file is implemented. This header file contains three macros: va_list (), va_start (), and va_end (). Its usage is to declare a va_list variable first, it is used to access the unconfirmed part of the parameter list. This variable is initialized by va_start. Its first parameter is the name of the va_list variable, and the second parameter is the last parameter with a name before the ellipsis. During initialization, The va_list variable is set to the first parameter pointing to a variable parameter. To access parameters, va_arg is required. This macro uses two parameters (the type of the next parameter in the va_list variable and Parameter List) to access the last parameter and needs to call va_end (), the format is as follows:

void fun(int a,...){va_list parameter;va_start(parameter, a);va_arg(parameter, int );}

 

For example, if you want to calculate the average of multiple numbers, because the default parameter is not allowed in C, you can only calculate the average using the above method. The Code is as follows:
Int average (int val ,...) {va_list arg; int sum = 0; int I = 0; va_start (arg, val); // va_arg (); for (I = 0; I <val; I ++) {sum + = va_arg (arg, int); // get one next each call} va_end (arg); return sum/val ;}

 

If you require a maximum of multiple numbers, you can write it
int average(int val, ...){va_list arg;int max;int sum = 0;int i = 0;va_start(arg, val);for (i = 0; i < val; i++){sum = va_arg(arg, int);if (sum>max)max = sum;}va_end(arg);return max;}

 

We can also use the stdarg macro to simulate the implementation of the printf function. Here I just simulate the string output, and the output code of the integer and struct types is as follows:
# Include <stdio. h> # include <stdarg. h> void printd (int n) // output the integer in bytes type {if (n <0) {putchar ('-');} if (n) {printd (n/10); putchar (n % 10 + '0') ;}} void my_printf (char * val ,...) {int ch; va_list arg; va_start (arg, val); while (* val! = '\ 0') {switch (* val) {case' % ': // run the switch case statement {if (* (val + 1) in %) = 'C') // output character {ch = va_arg (arg, char); putchar (ch); val ++; // shift the pointer variable down to a unit} else if (* (val + 1) = 'D') {ch = va_arg (arg, char ); // output integer printd (ch); val ++;} else if (* (val + 1) ='s ') // output string {char * p = va_arg (arg, char *); while (* p! = '\ 0') {putchar (* p); p ++;} val ++; // point to the next character of the first variable} elseputchar (' % '); break;} default: {putchar (* val); break;} val ++;} va_end (arg) ;} int main () // program debugging {int c = 100; char * p = "laomasb"; char a = 'C '; my_printf ("% s \ n % d \ n % c \ n", p, c, a); system ("pause"); return 0 ;}

 

The code is still quite poorly written here. For example, if % is followed by a number, it cannot be identified and the output space is rushed, and the code is relatively low. Hope you can give me some suggestions.

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.