Explain different types of format specifiers used in C with examples.

In C, format specifiers are used with functions like printf() and scanf() to control the input and output format of variables. Below are some commonly used format specifiers:

  • %d or %i: Used for printing or reading integers.

    int num = 10;
    printf("%d", num); // Output: 10
  • %f: Used for printing or reading floating-point numbers (e.g., float or double).

    float num = 3.14;
    printf("%f", num); // Output: 3.140000
  • %c: Used for printing or reading a single character.

    char letter = 'A';
    printf("%c", letter); // Output: A
  • %s: Used for printing or reading a string.

    char name[] = "John";
    printf("%s", name); // Output: John
  • %lf: Used for printing double values (floating-point numbers with double precision).

    double num = 3.14159265359;
    printf("%lf", num); // Output: 3.141593
  • %x: Used for printing integers in hexadecimal format.

    int num = 255;
    printf("%x", num); // Output: ff

Each format specifier is used to correctly interpret and print the corresponding type of data. These specifiers help control how the data is represented in the output.