Variable parameters practice

参考1: https://www.cnblogs.com/edver/p/8419807.html

参考2:https://blog.csdn.net/iynu17/article/details/51588199

Reference 3:  https://www.cnblogs.com/fanzhidongyzby/p/3519838.html

 

 

#include <iostream>
#include <stdarg.h>
#include <stdio.h>


using namespace std;

//============================================================================
//总结:
//============================================================================


void fun(int paramCount, ...){
    cout<<"参数个数:"<<paramCount<<endl;

    va_list params;
    va_start(params, paramCount);

    while(paramCount--){

        char* strParam = va_arg(params, char*);
        int intParam = va_arg(params, int);
        cout<< strParam << endl;
        cout<< intParam << endl;
    }
    va_end(params);
}


void print11(const char *format, ...)
{
    va_list arg;
    va_start(arg, format);

    while (*format)
    {
        char ret = *format;
        if (ret == '%')
        {
            switch (*++format)
            {
            case 'c':
            {
                        char ch = va_arg(arg, char);
                        putchar(ch);
                        break;
            }
            case 's':
            {
                        char *pc = va_arg(arg, char *);
                        while (*pc)
                        {
                            putchar(*pc);
                            PC ++ ; 
                        } 
                        BREAK ; 
            } 
            default :
                 BREAK ; 
            } 
        } 
        the else 
        { 
            the putchar ( * the format); 
        } 
        the format ++ ; 
    } 
    to va_end (Arg); 
} 

int main () { 

    COUT << " function of the variable parameter Practice: " << endl; 

    // 2 refers to the two pairs of parameters 
    Fun ( 2 , " Welcome1: ",1,"Welcome2:",2);


    print11("%s %s %c%c%c%c%c!\n", "welcome", "to", 'C', 'h', 'i', 'n', 'a');


    cout << "end." << endl;

    return 0;
}

 

problem:

 

Guess you like

Origin www.cnblogs.com/do-your-best/p/11110959.html