#include <stdio.h>
char str[0xff];
void str_cpy(const char* s, char* d) {
while(*s)
*d++ = *s++;
*d = '\0';
return;
}
int str_len(const char* s) {
int len = 0;
while(*s++)
len++;
return len;
}
int mydiv(int* x, int y) {
if(y == 0)
return -*x;
int tmp;
tmp = *x % y;
*x /= y;
return tmp;
}
char* myitoa(int x, int d){
static char mystr[0x20];
char tmp[0x20];
const char* c = "0123456789abcdef";
int y, i = 0, j;
while(x) {
y = mydiv(&x, d);
tmp[i++] = c[y];
}
tmp[i] = '\0';
j = str_len(tmp);
for(i = 0; i < str_len(tmp); i++){
mystr[i] = tmp[--j];
}
mystr[i] = '\0';
return mystr;
}
void myprintf(const char* s, ...){
char* mystr = str;
char* tmp;
char** varg = (char**)&s;
varg++;
char c;
int len;
while(c = *s) {
if('%' != c) {
*mystr++ = c;
s++;
} else {
s++;
switch(c = *s) {
case 's':
s++;
str_cpy(*varg, mystr);
len = str_len(str);
mystr = str + len;
varg++;
break;
case 'x':
s++;
tmp = myitoa(*((int*)varg), 16);
*mystr++ = '0';
*mystr++ = 'x';
str_cpy(tmp, mystr);
len = str_len(str);
mystr = str + len;
varg++;
break;
case 'd':
s++;
tmp = myitoa(*((int*)varg), 10);
str_cpy(tmp, mystr);
len = str_len(str);
mystr = str + len;
varg++;
break;
default:
break;
}
}
}
}
int main(void) {
myprintf("Hello, %World!\n%s, %s, %d, %s, %x, %s, %x", "How", "are",
0xff, "YOU", 15, "Welcome!", 128);
printf("%s", str);
}