Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

10,405 questions

14,169 answers

573 users

How to write you own printf() that take different number different types of arguments in C

1 Answer

0 votes
#include <stdio.h>
#include <stdarg.h>
 
void my_printf(const char* fmt, ...) {
    va_list args;
    va_start(args, fmt);
 
    while (*fmt != '\0') {
        if (*fmt == 'd') {
            int i = va_arg(args, int);
            printf(" %4d", i);
        } else if (*fmt == 'c') {
            int ch = va_arg(args, int);
            printf(" %3c", ch);
        } else if (*fmt == 'f') {
            double f = va_arg(args, double);
            printf(" %.6f", f);
        }
        fmt++;
    }
    printf("\n");
 
    va_end(args);
}
 
int main(void)
{
    my_printf("dcf", 100, 'x', 3.14);
    my_printf("cff", 'x', 3.14, 4.872);
}



/*
run:

  100   x 3.140000
   x 3.140000 4.872000

*/

 




answered Apr 7 by avibootz
...