✏️ Explanatory Question

How are Format Specifiers used in scanf() function?

👁 0 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

  Key Usage: Used to take user input

The scanf() function uses format specifiers to read input from the user.


#include 

int main() {
    int age;
    float salary;

    printf("Enter age: ");
    scanf("%d", &age);

    printf("Enter salary: ");
    scanf("%f", &salary);

    printf("Age: %d, Salary: %.2f", age, salary);

    return 0;
}
    
Common Mistake Forgetting & symbol in scanf() causes runtime errors.
Best Practice Always pass the address of variables using & operator.

  Important Tips for Format Specifiers

  • Always match the correct specifier with variable type
  • Use precision (.2f) for controlling decimal output
  • Avoid mixing data types incorrectly
  • Use readable formatting for better output display