Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdarg.h>
- #include <stdio.h> //for real printf and putchar
- void myprintf(const char *, ...);
- void _p_myprintf_int(int arg)
- {
- printf("%d", arg); //we're just going to use real printf for simplicity
- }
- void _p_myprintf_unsigned(unsigned arg)
- {
- printf("%u", arg); //for simplicity
- }
- void _p_myprintf_string(char *str)
- {
- while (*str) //while current char is not null terminator
- putchar(*str++); //put it to the screen, increment current
- }
- void myprintf(const char *fmt, ...)
- {
- va_list args; //this is actually just a placeholder to our first (variable) argument
- va_start(args, fmt); //set up our list so we can retrieve it
- while (*fmt) //loop through each character of the string
- {
- if (*fmt++ == '%') //our control character
- { //we increment it as well so our next character is our type spec
- switch (*fmt++) //case-select our list, and increment it as well.
- {
- case 'i': //integer
- break;
- case 'u': //unsigned int
- break;
- case 's': //string
- break;
- }
- }
- }
- va_end(args);
- }
Add Comment
Please, Sign In to add comment