The "C language" simulation implements the printf function (variable parameter)

Source: Internet
Author: User

First, the printf function introduces the printf function

The printf function is a formatted output function that is typically used to output information to a standard output device in a prescribed format.

printf prototype

int printf (const char* format, [argument] ...); Defined in the Stdio.h

The above part comes from the high-minor blog (a very good UI, no ads, a pure green blog, suggest that you collect)

Second, the use of variable parameter list simulation to achieve PRINTF1. Analyze printf functions

printf ("Hello haohaosong!\n");

printf ("%s", "Welcome to My bolg\n");

printf ("hell%c\n", ' O ');

We find that the parameters of the printf function are indeterminate

Other things must be used in the place of the reference.

2. Workaround

Using STDARG macros to solve variable parameter problems

Header file:<stdarg.h>

va_list;

Type Va_arg (va_list arg_ptr, type);

void Va_end (Va_list arg_ptr);

void Va_start (Va_list arg_ptr, Prev_param); (ANSI version)

Under VC6.0, we go to the definition and we can clearly see the definition of these macros:


Analysis

Va_list is an identifier defined with a macro, which is a pointer to a character type

Va_start (AP,V) takes the address of the variable defined by the Va_list and adds the number of variable elements

Va_arg (ap,t) takes the contents of the pointer each time, and moves the pointer back inside the macro

Va_end (AP) points The original pointer to null to prevent the wild pointer from appearing

Third, the specific code block:
#include <stdio.h> #include <stdarg.h>int my_printf (char* str,...) {va_list arg;//defines the char* variable argint count = 0;char* str_tmp = Null;va_start (ARG,STR);//Initialize for ARG while (*str! = '} ') {switch (* STR) {case ' C ':p Utchar (Va_arg (Arg,int));//Remove the character of a parameter and print count++;break;case ' s ': str_tmp = (char*) va_arg (arg,int);// Remove the address of a parameter because this is the string while (*str_tmp! = ')//Use the dereference to output {Putchar (*str_tmp); count++;str_tmp++;} Break;default:putchar (*STR);//Not for ' C ' or ' s ', then print it directly count++;break;} str++;} Va_end (ARG);//point arg to NULL to prevent the wild pointer return count;} int main () {my_printf ("s CCC", "Hello", ' h ', ' h ', ' s '); return 0;}
Operation Result:

The "C language" simulation implements the printf function (variable parameter)

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.