✏️ Explanatory Question

How are Format Specifiers used in printf() function?

👁 0 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

  Key Usage: Used to control output display

The printf() function uses format specifiers to print variables in a specific format.


#include 

int main() {
    int age = 21;
    float salary = 25000.50;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Salary: %.2f\n", salary);
    printf("Grade: %c\n", grade);

    return 0;
}
    

In this example:

  •   %d prints integer
  •   %.2f prints float with 2 decimal places
  •   %c prints character