OK, here's my program.
#include <stdio.h>
#include <string.h>
void minprintf(char *fmt, ...);
/* prints a few lines of output, to test minprintf */
int main()
{
int i = 321;
double ld = 43.5939232223;
char s[] = "The quick brown fox jumps over the lazy dog.";
minprintf("%s\n", s);
minprintf("%10d %10.3s\n", i, s);
minprintf("%10.3lf %10.3s\n", ld, s);
return 0;
}
#include <stdarg.h>
/* minprintf: minimal printf with variable argument list */
void minprintf(char *fmt, ...)
{
va_list ap; /* points to each unnamed arg in turn */
char *p, *sval;
char cval;
int ival;
short int sival;
long int lival;
double dval;
long double ldval;
char opt[25];
int i;
va_start(ap, fmt); /* make ap point to 1st unnamed arg */
for (p = fmt; *p; p++) {
if (*p != '%') {
putchar(*p);
continue;
}
i = 0;
opt[i++] = '%';
if (*++p == '-') {
opt[i++] = '-';
p++;
}
while (isdigit(*p)) {
opt[i++] = *p;
p++;
}
if (*p == '.') {
opt[i++] = '.';
p++;
}
while (isdigit(*p)) {
opt[i++] = *p;
p++;
}
opt = '\0';
switch(*p) {
case 'c':
cval = va_arg(ap, char);
strcat(opt, "c"
;
printf(opt, cval);
break;
case 'd':
ival = va_arg(ap, int);
strcat(opt, "d"
;
printf(opt, ival);
break;
case 'f':
dval = va_arg(ap, double);
strcat(opt, "f"
;
printf(opt, dval);
break;
case 's':
sval = va_arg(ap, char *);
strcat(opt, "s"
;
printf(opt, sval);
break;
case 'h':
strcat(opt, "h"
;
switch (*++p) {
case 'd':
sival = va_arg(ap, short int);
strcat(opt, "d"
;
printf(opt, sival);
break;
default:
strcat(opt, p);
printf("%s", opt);
break;
}
break;
case 'l':
strcat(opt, "l"
;
switch (*++p) {
case 'd':
lival = va_arg(ap, long int);
strcat(opt, "d"
;
printf(opt, lival);
break;
case 'f':
ldval = va_arg(ap, long double);
strcat(opt, "f"
;
printf(opt, ldval);
break;
default:
strcat(opt, p);
printf("%s", opt);
break;
}
break;
default:
strcat(opt, p);
printf("%s", opt);
break;
}
}
va_end(ap); /* clean up when done */
}
Now, when I run it, here's what I get:
The quick brown fox jumps over the lazy dog.
321 The
43.594 ŠM
Everything works fine, except for that final time I try to print the string. Could someone help me with this? I'm thinking it has something to do with that long double argument not being completely read with va_arg, but I'm not sure.
#include <stdio.h>
#include <string.h>
void minprintf(char *fmt, ...);
/* prints a few lines of output, to test minprintf */
int main()
{
int i = 321;
double ld = 43.5939232223;
char s[] = "The quick brown fox jumps over the lazy dog.";
minprintf("%s\n", s);
minprintf("%10d %10.3s\n", i, s);
minprintf("%10.3lf %10.3s\n", ld, s);
return 0;
}
#include <stdarg.h>
/* minprintf: minimal printf with variable argument list */
void minprintf(char *fmt, ...)
{
va_list ap; /* points to each unnamed arg in turn */
char *p, *sval;
char cval;
int ival;
short int sival;
long int lival;
double dval;
long double ldval;
char opt[25];
int i;
va_start(ap, fmt); /* make ap point to 1st unnamed arg */
for (p = fmt; *p; p++) {
if (*p != '%') {
putchar(*p);
continue;
}
i = 0;
opt[i++] = '%';
if (*++p == '-') {
opt[i++] = '-';
p++;
}
while (isdigit(*p)) {
opt[i++] = *p;
p++;
}
if (*p == '.') {
opt[i++] = '.';
p++;
}
while (isdigit(*p)) {
opt[i++] = *p;
p++;
}
opt = '\0';
switch(*p) {
case 'c':
cval = va_arg(ap, char);
strcat(opt, "c"
printf(opt, cval);
break;
case 'd':
ival = va_arg(ap, int);
strcat(opt, "d"
printf(opt, ival);
break;
case 'f':
dval = va_arg(ap, double);
strcat(opt, "f"
printf(opt, dval);
break;
case 's':
sval = va_arg(ap, char *);
strcat(opt, "s"
printf(opt, sval);
break;
case 'h':
strcat(opt, "h"
switch (*++p) {
case 'd':
sival = va_arg(ap, short int);
strcat(opt, "d"
printf(opt, sival);
break;
default:
strcat(opt, p);
printf("%s", opt);
break;
}
break;
case 'l':
strcat(opt, "l"
switch (*++p) {
case 'd':
lival = va_arg(ap, long int);
strcat(opt, "d"
printf(opt, lival);
break;
case 'f':
ldval = va_arg(ap, long double);
strcat(opt, "f"
printf(opt, ldval);
break;
default:
strcat(opt, p);
printf("%s", opt);
break;
}
break;
default:
strcat(opt, p);
printf("%s", opt);
break;
}
}
va_end(ap); /* clean up when done */
}
Now, when I run it, here's what I get:
The quick brown fox jumps over the lazy dog.
321 The
43.594 ŠM
Everything works fine, except for that final time I try to print the string. Could someone help me with this? I'm thinking it has something to do with that long double argument not being completely read with va_arg, but I'm not sure.