format specifiers in C language

Format specifiers in C Programming language

Format specifiers in C are used for input and output purposes. Using format specifier the compiler can understand that what type of data is in input and output operation. For example, what type of data is storing in a variable using scanf or printing using printf decide by format specifiers?

There are some elements that affect the format specifier. Below, I have mentioned elements that affect the format specifier.

1. A minus symbol (-) sign tells left alignment

2. A number after % specifies the minimum field width. If the string is less than the width, it will be  filled with spaces

3. A period (.) is used to separate field width and precision.

List of format specifiers which generally used in programming:

 

Format Specifier Type
%c Character
%d Signed integer
%e or %E Scientific notation of floats
%f Float values
%g or %G Similar as %e or %E
%hi Signed integer (short)
%hu Unsigned Integer (short)
%i integer
%l or %ld or %li Long
%lf Double
%Lf Long double
%lu Unsigned int or unsigned long
%lli or %lld Long long
%llu Unsigned long long
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character

 

Few examples to understand the use of format specifiers with printf() in C:

 

1. Format specifier (character): %c

 

#include <stdio.h>

int main()
{
    char data = 'A';

    printf("%c\n", data);

    return 0;
}

Output: A

 

#include <stdio.h>

int main()
{
    int data = 65;

    printf("%c\n", data);

    return 0;
}

Output: A

In both codes, you can see %c convert data in character and printf function print it on the console.

 

2. Format specifiers (integer): %d, %i, %u

 

#include <stdio.h>

int main()
{
    int data = 65;

    printf("%d\n", data);
    printf("%u\n", data);
    printf("%i\n", data);

    return 0;
}

Output:

65
65
65

 

Difference between %d and %i format specifier in C:

When you are printing using the printf function, there is no specific difference between the %i and %d format specifiers. But both format specifiers behave differently with the scanf function.

The %d format specifier takes the integer number as decimal but the %i format specifier takes the integer number as decimal, hexadecimal or octal type. it means the %i automatically identified the base of the input integer number.

Note: You must put ‘0x’ for hexadecimal number and ‘0’ for octal number while entering the input number.

#include <stdio.h>

int main()
{
    int data1, data2, data3;

    printf("Enter value in decimal format:");
    scanf("%d",&data1);
    printf("data1 = %i\n\n", data1);

    printf("Enter value in hexadecimal format:");
    scanf("%i",&data2);
    printf("data2 = %i\n\n", data2);

    printf("Enter value in octal format:");
    scanf("%i",&data3);
    printf("data2 = %i\n\n", data3);

    return 0;
}

Output:

difference between %i and %d in c

 

3. Format specifiers (float) : %f, %e or %E

 

#include <stdio.h>

int main()
{
    float data = 6.27;

    printf("%f\n", data);
    printf("%e\n", data);

    return 0;
}

Output:

6.270000
6.270000e+000

Use of special elements with %f

 

Example 1:

#include <stdio.h>

int main()
{
    float data = 6.276240;

    printf("%f\n", data);
    printf("%0.2f\n", data);
    printf("%0.4f\n", data);

    return 0;
}

Output:

6.276240
6.28
6.2762

You can see, how we can control the precision of float by placing elements with a format specifier. Here %.2f  and %.4f will restrict the values up to two and four decimal values.

 

Example 2:

#include <stdio.h>

int main()
{
    int pos = 14;
    float data = 1.2;
    printf("%*f",pos,data);

    return 0;
}

Ans:

The output of the above code will be 1.200000 with 6 space.

Explanation:

Here 1.200000 is printing with, 6 spaces, because by giving * in printf we can specify an additional width parameter, here ‘pos’ is the width and ‘data’ is the value. if the number is smaller than the width then rest is filled with spaces.

 

Differences between %f, %e, and %g format specifiers in C language

Let see an example to understand the difference between %f, %e, and %g format specifier.

#include <stdio.h>

int main(void)
{
    double data1 = 123456.0;

    printf("%e\n", data1);
    printf("%f\n", data1);
    printf("%g\n", data1);
    printf("\n");

    double data2 = 1234567.0;

    printf("%e\n", data2);
    printf("%f\n", data2);
    printf("%g\n", data2);
    return 0;
}

Output:

1.234560e+005
123456.000000
123456

1.234567e+006
1234567.000000
1.23457e+006

Explanation:

When using the G ( or g) conversion specifier, the double argument representing a floating-point number is converted in style f or e (or in style F or E ), depending on the value converted and the precision.

 

4. Format specifiers (octal number): %o

 

#include <stdio.h> 

int main() 
{ 
    int data = 65;
    
    printf("%o\n", data); 
    
    return 0; 
}

Output: 101

 

5. Format specifier (Hexadecimal number): %x, %X

 

#include <stdio.h>
int main()
{
    int data = 11;
    printf("%x\n", data);

    return 0;
}

Output: b

 

6. Format specifier (character array or string): %s

 

#include <stdio.h>
int main()
{
    char blogName[] = "aticleworld";

    printf("%s\n", blogName);

    return 0;
}

Output: aticleworld

 

Use of special elements with %s

 

#include <stdio.h>
int main()
{
    char blogName[] = "aticleworld";

    printf("%s\n", blogName);
    printf("%24s\n", blogName);
    printf("%-24s\n", blogName);
    printf("%24.6s\n", blogName);
    printf("%-24.6s\n", blogName);

    return 0;
}

Output:

format specifiers in C

In the below code, you can see how - and + are used for left and right alignment. The value after the decimal represents precision.

Note: A precision explains how many digits come after the decimal part in floating number, number of digits in integer, and number of characters in the string.

 

Few examples to understand the use of format specifiers with scanf() and fgets in C:

The scanf function is equivalent to fscanf with the argument stdin interposed before the arguments to scanf. In simple words, the scanf function reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments.

 

1. Format specifier (character): %c

#include <stdio.h>

int main()
{
    char data;

    scanf("%c", &data); //suppose input 'A'

    printf("%c\n",data);

    return 0;
}

Output: ‘A’

 

2. Format specifiers (integer): %d, %i, %u

#include <stdio.h>

int main()
{
    int data;

    //Use of %d
    scanf("%d",&data);  // input 19
    printf("%d\n", data);

    //Use of %u
    scanf("%u",&data); // input 24
    printf("%u\n", data);

    //Use of %i
    scanf("%i",&data); // input 78
    printf("%i\n", data);

    return 0;
}

Output: 19 , 24 ,78

 

3. Format specifiers (double) : %lf

#include <stdio.h>

int main()
{
    double data = 0.0;

    scanf("%lf", &data); // input is 27.06

    printf("%lf\n",data);

    return 0;
}

Output: 27.060000

 

4. Format specifier (Hexadecimal number): %x, %X

 

#include <stdio.h>

int main()
{
    int data;

    scanf("%x", &data); //take input in hex 10

    printf("%d\n", data); //print in decimal 

    return 0;
}

Output: 16 ( in decimal)

 

5. Format specifier (character array or string): %s

#include <stdio.h>

#define ARRAY_SIZE 10

int main()
{
    char buf[ARRAY_SIZE];

    printf("Enter a string: ");

    fgets(buf,ARRAY_SIZE,stdin);

    printf("string is: %s\n",buf);

    return 0;
}

Output:

fgets in c avoid risk

 

Recommended Posts for you: