C and pointer Chapter 7 V

Source: Internet
Author: User

Implements a simplified printf function that can process formats such as % d, % F, % s, and % C.

 

/*************************************** * *********************************> File Name: permutation. c> created time: tuesday, June 17, 2014, 34 seconds ******************************** **************************************** /# include <stdio. h> # include <stdarg. h> # include <stdlib. h> // output the normal string int printstr (char const * Str) {int Len = 0; while (* Str! = '\ 0') {putchar (* STR ++); Len ++;} return Len ;}// description, a simplified printf function, this function is in the format of % d, % C, % F, and % S. Other formats are undefined. Int myprintf (char const * format ,...) {int Len = 0; int sublen; va_list vlist; va_start (vlist, format); // obtain the first address of the first parameter while (* Format! = '\ 0') {char data [50]; int sublen = 1; if (* Format ++ =' % ') {If (* format = 'D') {int d = va_arg (vlist, INT); char dd [10]; ITOA (D, DD, 10 ); sublen = printstr (dd);} else if (* format = 'C') {char c = va_arg (vlist, char); putchar (c); sublen = 1 ;} else if (* format = 's') {char * STR = va_arg (vlist, char *); sublen = printstr (STR );} else if (* format = 'F') {double D = va_arg (vlist, double); char dd [20]; gcvt (D, 10, DD ); sublen = printstr (dd);} format ++; printf ("\ n");} Len + = sublen;} va_end (vlist); Return Len;} int main () {int num = myprintf ("% d % C % S % F", 4, 'A', "1213", 3.14); printf ("length of output characters (space, except carriage return): % d \ n ", num); Return 0 ;}

 

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.