ch***********@hotmail.com wrote:
put float to double
put char to int
why ?
%c is supposed to take an int argument in a printf call.
printf("%f", pdouble);
%f takes a double argument in a printf call.
va_end(stage); // :) i lost it befor
but i find the function still work well, so .....
"Nothing quite encourages as does one's first unpunished crime."
http://fr.ebookslib.com/?a=sa&b=1089
i want to read the printf() code!
can you post it? thank you
min_printf is as far as I've gone
trying to code something like printf.
/* BEGIN min_printf.c */
#include <stdarg.h>
#include <stdio.h>
/*
** Only 5 features from stdio.h,
** are used in min_printf.c:
**
** 1 putc(c, stream)
** 2 stdout
** 3 FILE
** 4 EOF
** 5 int ferror(FILE *stream);
*/
#define put_c(c, stream) (putc((c), (stream)))
#define put_char(c) (put_c((c), stdout))
#define fsput_char(c) \
(put_char(c) == EOF && ferror(stdout) != 0 ? EOF : 1)
int min_printf(const char *s, ...);
static int fsput_s(const char *s);
static int fsput_u(unsigned integer);
static int fsput_u_plus_1(unsigned integer);
static int fsput_d(int integer);
int main(void)
{
min_printf(
"\n%%u 9u is %u.\n%%d 9 is %d.\n"
"%%s \"nine\" is %s.\n%%c '9' is %c.\n",
9u, 9, "nine", '9'
);
return 0;
}
static int fsput_s(const char *s)
{
int count;
for (count = 0; *s != '\0'; ++s) {
if (put_char(*s) == EOF && ferror(stdout) != 0) {
return EOF;
}
++count;
}
return count;
}
static int fsput_d(int integer)
{
int count;
if (0 > integer) {
if (put_char('-') != EOF) {
count = fsput_u_plus_1(-(integer + 1));
if (count != EOF) {
++count;
}
} else {
count = EOF;
}
} else {
count = fsput_u(integer);
}
return count;
}
static int fsput_u(unsigned integer)
{
int count;
unsigned digit, tenth;
tenth = integer / 10;
digit = integer - 10 * tenth + '0';
count = tenth != 0 ? fsput_u(tenth) : 0;
return count != EOF && put_char(digit) != EOF ? count + 1 : EOF;
}
static int fsput_u_plus_1(unsigned integer)
{
int count;
unsigned digit, tenth;
tenth = integer / 10;
digit = integer - 10 * tenth + '0';
if (digit == '9') {
if (tenth != 0) {
count = fsput_u_plus_1(tenth);
} else {
count = put_char('1') == EOF ? EOF : 1;
}
digit = '0';
} else {
count = tenth != 0 ? fsput_u(tenth) : 0;
++digit;
}
return count != EOF && put_char(digit) != EOF ? count + 1 : EOF;
}
int min_printf(const char *s, ...)
{
int count, increment;
va_list ap;
va_start(ap, s);
for (count = 0; *s != '\0'; ++s) {
if (*s == '%') {
switch (*++s) {
case 'c':
increment = fsput_char(va_arg(ap, int));
break;
case 'd':
increment = fsput_d(va_arg(ap, int));
break;
case 's':
increment = fsput_s(va_arg(ap, char *));
break;
case 'u':
increment = fsput_u(va_arg(ap, unsigned));
break;
default:
increment = fsput_char(*s);
break;
}
} else {
increment = fsput_char(*s);
}
if (increment != EOF) {
count += increment;
} else {
count = EOF;
break;
}
}
va_end(ap);
return count;
}
/* END min_printf.c */
--
pete