FORMAT SPECIFIER IN C

Suriya Ravichandran



Format Specifier In C

In C programming, a format specifier is a string utilized in formatted input and output functions. The format string, crucial in determining the presentation of input and output, invariably commences with a '%' character.

The Following Are Commonly Used Format Specifiers In The printf() Function

  1. %d or %i: Used to display signed integer values. A signed integer can encompass both positive and negative values.
  2. %u: Employed for presenting unsigned integer values. An unsigned integer, in this context, can only hold positive values.
  3. %o: Outputs the octal representation of an unsigned integer. Octal integer values always commence with a '0'.
  4. %x: Prints the hexadecimal representation of an unsigned integer. Hexadecimal integer values commence with '0x', and alphabetical characters are in lowercase (e.g., a, b, c).
  5. %X: Similar to %x, but with uppercase alphabetical characters (e.g., A, B, C).
  6. %f: Used for printing decimal floating-point values. By default, it displays six values after the decimal point.
  7. %e/%E: Outputs values in scientific notation, also known as Mantissa or Exponent.
  8. %g: Presents decimal floating-point values using fixed precision, ensuring that the value after the decimal in the input is exactly the same as in the output.
  9. %p: Displays the address in hexadecimal form.
  10. %c: Outputs an unsigned character.
  11. %s: Prints strings.
  12. %ld: Utilized to display long signed integer values.

Example Of Format Specifiers In C

Let as consider format specifiers in detailed example:

1. Program To Using %d And %i In printf:

Program:


   #include <stdio.h>
   //int value to print using %d & %i
   int main(){
int a=10;
printf("using %%d to print a=%d\n",a);
printf("using %%i to print a=%i\n",a);
  }


Output:


  using %d to print a=10
  using %i to print a=10



2. Program To Using %X Print The Int Value In Hexadecimal

Program:


   #include<stdio.h>
   int main(){
//print hexadecimal value of a using %X
int a=11;
printf("using %%X hexadecimal of a =%X\n",a);
printf("using %%#X hexadecimal of a =%#X\n",a);
  }


Output:


  using %X hexadecimal of a =B
  using %#X hexadecimal of a =0XB



3. Program To All Format Specifier

Program:


  #include <stdio.h>

  int main() {
    int integerVar = 42;
    unsigned int unsignedVar = 99;
    char charVar = 'A';
    float floatVar = 3.14;
    double doubleVar = 2.71828;
    long longVar = 123456789;

    // Using %d to print a signed integer
    printf("Signed Integer: %d\n", integerVar);

    // Using %u to print an unsigned integer
    printf("Unsigned Integer: %u\n", unsignedVar);

    // Using %c to print a character
    printf("Character: %c\n", charVar);

    // Using %f to print a float
    printf("Float: %f\n", floatVar);

    // Using %lf to print a double
    printf("Double: %lf\n", doubleVar);

    // Using %ld to print a long integer
    printf("Long Integer: %ld\n", longVar);

    // Using %x to print an integer in hexadecimal (lowercase)
    printf("Hexadecimal (lowercase): %x\n", integerVar);

    // Using %X to print an integer in hexadecimal (uppercase)
    printf("Hexadecimal (uppercase): %X\n", integerVar);

    // Using %e to print a float in scientific notation
    printf("Scientific Notation: %e\n", floatVar);

    // Using %g to print a float in a compact format
    printf("Compact Format: %g\n", floatVar);

    // Using %p to print the address of a variable
    printf("Address: %p\n", (void*)&integerVar);

    // Using %s to print a string
    printf("String: %s\n", "Hello, World!");

    return 0;
  }


Output:


  Signed Integer: 42
  Unsigned Integer: 99
  Character: A
  Float: 3.140000
  Double: 2.718280
  Long Integer: 123456789
  Hexadecimal (lowercase): 2a
  Hexadecimal (uppercase): 2A
  Scientific Notation: 3.140000e+00
  Compact Format: 3.14
  Address: 0x7ffd50b4ae4c
  String: Hello, World!




More topic in C

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send