Format specifiers in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Format specifiers in C , initiated with %, denote data types for input/output in functions like scanf and printf. They specify types like integer, string, or float. Different types have unique specifiers. Crucial for compiler understanding, they aid in accurate data handling during program execution.

List of Format Specifiers in C

Below is the list of all Format Specifiers in C.

Format SpecifierDescription
%d or %iSigned Integer
%uUnsigned Integer
%fFloating Point
%sString
%cCharacter
%pAddress Printing Format Specifier
%ldLong Integer
%lldLong Long Integer
%lfDouble floating Format Specifier
%oOctal representation
%xHexadecimal representation
%%Prints % character

Examples of Format Specifiers in C

1. Integer Format Specifier (signed) – %d in C

The %d format specifier is employed in functions like scanf() and printf() for processing signed integer data type, ensuring proper input and output handling within formatted strings.

Syntax

Example

Output:

2. Unsigned Integer Format Specifier – %u in C

The %u format specifier in C is designated for unsigned integer data type. Providing a negative integer to %u results in its conversion to its one's complement representation.

Syntax

Example

Output:

3. Floating-point format specifier – %f in C

In C, %f serves as the format specifier for floating-point data type in formatted strings, facilitating input and output operations. Additionally, %e or %E specifiers can be employed to present floating-point values in exponential notation.

Syntax

Example

Output:

4. String Format Specifier – %s in C

In C, %s is utilized for handling strings, enabling both input and output operations.

Syntax

Example

5. Character Format Specifier – %c in C

In C, %c serves as the format specifier for character data type, facilitating both input and output operations within formatted strings.

Syntax

Example

Output:

6. Address Format Specifier – %p in C

In C, %p serves as the format specifier to display addresses and pointers.

Syntax

Example

Output:

7. Unsigned Octal number for integer – %o in C

In C programming, %o serves as the format specifier for handling unsigned octal integer numbers, enabling both output and input operations.

Syntax

Example

Output:

8. Unsigned Hexadecimal for integer – %x in C

In C, the %x format specifier handles hexadecimal integers within formatted strings, representing alphabets in lowercase. Alternatively, %X is used for uppercase alphabet digits.

Syntax

Example

Output:

Input and Output Formatting

C provides tools for input and output formatting. They are placed between the % sign and the format specifier symbol:

  • A minus (-) sign indicates left alignment.
  • In C, when you use a number after %, it sets the minimum space for output. If there are fewer characters, spaces fill the gap; if more, they're printed as is.
  • A period (.) symbol separates field width with precision.

Precision in C format specifiers determines the minimum digits for integers, maximum characters for strings, and digits after the decimal point for floating-point values.

I/O Format Example

Code:

Example:

Conclusion

  • Format specifiers in C specify the data type being used, such as integer, floating-point, character, string, or address/pointer.
  • They are utilized in functions like scanf() and printf() for input and output operations, ensuring correct interpretation and display of data.
  • Format specifiers in C control the appearance of output by enabling alignment, defining field widths, and specifying precision for floating-point values.
  • Format specifiers in C offer versatility by accommodating various data types and allowing customization of output appearance, contributing to clear and organized program output.