Can I combine multiple specifiers?

Yes, you can combine multiple format specifiers in a single printf statement to format different types of data together.

Here’s an example that combines both %5d for an integer and %-10s for a string:

#include <stdio.h>

int main() {
    int num = 42;
    char str[] = "Hello";

    printf("%5d %-10s\n", num, str); // Output: "   42 Hello     "
    return 0;
}

In this example:

  • The integer 42 is right-aligned in a field of width 5.
  • The string "Hello" is left-aligned in a field of width 10.

You can add more specifiers as needed for additional variables. Just ensure that the order of the specifiers matches the order of the variables provided in the printf function. If you have more questions, feel free to ask!

0 Comments

no data
Be the first to share your comment!