Format Specifiers in C programming

Spread the love

Format-specifiers-in-c-programming

Introduction:

In this article, We are going to discuss the format specifiers in C programming language. and we will also learn when to use which format specifier and how to use the format specifiers.

Format Specifiers in C:

Format Specifiers in the C language are used for formatting input and output values. Which helps us to specify the datatype of each value.

As C-Language is the Strictly typed language. We need to specify the datatype of each variable.

Similarly, We need to specify the datatype while taking the input from the user, and also we need to specify the datatype while printing the value of any variable. We need to specify variables type explicitly. The most common use of format specifiers are in printf() and scanf() functions. Format specifiers are one way to let the compiler know about the data we are using.

C Programming language support few format specifiers we are going to discuss them

📢 We also discussed all available data types and The size and ranges of all datatypes in following article, Please check for more info.

Size and Limits of Datatypes in C Language – SillyCodes

Different type of Format Specifiers in C Programming:

Here is the list of format specifiers in C programming Language

Datatype NameFormat Specifier
Character ( Singed and Unsigned ) %c
Short Integer (Signed) %hd or %hi
Unsigned Short Integer %hu
Integer (Signed) %d or %i
Unsigned Integer %u
Long Integer (Signed) %ld
Unsigned Long Integer %lu
Long Long Integer (Signed) %lld
Unsigned Long Long Integer %llu
Float %f
Double %lf
Long Double %Lf
Pointer (Memory Address) %p
Scientific Notation (Floating point data) %e
Octal (Base 8 representation) %o
String %s
Hexadecimal Representation %x or %X
% Character %%
Nothing (Empty) %n

Here are few examples to showcase the usage of format specifiers.

The Character (%c) Format Specifier:

Character format specifier %c is used to represent the character and unsigned character datatype.

Program:

Output:

The Integer (%d or %i ) and Unsigned Integer (%u) Format Specifier:

We can use %dand %i format specifiers for the Signed Integer datatype. We use %ufor the unsigned Integer datatype.

Here We used both datatypes of integer datatype. Also for the Unsigned integer datatype, we used the value as -1, So that we will get the Maximum allowable Integer value ( INT_MAX).

Output:

The Float (%f) format Specifier:

The %f format specifier is used to represent the float datatype values.

Program:

Output:

📢 Float datatype takes the precision upto 6 digits.

The Address or Pointer (%p) Format Specifier:

We use %p format specifier for representing the Address or Pointer type values.

Let’s use above float datatype example and convert it to get Address of variable and display it using the %p format specifier.

Program:

Output:

The Octal (%o), Hexa-decimal(%x and %X) Format Specifiers:

The %oformat specifier is used to represent the data in Octal or Base-8 format.

The %xor %X format specifier is used to represent the data in Hexadecimal or base-16 format.

Here is an example program.

Program:

Output:

Similarly, We can have all other format specifiers as well. Please play around with the format specifier to make yourself comfortable.

Conclusion:

In this article, We discussed what are format specifiers and the List of all format specifiers in the C language. We also looked into few examples of format specifiers.

Related Articles:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. June 4, 2021

    […] Format Specifiers […]

  2. July 5, 2021

    […] 📢 Learn More about Format specifier here – Format Specifiers in C programming – SillyCodes […]

Leave a Reply