LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   this is how printf works (https://www.linuxquestions.org/questions/programming-9/this-is-how-printf-works-757383/)

smeezekitty 09-23-2009 07:42 PM

this is how printf works
 
i wrote my own verison of printf and wanted to show off
the first param is a function to print a null terminated string
Code:


#define PERCENT_D (10)
#define PERCENT_X (16)

void printf(void (*out)(char *), const char *format, ...){ //void instead of int
int v;
char u[10],t=0;
va_list va;
va_start(va, format);
while(format[t]){
if(format[t]!='%'){u[0]=format[t]; u[1]=0; out(u);} else {
t++;
switch(tolower(format[t])){
case 'u':
case 'd':
v=va_arg(va, int);
itoa(v, u, PERCENT_D);
out(u);
break;
case 's':
out(va_arg(va, char *));
break;
case 'f':
v=va_arg(va, float);
itoa(v, u, PERCENT_D);
out(u);
break;
case 'x':
case 'X':
v=va_arg(va, int);
itoa(v, u, PERCENT_X);
out(u);
break;
default:out("%");
}
}
t++;
}
va_end(va);
}


carbonfiber 09-24-2009 12:38 PM

Horrible.

P.S.: Constructivecriticism is my middle name.

smeezekitty 09-24-2009 12:47 PM

Quote:

Originally Posted by carbonfiber (Post 3695796)
Horrible.

P.S.: Constructivecriticism is my middle name.

that isnt consturctive criticism
it would be if you say what is horrable

Sergei Steshenko 09-24-2009 01:16 PM

Quote:

Originally Posted by smeezekitty (Post 3694922)
i wrote my own verison of printf and wanted to show off
the first param is a function to print a null terminated string
Code:


void printf(void (*out)(char *), const char *format, ...){ //void instead of int
int v;
char u[10],t=0;
va_list va;
va_start(va, format);
while(format[t]){
if(format[t]!='%'){u[0]=format[t]; u[1]=0; out(u);} else {
t++;
switch(tolower(format[t])){
case 'u':
case 'd':
v=va_arg(va, int);
itoa(v, u, 9);
out(u);
break;
case 's':
out(va_arg(va, char *));
break;
case 'f':
v=va_arg(va, float);
itoa(v, u, 10);
out(u);
break;
case 'x':
case 'X':
v=va_arg(va, int);
itoa(v, u, 16);
out(u);
break;
default:out("%");
}
}
t++;
}
va_end(va);
}


Why did you write your own 'printf' and why should others consider the code ?

...


Whatever you write, if you have constants like 9, 10, 16, etc. instead of meaningful macro names, your code is bad.

The allowed constants are 0 (like you are starting from the number zero element in an array) and +1, -1 - like 'idx = idx + 1;' - meaning next index/value in an array. And because of ++, -- in "C" one rarely needs +1, -1.

Sometimes 2, 3 - like each second or each third element, but 3 is already suspicious.

Hko 09-24-2009 01:29 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3695862)
Whatever you write, if you have constants like 9, 10, 16, etc. instead of meaningful macro names, your code is bad.

Here it is used as the radix of the conversion, one of the few cases I wouldn't consider bad, let alone "horrible"...

Sergei Steshenko 09-24-2009 05:23 PM

Quote:

Originally Posted by Hko (Post 3695878)
Here it is used as the radix of the conversion, one of the few cases I wouldn't consider bad, let alone "horrible"...

Still,

Code:

#define D_RADIX ((unsigned)10) /* for %d conversions */
#define X_RADIX ((unsigned)16) /* for %x conversions */

, i.e. relationship between symbolic ('D', 'X') and numeric (10, 16) radix. And the relationship is reflected in a meaningful macro name.


All times are GMT -5. The time now is 01:58 PM.