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:


%d or %i: Used to display signed integer values. A signed integer can encompass both positive and negative values.

%u: Employed for presenting unsigned integer values. An unsigned integer, in this context, can only hold positive values.

%o: Outputs the octal representation of an unsigned integer. Octal integer values always commence with a '0'.

%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).

%X: Similar to %x, but with uppercase alphabetical characters (e.g., A, B, C).

%f: Used for printing decimal floating-point values. By default, it displays six values after the decimal point.

%e/%E: Outputs values in scientific notation, also known as Mantissa or Exponent.

%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.

%p: Displays the address in hexadecimal form.

%c: Outputs an unsigned character.

%s: Prints strings.

%ld: Utilized to display long signed integer values.

Let's understand the format specifiers in detail through an 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


#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