w3resource

C programming: Example of C custom printf() function

Implementing a custom printf() function in C.

Here is an example implementation of a custom printf() function that supports a subset of format specifiers (Character, String, Integer and Double).

Note: This implementation does not support all of the format specifiers that printf() does, and it does not implement all of the functionality of printf().

Code:

#include <stdio.h>
#include <stdarg.h>
void custom_printf(const char* format, ...) {
    va_list arg_list;
    va_start(arg_list, format);
    while (*format != '\0') {
        if (*format == '%') {
            format++;
            if (*format == 'd') {
                int val = va_arg(arg_list, int);
                printf("%d", val);
            }
            else if (*format == 's') {
                char* val = va_arg(arg_list, char*);
                printf("%s", val);
            }
            else if (*format == 'c') {
                char val = va_arg(arg_list, int);
                printf("%c", val);
            }
            else if (*format == 'f') {
                double val = va_arg(arg_list, double);
                printf("%f", val);
            }
            format++;
        }
        else {
            printf("%c", *format);
            format++;
        }
    }
    va_end(arg_list);
}
int main() {
    int x = 100;
    double d = 23.56;
    char c = 'C';
    char* str = "Custome printf() function!";
    custom_printf("Example of C custom printf() function:\n");
    custom_printf("\nCharacter: %c\n", c);
	custom_printf("String: %s\n", str);
	custom_printf("Integer: %d\n", x);
    custom_printf("Double: %f\n", d);        
    return 0;
}

Output:

Example of C custom printf() function:
Character: C
String: Custome printf() function!
Integer: 100
Double: 23.560000

This implementation uses va_list, va_start(), va_arg(), and va_end() from the header to support variable argument lists. It then uses a loop to iterate through the format string, handling each format specifier as it is encountered.

Contribute your code and comments through Disqus.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - How do you pass a function as a parameter in C?

Declaration

A prototype for a function which takes a function parameter looks like the following:

void func ( void (*f)(int) );

This states that the parameter f will be a pointer to a function which has a void return type and which takes a single int parameter. The following function (print) is an example of a function which could be passed to func as a parameter because it is the proper type:

void print ( int x ) {
  printf("%d\n", x);
}

Function Call

When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this:

func(print);

would call func, passing the print function to it.

Function Body

As with any parameter, func can now use the parameter's name in the function body to access the value of the parameter. Let's say that func will apply the function it is passed to the numbers 0-4. Consider, first, what the loop would look like to call print directly:

for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
  print(ctr);
}

Since func's parameter declaration says that f is the name for a pointer to the desired function, we recall first that if f is a pointer then *f is the thing that f points to (i.e. the function print in this case). As a result, just replace every occurrence of print in the loop above with *f:

void func ( void (*f)(int) ) {
  for ( int ctr = 0 ; ctr < 5 ; ctr++ ) {
    (*f)(ctr);
  }
}

Ref : https://bit.ly/3skw9Um