Create function simulates the function of printf by using the form of a mutable parameter list

Source: Internet
Author: User

★ Create function simulates the function of printf with the form of a variable parameter list.

Simulates the output form of a simple input single character and string

such as: input:%c%c%c%c%c\t%s, ' h ', ' e ', ' l ', ' l ', ' o ', ' Welcome to here! '

Output: H e l l o Welcome to here!

#include <stdio.h> #include <stdlib.h> #include <stdarg.h>    // The Stdarg header file needs to be introduced in order to establish a variable parameter list void new_printch (CHAR&NBSP;CH)     //for a single character processing function {Putchar (CH);        //It is important to remember not to use the output form of printf, otherwise there is no need to write this function}void my_printstr (char* &NBSP;STR)     //for the output of the string, invokes a single character output function and keeps the pointer moving back until it encounters ' \ ', representing a complete string {while  (*str) {New_ Printch (*str++);}} Void my_print (char* val,  ...)   //char* represents the offset of the pointer at the time the stack is pressed, offset the size of a character pointer {char* vargpch = null;char vargch = 0 ;int  vargint = 0;char *pval = null;va_list vp;     //va_list,va_start,va_end is a variable parameter list representation, note that the parameter after start, if the write error or write the inverse of the pointer offset must be wrong, out of bounds to the unpredictable random value va_start (vp, val); pval = val;while  (*pval) {if  (*pval ==  '% ') {switch  (* (++pval)) {case  ' C ' :     //entry Vargch = va_arg (vp, int) for single character processing; new_printCH (VARGCH);break;case  ' s ':     //for string processing of the entry Vargpch = va_arg (vp, char*); My_printstr (vargpch); break;default:break;} pval++;} Else{new_printch (*pval++);}} Va_end (VP);} Int main () {my_print ("%c %c %c %c %c\t%s\n", ' h ', ' e ', ' l ', ' l ', ' o ', "welcome to  Here! "); System ("pause"); return 0;}

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/75/C2/wKioL1ZB53iydPufAAAWvrMjHNQ311.png "title=" Run Result " alt= "Wkiol1zb53iydpufaaawvrmjhnq311.png"/>

This article is from the "Warm Smile" blog, please be sure to keep this source http://10738469.blog.51cto.com/10728469/1711585

Create function simulates the function of printf by using the form of a mutable parameter list

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.