Format Specifiers In C

Format Specifiers In C

In the programming world, we often need to be very precise about how data looks and behaves. This is where format specifiers in C come in handy. They are like codes that tell the computer how to show data when we use functions like printf and scanf. These format specifiers help us display different types of data, like numbers or characters, in the right way. Understanding format specifiers in C is essential for anyone who wants to use the language effectively. In this article, we’ll explore what format specifiers are, what they do, and how to use them to make our programs work just the way we want.

List of Format Specifiers in C

Format SpecifierDescription
%cFor b type.
%dFor signed integer type.
%e or %EFor scientific notation of floats.
%fFor float type.
%g or %GFor float type with the current precision.
%iUnsigned integer
%ld or %liLong
%lfDouble
%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

Examples of Format Specifiers in C

1. Character Format Specifier – %c in C

In C language, %c is the code used to specify the format for the char data type. It’s versatile because you can use it to both input and output char data in a specific format when working with C programs.

Syntax

scanf("%d...", ...);
printf("%d...", ...);

Example

// C Program to illustrate the %c format specifier.
#include <stdio.h>
 
int main()
{
 
    char c;
    // using %c for character input
    scanf("Enter some character: %c", &c);
 
    // using %c for character output
    printf("The entered character: %c", &c);
    return 0;
}

Input

Enter some character: A

Output

The entered character: A

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

You can use the signed integer format specifier %d in functions like scanf() and printf(), as well as in other functions that rely on formatted strings for input and output when working with integer (int) data types.

Syntax

scanf("%d...", ...);
printf("%i...", ...);

Example

// C Program to demonstrate the use of %d and %i
#include <stdio.h>
 
// Driver code
int main()
{
    int x;
    // taking integer input
    scanf("Enter the two integers: %d", &x);
 
    // printing integer output
    printf("Printed using %%d: %d\n", x);
    printf("Printed using %%i: %3i\n", x);
    return 0;
}

Input

Enter the integer: 45

Output

Printed using %d: 45
Printed using %i:    45

3. Unsigned Integer Format Specifier –  %u in C

The %u format specifier is used for unsigned integer data types in C. When you use %u with a negative integer value, it doesn’t treat it as a negative number but instead converts it to its two’s complement representation. This means it interprets the value as if it were unsigned.

Syntax

printf("%u...", ...);
scanf("%u...", ...);

Example: The following C Program demonstrates how to use %u in C.

// C Program to illustrate the how to use %u
#include <stdio.h>
 
// driver code
int main()
{
    unsigned int var;
 
    scanf("Enter an integer: %u", &var);
 
    printf("Entered Unsigned Integer: %u", var);
 
    // trying to print negative value using %u
    printf("Printing -10 using %%u: %u\n", -10);
    return 0;
}

Input

Enter an integer: 25

Output

Entered unsigned integer: 25
Printing -10 using %u: 4294967286

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

In the C language, %f is the format specifier used for floating-point data types. You can use it within formatted strings for both input and output when working with float data. Besides %f, you also have the option to use %e or %E as format specifiers to display floating-point values in exponential notation. These format specifiers offer different ways to represent floating-point numbers in your C programs.

Syntax

printf("%f...", ...);
scanf("%e...", ...);
printf("%E...", ...);

Example

// C program to demonstrate the use of %f, %e  and %E
#include <stdio.h>
 
// driver code
int main()
{
    float a = 12.67;
    printf("Using %%f: %f\n", a);
    printf("Using %%e: %e\n", a);
    printf("Using %%E, %E", a);
    return 0;
}

Output

Using %f: 12.670000
Using %e: 1.267000e+01
Using %E, 1.267000E+01

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

In a C program, you can use the %o format specifier to print or read input for unsigned octal integer numbers. This format specifier is particularly useful when you want to work with numbers in octal (base-8) representation. It allows you to display and handle octal integers effectively in your C code.

Syntax

printf("%o...", ...);
scanf("%o...", ...);

Example

#include <stdio.h>
int main()
{
    int a = 67;
    printf("%o\n", a);
    return 0;
}

Output

103

6. Unsigned Hexadecimal for integer – %x in C

The %x format specifier is employed in formatted strings for displaying hexadecimal integers. When you use %x, the letters in the hexadecimal representation will be in lowercase. If you want the hexadecimal letters to be in uppercase, you can use %X instead. These format specifiers provide control over the case of letters in hexadecimal numbers when working with C programs.

printf("%x...", ...);
scanf("%X...", ...);

Example

// C Program to demonstrate the use of %x and %X
#include <stdio.h>
int main()
{
    int a = 15454;
    printf("%x\n", a);
    printf("%X", a);
    return 0;
}

Output

3c5e
3C5E

7. String Format Specifier – %s in C

In C, the %s format specifier is used for working with strings. You can use it to print strings or read strings as input. It’s a versatile format specifier for handling character arrays, which represent strings in C, allowing you to display and manipulate textual data in your programs.

Syntax

printf("%s...", ...);
scanf("%s...", ...);

Example

#include <stdio.h>

int main() {
    char name[50]; // Declare a character array to store the string
    
    // Input a string
    printf("Enter your name: ");
    scanf("%s", name); // %s format specifier is used to input a string
    
    // Display the entered string
    printf("Hello, %s!\n", name); // %s format specifier is used to print the string
    
    return 0;
}

Output

Enter your name

8. Address Format Specifier – %p in C

in the C language, you can use the %p format specifier to print addresses and pointers. This format specifier is particularly useful when you need to display memory addresses or the values stored in pointers in your C programs.

Syntax

printf("%p...", ...);

Example

#include <stdio.h>
int main()
{
    int a = 10;
    printf("The Memory Address of a: %p\n",(void*)&a);
    return 0;
}

Output

The Memory Address of a: 0x7ffe9645b3fc

Input and Output Formatting

In C language, you have the tools to format input and output effectively. These formatting options are typically inserted between the % sign and the format specifier symbol in printf and scanf functions. Here are some of these formatting tools:

  1. Minus (-) Sign for Left Alignment: When you include a minus (-) sign, it signifies left alignment. This means that the data will be printed or read with the left edge as the reference point, and any extra space will be added on the right side if needed.
  2. Number for Minimum Field Width: You can specify a number after the % sign, which sets the minimum field width for the printed or read data. If the characters in the data are fewer than the specified width, spaces will be added to meet the minimum width. Conversely, if the data is larger, it will be printed or read as is without truncation.
  3. Period (.) for Precision: The period (.) symbol is used to separate the field width from precision. This is often used with format specifiers for floating-point numbers (e.g., %f). Precision allows you to specify the number of decimal places to display for floating-point values.

These formatting options in C provide control over how data is presented and handled in input and output operations, making it easier to create well-formatted and readable programs.

Example Of I/O Formatting

// C Program to demonstrate the formatting methods.
#include <stdio.h>
int main()
{
    char str[] = "skill vertex";
    printf("%20s\n", str);
    printf("%-20s\n", str);
    printf("%20.5s\n", str);
    printf("%-20.5s\n", str);
    return 0;
}

Output

Skill vertex
Skill vertex    
               skill vertex 
Skill vertex          

FAQ- Format Specifiers In C

Q1. What are format specifiers in C?

Ans. They are indeed used to specify the type of data that should be input or output in a program. The % symbol, followed by a character that represents the data type (e.g., %d for integers, %f for floating-point numbers), is used to define these format specifiers. They play a crucial role in informing the compiler about how to handle and display different types of data in C programs.

Q2. How many types of format specifiers are there in C?

Ans. There are basically six types of format specifiers in C

Q3. What is %F and %d in C?

Ans. %d: This format specifier is used for integers. It is used when you want to print or read integer values, which can be positive or negative whole numbers (e.g., -3, -100, 3, 100).
%f: This format specifier is used for floating-point numbers. It is used for numbers with decimal points (e.g., 10.6, -39.0).

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment