Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Don't know what's causing this bug

Status
Not open for further replies.

Strogian

Technical User
Nov 11, 2000
36
US
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[] = &quot;The quick brown fox jumps over the lazy dog.&quot;;

minprintf(&quot;%s\n&quot;, s);
minprintf(&quot;%10d %10.3s\n&quot;, i, s);
minprintf(&quot;%10.3lf %10.3s\n&quot;, 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, &quot;c&quot;);
printf(opt, cval);
break;
case 'd':
ival = va_arg(ap, int);
strcat(opt, &quot;d&quot;);
printf(opt, ival);
break;
case 'f':
dval = va_arg(ap, double);
strcat(opt, &quot;f&quot;);
printf(opt, dval);
break;
case 's':
sval = va_arg(ap, char *);
strcat(opt, &quot;s&quot;);
printf(opt, sval);
break;
case 'h':
strcat(opt, &quot;h&quot;);
switch (*++p) {
case 'd':
sival = va_arg(ap, short int);
strcat(opt, &quot;d&quot;);
printf(opt, sival);
break;
default:
strcat(opt, p);
printf(&quot;%s&quot;, opt);
break;
}
break;
case 'l':
strcat(opt, &quot;l&quot;);
switch (*++p) {
case 'd':
lival = va_arg(ap, long int);
strcat(opt, &quot;d&quot;);
printf(opt, lival);
break;
case 'f':
ldval = va_arg(ap, long double);
strcat(opt, &quot;f&quot;);
printf(opt, ldval);
break;
default:
strcat(opt, p);
printf(&quot;%s&quot;, opt);
break;
}
break;
default:
strcat(opt, p);
printf(&quot;%s&quot;, 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.
 
Nevermind, I figured it out. =) (I didn't declare ld as a long double in main(), and I needed to use %Lf for it, not %lf)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top