Output % d, % c, % p, % s in the basic format of printf in c Language
As for the implementation of printf, I must have read many basic knowledge from my previous articles. Well, let's not talk about it anymore. Go to the source code and look at a simple implementation method:
# Include
# Define myfflush (out) do {} while (0) typedef int uint32_t; // output decimal number static void print_Dec (uint32_t n) {if (n> = 10) {// call print_Dec recursively (n/10); n % = 10;} putchar (char) (n + '0 '));} // output hexadecimal number static void print_Hex (unsigned int hex) {int I = 8; putchar ('0'); putchar ('x'); while (I --) {unsigned char c = (hex & 0xF0000000)> 28; putchar (c <0xa? C + '0': c-0xa + 'A'); hex <= 4 ;}/// output string void print_String (const char * s) {while (* s) {putchar (* s); s ++ ;}}// output character void print_char (char ch) {putchar (ch );} typedef unsigned long volatile ulv; typedef unsigned long ul; int main (void) {// output 10 hexadecimal number print_Dec (10); putchar ('\ n '); // output hexadecimal number print_Hex (0xa); putchar ('\ n'); // output string print_String ("hello world"); myfflush (stdout ); return 0 ;}
Running result: