- What Is Format Specifier In C?
- List Of Format Specifiers In C
- Types Of Format Specifiers In C
- What Is Double Format Specifier In C?
- Float Data Type Vs. Double Data Type In C
- Why Should I Use Format Specifiers In C?
- Conclusion
- Frequently Asked Questions
Format Specifiers In C | A Complete Guide With Detailed Examples

One of the most powerful and versatile programming languages, C is known for its low-level capabilities and efficiency. To work effectively in the C, developers must understand various features and concepts. This includes format specifiers in C programming that are essential for handling input and output operations. These specifiers help define the data type of values to be read or written.
In this article, we will explore the concept of format specifiers in C programming, explaining what they are and how to use them with practical examples.
What Is Format Specifier In C?
Format specifiers, also known as format codes or format strings, are placeholders used in input and output functions to represent data types. They instruct the compiler on how to interpret and display data when using functions like printf() and scanf().
- In other words, format specifiers help ensure that the program correctly reads and displays data based on the specified data type.
- In C, format specifiers are special characters that begin with the modulus/percent symbol (%), followed by a character indicating the data type.
- For example, the format specifier symbol %d represents a decimal integer/ integer data type, %f represents a floating-point number, and %c represents a character.
- Each format specifier corresponds to a specific data type, ensuring that the program handles the data correctly.
General Syntax of Format Specifiers:
A format specifier follows this general syntax:
%[flags][width][.precision][length]specifier
- %: Indicates the start of the format specifier.
- flags (optional): Modify the output format (e.g., - for left alignment, + to force a plus sign for positive numbers).
- width (optional): Specifies the minimum number of characters to be printed.
- .precision (optional): Specifies the number of digits after the decimal point for floating-point numbers.
- length (optional): Modifies the length of the data type (e.g., l for long, h for short).
- specifier: A character that specifies the type of data (e.g., d for integers, f for floating-point numbers, c for characters).
List Of Format Specifiers In C
It is important to correctly use format specifiers in C programming language. Incorrect usage can lead to errors and unexpected results.
- To prevent this, it is recommended to use appropriate format specifiers according to the type of data that needs to be displayed or read.
- If a specific format specifier is not available for the data type being used, it may be necessary to write custom code or functions properly.
The table below lists some of the most commonly used format specifiers in C, along with a description, range, and size.
Notation/Symbol |
Data Type |
Description |
Example Usage |
Size & Range |
%d or %i |
int |
Prints signed decimal integers (base 10). %i can interpret input in different bases with scanf(). |
printf("%d", 42); |
4 bytes; -2,147,483,648 to 2,147,483,647 |
%u |
unsigned int |
Prints unsigned decimal integers. |
printf("%u", 42); |
4 bytes; 0 to 4,294,967,295 |
%ld |
long int |
Prints signed long integers. |
printf("%ld", 123456L); |
4 bytes; -2,147,483,648 to 2,147,483,647 |
%lu |
unsigned long int |
Prints unsigned long integers. |
printf("%lu", 123456UL); |
4 bytes; 0 to 4,294,967,295 |
%lld |
long long int |
Prints signed long long integers. |
printf("%lld", 123456789LL); |
8 bytes; -(2^63) to (2^63)-1 |
%llu |
unsigned long long int |
Prints unsigned long long integers. |
printf("%llu", 123456789ULL); |
8 bytes; 0 to 18,446,744,073,709,551,615 |
%f |
float |
Prints decimal floating-point numbers with six digits of precision. |
printf("%f", 3.14); |
4 bytes; 1.2E-38 to 3.4E+38 |
%lf |
double |
Prints double-precision floating-point numbers. |
printf("%lf", 3.1415); |
8 bytes; 1.7E-308 to 1.7E+308 |
%Lf |
long double |
Prints extended-precision floating-point numbers. |
printf("%Lf", 3.141592L); |
10 or 16 bytes; 3.4E-4932 to 1.1E+4932 |
%e or %E |
float or double |
Prints numbers in scientific notation using 'e' or 'E'. |
printf("%e", 1234.56); |
4 or 8 bytes; depends on type |
%g or %G |
float or double |
Prints numbers in %f or %e format, whichever is shorter, with no trailing zeros. |
printf("%g", 1234.56); |
4 or 8 bytes; depends on type |
%x |
unsigned int |
Prints unsigned hexadecimal integers (base 16)(lowercase letters). |
printf("%x", 255); |
4 bytes; 0 to 4,294,967,295 |
%X |
unsigned int |
Prints unsigned hexadecimal integers (base 16) (uppercase letters). |
printf("%X", 255); |
4 bytes; 0 to 4,294,967,295 |
%o |
unsigned int |
Prints unsigned octal integers. |
printf("%o", 255); |
4 bytes; 0 to 4,294,967,295 |
%c |
char |
Prints a single character. |
printf("%c", 'A'); |
1 byte; -128 to 127 (signed) or 0 to 255 (unsigned) |
%s |
char[] |
Prints a string of characters. |
printf("%s", "Hello"); |
Depends on string length |
%p |
void* |
Prints a pointer address. |
printf("%p", ptr); |
4 or 8 bytes; depends on system architecture |
%% |
N/A |
Prints a percent sign. |
printf("%%"); |
N/A |
Note: The sizes listed here are for a typical 32-bit or 64-bit system and may vary depending on the system and compiler used to run the C program. Also, note that the size of a pointer depends on the system architecture. On a 32-bit system, pointers are typically 4 bytes, while on a 64-bit system, pointers are typically 8 bytes.
Types Of Format Specifiers In C
Integer Format Specifier In C (%i or %d)
The %i and %d format specifiers are used to represent integer values when the data type is a signed integer. There are many kinds of integer format specifiers, of which %d is used to format an integer as a signed decimal.
This means that it represents a value that is decimal, regardless of whether it is positive or negative. Overall, both %i and %d serve the same purpose in the context of the printf() function.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKaW50IG51bSA9IDEwOwpwcmludGYoIlRoZSB2YWx1ZSBvZiBudW0gaXMgJWRcbiIsIG51bSk7CgpyZXR1cm4gMDsKfQ==
Output:
The value of num is 10
Explanation:
We begin the simple C program example by including the header file <stdio.h> for standard input-output operations.
- We then initiate the main() function, which is the program's entry point.
- Inside the main, we declare an integer type variable num and initialize it with the value 10.
- Next, we use the library function printf() to display the value with a message on the console. The message includes the format specifier %d, which indicates where the integer value will be inserted.
- The formatted string also contains the newline escape sequence (\n), which shifts the cursor to the next line.
- The num variable is provided as an argument to the printf() function, which replaces the %d format specifier with the value of num.
- Finally, the return 0 statement signifies a successful execution of the program, and the program terminates.
Floating Format Specifier In C ( %f )
This format specifier is used to print floating-point numbers, that is, numbers with a fractional part like 2.345343 or 3.14159, etc. The format specifier symbol %f, when used inside the formatted string for input and output, instructs the function to replace it with a floating-point value. Let's look at an example of this.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKZmxvYXQgbnVtID0gMy4xNDE1OTsKcHJpbnRmKCJUaGUgdmFsdWUgb2YgdGhlIG51bSBpcyAlZlxuIiwgbnVtKTsKCnJldHVybiAwOwp9
Output:
The value of the num is 3.141590
Explanation:
In the C program example above-
- We once again include the stdio.h header file and start the main() function.
- Then, we declare a variable, called num, of floating-point data type and initialize it with the value 3.14159. This value represents the mathematical constant π (pi).
- Next, we use the printf() function to display a message on the console, where the message includes the format specifier %f, indicating that a floating-point number will be inserted in its place.
- The variable num is passed as an argument to the printf function. The %f format specifier is replaced with the value of num.
Character Format Specifier In C ( %c )
The %c format specifier is employed to print a single character in C. It interprets the corresponding argument as a character value, displaying it as such. This specifier is commonly used when outputting individual characters, such as letters, digits, or symbols. It's important to note that characters in C are internally represented by their ASCII values, so %c ensures the correct character representation is displayed.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKY2hhciBsZXR0ZXIgPSAnQSc7CnByaW50ZigiVGhlIHZhbHVlIG9mIHRoZSBsZXR0ZXIgaXMgJWNcbiIsIGxldHRlcik7CgpyZXR1cm4gMDsKfQ==
Output:
The value of the letter is A
Explanation:
In the example C program-
- We declare a character variable called letter and initialize it with the value 'A', inside the main() function.
- Then, we use the printf() function to display the value of the letter using the %c format specifier, which formats the character value as a single character.
Strings Format Specifier In C ( %s )
The %s format specifier is used to print a sequence of characters, or a string, in C. When used with functions like printf(), it outputs the characters stored in a character array until it encounters a null terminator (\0). This specifier is fundamental for displaying text and is widely used in various applications, from simple messages to complex data processing.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKY2hhciBzdHJbXSA9ICJIZWxsbywgd29ybGQhIjsKcHJpbnRmKCJUaGUgdmFsdWUgb2Ygc3RyIGlzICVzXG4iLCBzdHIpOwoKcmV0dXJuIDA7Cn0=
Output:
The value of str is Hello, world!
Explanation:
In the C code example-
- Inside the main() function, we declare a character array str and initialize it with the string Hello, world!
- We then use the printf() function to print a message with the value of the string.
- The message is enclosed in double quotes and contains a format specifier %s, indicating that a string (character array) will be inserted in its place.
- The str variable is passed as an argument to the printf function. The %s format specifier is replaced with the value of str.
- Finally, the return 0 statement signifies that the program has been executed successfully, and it exits with a return code of 0.
Pointers Format Specifier In C ( %p )
The %p format specifier prints the memory address of a pointer variable, which stores the memory address of another variable it is pointing to. This is particularly useful for debugging purposes, allowing developers to verify the addresses stored in pointer variables. The output is typically in hexadecimal format, reflecting the actual memory address the pointer references.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKaW50IG51bSA9IDEwOwppbnQgKnB0ciA9ICZudW07IC8vQXNzaWduaW5nIGFkZHJlc3Mgb2YgbnVtIHZhcmlhYmxlIHRvIHBvaW50ZXIgcHRyCnByaW50ZigiVGhlIHZhbHVlIG9mIHB0ciBpcyAlcFxuIiwgcHRyKTsKCnJldHVybiAwOwp9
Output:
The value of ptr is 0x7ffcc90b2914
Explanation:
In the example C code-
- Inside the main() function, we declare an integer variable num and initialize it with the value 10.
- As mentioned in the code comment, we declare a pointer variable, ptr, i.e., a pointer to an integer (int *), and assign the memory address of num to it using the address-of operator (&).
- Then, we use the printf() function to display a message and the address of the num on the console.
- The message includes a format specifier %p, indicating that a pointer's address will be inserted in its place.
- The ptr variable, which holds the memory address of num, is passed as an argument to the printf function. The %p format specifier is replaced with the memory address held by ptr.
Long Integers (%ld) Format Specifier In C
The %ld format specifier is used to print signed long integers in C. A long int typically occupies more memory than a standard int, allowing it to represent a broader range of integer values. This specifier is particularly useful when dealing with large numerical values that exceed the limits of standard integers, such as file sizes, timestamps, or counters in extensive loops. On many systems, a long int provides a range from –2,147,483,648 to 2,147,483,647.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKbG9uZyBudW0gPSAxMjM0NTY3ODkwOwpwcmludGYoIlRoZSB2YWx1ZSBvZiBudW0gaXMgJWxkXG4iLCBudW0pOwoKcmV0dXJuIDA7Cn0=
Output:
The value of num is 1234567890
Explanation:
In the sample C program-
- We declare a long integer variable x and initialize it with the value 123456789 inside the main() function.
- Then, we print the value of x to the console using the printf() function and the %ld format specifier, which formats the long integer value as a decimal integer.
- The num variable is passed as an argument to printf, and the %ld format specifier is replaced with the value of num.
Unsigned Integers Format Specifier In C (%u)
The %u format specifier is used in C to print unsigned decimal integers. Unlike signed integers, unsigned integers can only represent non-negative values, effectively doubling the upper limit of positive values they can store. On a 32-bit system, an unsigned int ranges from 0 to 4,294,967,295.
When using %u with functions like printf(), it's essential to ensure that the corresponding variable is of an unsigned type. If a negative value is inadvertently printed using %u, it will be interpreted as a large positive number due to the way negative numbers are represented in memory (two's complement representation). For example, printing -1 with %u may output 4294967295.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKdW5zaWduZWQgaW50IHggPSA0MjsKcHJpbnRmKCJUaGUgdmFsdWUgb2YgeCBpcyAldVxuIiwgeCk7CgpyZXR1cm4gMDsKfQ==
Output:
The value of x is 42
Explanation:
In the C program sample-
- Inside the main() function, we first declare an unsigned integer variable x and then initialize it with the value 42.
- We then use the printf() function to display the value of x variable using the format specifier %u, indicating where the unsigned integer value will be inserted.
- The value of x (42) is passed as an argument to printf, and the %u format specifier is replaced with the unsigned integer value.
Unsigned Long Integer (%lu) Format Specifier In C
The %lu format specifier is used to print unsigned long integers in C. An unsigned long integer is a non-negative integer that typically occupies more memory than a standard unsigned int, allowing it to represent a larger range of values. This format specifier is particularly useful when dealing with large numerical values that should not be negative, such as memory addresses or file sizes. For instance, on a 32-bit system, an unsigned long can represent values from 0 to 4,294,967,295.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKdW5zaWduZWQgbG9uZyBudW0gPSAxMjM0NTY3ODkwOwpwcmludGYoIlRoZSB2YWx1ZSBvZiBudW0gaXMgJWx1XG4iLCBudW0pOwoKcmV0dXJuIDA7Cn0=
Output:
The value of num is 1234567890
Explanation:
In the sample C code-
- We declare an unsigned long integer variable num inside the main() function and initialize it with the value 1234567890.
- We then print this value with a message to the console using the printf() function.
- In the message, we use the %lu format specifier to indicate that an unsigned long integer value will be inserted in its place.
- The num variable is passed as an argument to the printf function. The %lu format specifier is replaced with the value of num.
- When the program is executed, the output will display 'The value of num is 1234567890', showing the value of num as an unsigned long integer.
Long Long Integers Format Specifier In C ( %lld )
The %lld format specifier is designed for printing signed long long integers in C. This data type is capable of storing very large positive and negative integers, typically ranging from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 on a 64-bit system. It's commonly used in applications requiring high-precision arithmetic or when dealing with large datasets where standard int types are insufficient.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKbG9uZyBsb25nIG51bSA9IDEyMzQ1Njc4OTAxMjM0NTsKcHJpbnRmKCJUaGUgdmFsdWUgb2YgbnVtIGlzICVsbGRcbiIsIG51bSk7CgpyZXR1cm4gMDsKfQ==
Output:
The value of num is 123456789012345
Explanation:
In the C code sample-
- In the main() function, we declare and initialize a long long integer variable num to the value 123456789012345.
- Then, we use the printf() function to display a message on the console, with the format specifier %lld enclosed in the double quotes, indicating that a long long integer will be inserted in its place.
- The value of num is passed as an argument to printf, and the %lld format specifier is replaced with the value of num.
- The return 0 statement marks a successful program execution.
Unsigned Long Long Integers ( %llu )
Use the %llu format specifier to print unsigned long long integers. The only difference is that the %llu specifier is used for unsigned integer values that are larger than unsigned long integers.
This type is ideal for representing very large non-negative numbers, with a typical range up to 18,446,744,073,709,551,615 on 64-bit systems. It's particularly useful in scenarios like file system operations, cryptographic computations, or any context where large positive integers are required.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKdW5zaWduZWQgbG9uZyBsb25nIG51bSA9IDEyMzQ1Njc4OTAxMjM0NTsKcHJpbnRmKCJUaGUgdmFsdWUgb2YgbnVtIGlzICVsbHVcbiIsIG51bSk7CgpyZXR1cm4gMDsKfQ==
Output:
The value of num is 123456789012345
Explanation:
- Inside the main() function, we declare and initialize an unsigned long long integer variable called num with the value 123456789012345.
- Then, using the printf() function, we display a message on the console, with the format specifier %llu, to indicate that an unsigned long long integer value will be inserted in its place.
- The value of num is passed as an argument to printf, and the %llu format specifier is replaced with the value of num.
Hexadecimal Integers Format Specifier In C (%x)
The %x and %X format specifiers are used to print integers in hexadecimal (base-16) format. While %x outputs letters in lowercase (a–f), %X uses uppercase (A–F). Hexadecimal representation is prevalent in programming for tasks like memory address manipulation, color codes in graphics, and debugging, as it offers a more human-readable form of binary-coded values.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKaW50IG51bSA9IDI1NTsKcHJpbnRmKCJUaGUgdmFsdWUgb2YgbnVtIGlzICV4XG4iLCBudW0pOwoKcmV0dXJuIDA7Cn0=
Output:
The value of num is ff
Explanation:
- Inside the main() function, we declare an integer variable num and initialize it with the value 255.
- We then use the printf() function to display value of num in hexadecimal format using the %x format specifier, indicating that an integer value will be inserted in its hexadecimal representation.
- The value of num (which is 255 in decimal) is passed to printf(), and the %x format specifier is replaced with the hexadecimal value ff.
Note- If the notation were to be %2x, then it would indicate to the compiler that the value should be printed in hexadecimal form, with at least two characters (or padded with at least two characters). Also, if %X were used instead, the output would be FF instead of ff.
Scientific Notation Format Specifiers ( %e or %E )
The %e and %E format specifiers are used to print floating-point numbers in scientific notation (exponential notation), where the format is mantissa * 10^exponent. The character 'e' represents the exponential power, which here, by default, is considered to be 10. It is a placeholder for the corresponding argument that represents a floating-point value.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKZG91YmxlIHggPSAxMjM0LjU2Nzg5OwpwcmludGYoIlRoZSB2YWx1ZSBvZiB4IGluIHNjaWVudGlmaWMgbm90YXRpb24gaXMgJWVcbiIsIHgpOwoKcmV0dXJuIDA7Cn0=
Output:
The value of x in scientific notation is 1.234568e+03
Explanation:
- We declare a double-precision floating-point variable x in the main() function, and initialize it with the value 1234.56789.
- Then, we use the printf() function to output a message with the value for the variable x. The message contains the format specifier %e, indicating that a floating-point number will be inserted in scientific (exponential) notation.
- The value of x (1234.56789) is passed as an argument to printf, and the %e format specifier is replaced with the value in scientific notation, which might look like 1.234568e+03.
Uppercase Variant: If %E were used instead, the output would be in the form 1.234568E+03.
Octal Integers Format Specifier In C (%o)
The %o format specifier is used to display an integer in octal (base-8) format. Each digit in octal represents three binary digits, making it a compact way to represent binary data. This specifier is often utilized in systems programming, such as setting file permissions in Unix-based systems, where octal notation is standard.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKaW50IHggPSA0MjsKcHJpbnRmKCJUaGUgdmFsdWUgb2YgeCBpbiBvY3RhbCBpcyAlb1xuIiwgeCk7CgpyZXR1cm4gMDsKfQ==
Output:
The value of x in octal is 52
Explanation:
- We declare an integer variable x and initialize it with the value 42.
- Then, we use the printf() function to display the value of x in an octal format using the %o format specifier.
- The decimal value 42 is represented as 52 in octal, which is printed in the output.
Short Integers Format Specifiers ( %hd )
We know the %i or %d specifiers that print integer values. Then what is this for?
The %hd format specifier is used to print short integer values in C. The h indicates that the value is short, and the d specifies that it is a short integer.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKc2hvcnQgaW50IHggPSAzMjc2NzsKcHJpbnRmKCJUaGUgdmFsdWUgb2YgeCBpcyAlaGRcbiIsIHgpOwoKcmV0dXJuIDA7Cn0=
Output:
The value of x is 32767
Explanation:
- We begin the example above by including the standard input-output library and initiating the main() function.
- Inside the main, we declare a short integer variable x and initialize it by assigning a value 32767. Note that short int data types have a limited range compared to regular integers.
- Next, we use the printf() function to display a message on the console, with the format specifier %hd, indicating that a short integer value will be inserted.
- The value of x (32767) is passed as an argument to printf, and the %hd format specifier is replaced with the short integer value.
- Finally, the return 0 statement signifies a successful program execution.
Check out this amazing course to become the best version of the C programmer you can be.
Unsigned Short Integers Format Specifier ( %hu )
The %hu format specifier is utilized to print unsigned short integers in C. An unsigned short int is a data type that can store non-negative integer values, typically ranging from 0 to 65,535 on systems where it occupies 2 bytes. This specifier is particularly useful when dealing with data that should not be negative and requires less memory, such as port numbers, small counters, or specific hardware-related values.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKdW5zaWduZWQgc2hvcnQgaW50IHggPSA2NTUzNTsKcHJpbnRmKCJUaGUgdmFsdWUgb2YgeCBpcyAlaHVcbiIsIHgpOwoKcmV0dXJuIDA7Cn0=
Output:
The value of x is 65535
Explanation:
- In the main() function of the C program above, we declare an unsigned short integer variable x and initialize it with the value 65535.
- We then use the printf() function and pass the value of x to it as an argument.
- The function prints a message to the console containing the format specifier %hu, indicating that an unsigned short integer value will be inserted in that place.
- When the program is executed, the value 65535 is printed as-is, since it’s within the range of unsigned short integers.
Printing Error Message Format Specifier In C (%m)
The %m format specifier is used to print the error message corresponding to the value of errno in C. Note that this specifier is specific to the GNU C library and is not standard C, which can affect portability.
This is not a valid specifier in C, but since C89/90, using %m in a formatted string for functions acts as the placeholder for the result of an implied strerror(erno) call. So, the value inserted in its place is the text string/ error message corresponding to the current value of the errno variable (Linux std error code).
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxlcnJuby5oPgoKaW50IG1haW4oKSB7CkZJTEUgKmZwOwpmcCA9IGZvcGVuKCJub25leGlzdGVudGZpbGUudHh0IiwgInIiKTsKaWYgKGZwID09IE5VTEwpIHsKcHJpbnRmKCJFcnJvciBvcGVuaW5nIGZpbGU6ICVtXG4iKTsKcmV0dXJuIDE7fQpmY2xvc2UoZnApOwoKcmV0dXJuIDA7Cn0=
Output:
Error opening file: No such file or directory
Explanation:
- In the above code, we have tried to open a file that does not exist using the fopen() function.
- As the file does not exist, fopen will return NULL and the errno variable is automatically set to ENOENT, which indicates "No such file or directory".
- We have used the %m format specifier to print the error message corresponding to the value of errno.
- The program returns 1 to indicate an error condition, and the error message is displayed.
The %n Format Specifier In C
The %n format specifier doesn't print any output but instead stores the number of characters written so far by the printf() function into an integer variable provided as an argument. This can be useful for tracking output length or for advanced formatting needs. However, it should be used cautiously, as improper use can lead to security vulnerabilities.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKaW50IHgsIHk7CnByaW50ZigiVGhpcyBpcyBhIHNhbXBsZSBzdHJpbmclblxuIiwgJngpOwpwcmludGYoIlRoZSBudW1iZXIgb2YgY2hhcmFjdGVycyBwcmludGVkIGJlZm9yZSB0aGlzIGlzICVkXG4iLCB4KTsKcHJpbnRmKCJUaGlzIGlzIGFub3RoZXIgc2FtcGxlIHN0cmluZyVuXG4iLCAmeSk7CnByaW50ZigiVGhlIG51bWJlciBvZiBjaGFyYWN0ZXJzIHByaW50ZWQgYmVmb3JlIHRoaXMgaXMgJWRcbiIsIHkpOwoKcmV0dXJuIDA7Cn0=
Output :
This is a sample string
The number of characters printed before this is 23
This is another sample string
The number of characters printed before this is 29
Explanation:
- We first declare two integer variables x and y, inside the main() function using the comma operator.
- Next, we use the printf() function to print the string- This is a sample string, with the %n format specifier.
- As a result, the %n specifier format specifier counts the number of characters printed up until its occurrence and stores that count in the specified variable, here variable x, using the address-of operator.
- Next, we use printf() to display another phrase along with the value for x, where the %d is replaced with the value of x as counted by %n earlier.
- The process is repeated, where we print another phrase using printf(). The %n specifier counts the characters and this time stores the value in the variable y.
- Finally, we print the value of y, indicating the number of characters printed for the second string.
What Is Double Format Specifier In C?
In C, the double data type is used for storing high-precision floating-point numbers. It is named double because it typically holds twice the size of data compared to the float data type. A double in C ranges between 1.7E-308 - 1.7E+308, and it can store decimal numbers, real numbers, negative values, etc. Doubles provide greater precision and can represent a wider range of values, making them essential in applications requiring accurate and large-scale numerical computations.
Some key characteristics of the double data type in C are as follows:
- Size: Typically, a double type is stored in 8 bytes (64 bits) of memory, providing greater precision compared to the float data type, which is usually 4 bytes (32 bits).
- Precision: It offers double precision, representing values with a greater number of significant digits, typically about 15-17 significant digits. This precision makes them suitable for scientific calculations where accuracy is critical.
- Range: Due to its increased precision, the double type can represent a wider range of values, both very large and very small, compared to the float type.
- Suffix: In C, you can specify a double type constant by adding the double suffix d or D, though this is optional. For example, you can write a double constant like 3.14159265359 or 3.14159265359D.
- Usage: The double is commonly used in applications where precision is essential, such as scientific calculations, financial software, and engineering simulations. It is also the default floating-point type for many C functions and libraries.
Syntax For Declaration & Initialization
To declare and initialize a double variable in C, you must use the following syntax:
double variable_name = initial_value;
Here, the double keyword marks the type of the variable whose name is given by the identifier variable_name, and the value assigned is given by intial_value.
Representation Of Double In C
Double-precision floating-point numbers (doubles) are typically represented using 64 bits of memory according to the IEEE 754 standard. This standard defines the format for representing doubles, which includes a sign bit, an exponent, and a significant (mantissa) part.
- Sign bit (1 bit): Indicates whether the number is positive (0) or negative (1).
- Exponent (11 bits): Represents the power of 2 used to scale the mantissa.
- Mantissa (52 bits): Stores the significant digits of the number.
This representation allows doubles to store values with higher precision and a wider range than single-precision floats.
Here's an example of a double's binary representation (simplified for illustration):
0 | 10000000000 | 1100000000000000000000000000000000000000000000000000000
^ Exponent ^ Mantissa
Here, the sign bit (0) indicates that the number is positive, and the exponent (10000000000) represents the power of 2 used to scale the mantissa, which stores the significant digits of the number.
How To Print Double Value In C?
You can print the value of a double variable using the printf() function with the %f format specifier, which is used for formatting floating-point numbers. For example:
double num = 123.456;
printf("The value of num is: %f\n", num);
Example Of Double In C
Below is an example of using a double variable to calculate the area of a circle using the radius and then using the %f format specifier to print the value with double precision.
Code Example:
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKZG91YmxlIHJhZGl1cyA9IDUuMDsKZG91YmxlIGFyZWEgPSAzLjE0MTU5MjY1MzU5ICogcmFkaXVzICogcmFkaXVzOwoKcHJpbnRmKCJUaGUgYXJlYSBvZiB0aGUgY2lyY2xlIGlzOiAlZlxuIiwgYXJlYSk7CgpyZXR1cm4gMDsKfQ==
Output:
The area of the circle is: 78.539816
Explanation:
In the example-
- Inside the main() function, we declare two double variables, radius, and area, with the former initialised to the value of 5.
- We calculate the area variable representing the area of the circle by multiplying π (approximately 3.14159265359) by the square of the radius.
- Next, we use the printf() function to display the calculated area with the %f format specifier, which formats the double as a floating-point number.
- Finally, the return 0 statement signifies a successful program execution.
This example illustrates the power and flexibility of the double data type in handling precise and complex numerical computations in C.
Float Data Type Vs. Double Data Type In C
In C programming, both float and double are used to store floating-point numbers, but they differ in precision, range, memory usage, and computational efficiency. Here’s a comparison of these two data types:
Aspect | float | double |
---|---|---|
Data Type Size | Typically 4 bytes | Typically 8 bytes |
Precision | Single precision float number with 6-9 significant digits | Double-precision, 15-17 significant digits |
Range | Limited range compared to double | Larger range compared to float |
Memory Usage | Less memory is required | More memory is required |
Default Suffix | Default is f (e.g., 3.14f) | Default is either none or d (e.g., 3.14 or 3.14d) |
Use Case | Suitable for most common use cases | Used for high-precision calculations |
Computational Speed | Generally faster due to smaller size | Slightly slower due to the large size |
Portability | Provides faster calculations on some platforms | Provides greater precision on all platforms |
Example | float pi = 3.14159f; | double pi = 3.14159265359; |
Why Should I Use Format Specifiers In C?
Format specifiers in C are essential for handling input and output operations. They serve as placeholders in formatted strings, ensuring that the data is correctly interpreted and displayed. Here’s why they are important: Some key reasons why format specifiers are used in C are as follows-
- Data Type Specification: Format specifiers in C specify the expected data type during input and output operations, ensuring that the program interprets and handles the data correctly.
- Type Safety: They enhance type safety by enforcing consistency between the format string and the actual data. This reduces the risk of type-related errors, such as passing a float where an integer is expected.
- Data Formatting: Format specifiers in C allow for the precise formatting of data, enabling developers to control how numbers, dates, and other values are displayed. For instance, you can control the number of decimal places displayed in a floating-point number using %.2f.
- Internationalization and Localization: They facilitate internationalization and localization by adapting data presentation to different regions and languages, accounting for variations in numeric formats and character encoding.
- Code Clarity: Format specifiers in C make code more readable and maintainable by clearly indicating the expected data types and how they should be formatted.
- Error Prevention: Proper use of format specifiers in C helps prevent runtime errors, which can lead to program crashes or unexpected behavior. For example, format mismatches, invalid type casting/ conversions, or data-related issues.
- Security: Correct use of format specifiers contributes to program security by preventing vulnerabilities such as format string attacks, where an attacker could exploit a poorly formatted string to execute arbitrary code.
Looking for mentors? Find the perfect mentor for select experienced coding & software experts here.
Conclusion
Format specifiers are a fundamental concept in C programming as they play a crucial role in handling input and output operations. Understanding the use of format specifiers in C is essential for displaying and reading data accurately in your C programs. By using the correct format specifier for each data type, you can ensure that your programs work as expected and produce well-formatted output.
Also read: 100+ Top C Interview Questions With Answers (2025)
Frequently Asked Questions
Q. What are format specifiers in C?
Format specifiers in C are placeholders used in functions like printf() and scanf() to specify the format and type of data to be displayed or read. They allow programmers to control the formatting of input and output operations. For example, when we want to print an extra-long decimal integer value, we must use the format specifier %lld inside the function. This indicates that the value to be replaced is a long long integer.
long long num = 2342345435646532431232; \\declaring and initializing a long long variable
printf("The value of num is %lld\n", num);
Q. Why are format specifiers important in C programming?
As the name suggests, the format specifiers specify the type of value stored inside a variable. It is essential to use these specifiers in the program because-
- Format specifiers are essential for accurate and controlled data formatting.
- They ensure that data is displayed or read in the correct format and type, preventing errors and ensuring proper data handling.
- Format specifiers also enable localization and internationalization efforts, making software adaptable to different languages and regions.
- Properly used format specifiers contribute to program security.
- Format specifiers help prevent errors by enforcing consistency between the format string and the actual data.
Q. What are some common format specifiers in C?
In C, format specifiers are used to specify the data type when displaying or reading data using functions like printf() and scanf(). Here are some common format specifiers-
- %d: Represents a signed decimal integer.
- %u: Represents an unsigned decimal integer.
- %f: Represents a floating-point number in decimal notation.
- %lf: Represents a double-precision floating-point number.
- %c: Represents a character.
- %s: Represents a string (null-terminated array of characters).
- %p: Represents a pointer address.
- %x: Represents an integer in hexadecimal notation.
- %o: Represents an integer in octal notation.
- %ld: Represents a long integer.
- %lu: Represents an unsigned long integer.
- %lld: Represents a long long integer.
- %llu: Represents an unsigned long long integer.
Q. How do format specifiers in C ensure type-checking and error prevention?
Format specifiers in C play a crucial role in ensuring type-checking and error prevention during input and output operations.
- When using format specifiers in functions like scanf(), they specify the expected data type for the input.
- This ensures that the data provided by the user or a file is of the correct type, preventing unintended type-related errors, such as attempting to read a string into an integer variable.
- Likewise, when using format specifiers in printf(), they dictate how data should be displayed, helping to avoid formatting errors and mismatches between the format string and the actual data, which can lead to runtime issues.
In conclusion, format specifiers in C contribute to safer and more reliable programs/ code by enforcing type consistency and formatting.
Q. Can format specifiers be modified for localization and internationalization in C?
In C programming, format specifiers serve as placeholders for data types, instructing how to format and display values in functions like printf() and scanf(). While format specifiers themselves don't directly handle localization and internationalization, they play a pivotal role within the broader strategy for achieving these goals.
- For internationalization, C developers often employ internationalization libraries like GNU gettext or ICU, and they set the locale using setlocale to accommodate different cultural and linguistic preferences.
- Format specifiers come into play when formatting numeric, date, and time values, ensuring that these values are presented in a manner consistent with the user's chosen locale.
Thus, format specifiers in C, when used in conjunction with internationalization techniques, contribute to the development of software that can seamlessly adapt to diverse languages and regions, providing a more inclusive and user-friendly experience.
Q. What is %d in C?
In C programming, %d is a format specifier used with functions like printf() and scanf() to handle signed decimal integers. It instructs the compiler to interpret the corresponding argument as a signed integer in base 10. Example:
int num = -42;
printf("The value is %d", num);
Output:
The value is -42
This specifier is commonly used when dealing with integer values that can be both positive and negative.
Q. What is %i in C?
The %i format specifier in C is similar to %d and is used for signed integers. However, when used with scanf(), %i can interpret input numbers in different bases: decimal, octal, or hexadecimal, depending on the prefix of the input. Example:
int num;
scanf("%i", &num);
If the user inputs 0x10, num will be assigned the decimal value 16. This flexibility makes %i useful for reading numbers in various numeral systems.
Q. What is the difference between %d and %i in C?
While both %d and %i are used for signed integers, their behavior differs when used with scanf().
- %d: Interprets the input strictly as a decimal integer.
- %i: Interprets the input based on its prefix: no prefix for decimal, 0 for octal, and 0x or 0X for hexadecimal.
Example:
int num;
scanf("%i", &num);
If the user inputs 0x10, num will be assigned the decimal value 16. This distinction is crucial when reading inputs that may be in different numeral systems.
Q. What is %x in C?
In C, %x is a format specifier used with printf() to display an integer in hexadecimal (base 16) format using lowercase letters (a to f). Example:
int num = 255;
printf("Hexadecimal: %x", num);
Output:
Hexadecimal: ff
To display uppercase letters, use %X instead.
Q5. What is %lu in C?
The %lu format specifier is used in C to print an unsigned long int, which is a non-negative integer that typically has a larger range than a standard unsigned int. Example:
unsigned long int num = 4294967295;
printf("Value: %lu", num);
Output:
Value: 4294967295
This specifier is particularly useful when dealing with large positive integer values.
Q. What is %s in C?
In C, %s is a format specifier used with printf() to display a string, which is a sequence of characters terminated by a null character ('\0'). Example:
char str[] = "Hello, World!";
printf("%s", str);
Output:
Hello, World!
This specifier is essential for handling and displaying text in C programs.
Q. What is %c in C?
The %c format specifier in C is used with printf() to display a single character. It interprets the corresponding argument as a character value. Example:
char ch = 'A';
printf("Character: %c", ch);
Output:
Character: A
This compiles our discussion on format specifiers in C. You might also be interested in reading:
- Typedef In C | Syntax & Use With All Data Types (+ Code Examples)
- Logical Operators In C (AND, OR, NOT, XOR) With Code Examples
- Control Statements In C | The Beginner's Guide (With Examples)
- Recursion In C | Components, Working, Types & More (+Examples)
- Union In C | Declare, Initialize, Access Member & More (Examples)
An economics graduate with a passion for storytelling, I thrive on crafting content that blends creativity with technical insight. At Unstop, I create in-depth, SEO-driven content that simplifies complex tech topics and covers a wide array of subjects, all designed to inform, engage, and inspire our readers. My goal is to empower others to truly #BeUnstoppable through content that resonates. When I’m not writing, you’ll find me immersed in art, food, or lost in a good book—constantly drawing inspiration from the world around me.
Login to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.

Subscribe
to our newsletter
Comments
Add comment