Format Specifiers in C - GeeksforGeeks (2024)

The format specifier in C is used to tell the compiler about the type of data to be printed or scanned in input and output operations. They always start with a % symbol and are used in the formatted string in functions like printf(), scanf, sprintf(), etc.

The C language provides a number of format specifiers that are associated with the different data types such as %d for int, %c for char, etc. In this article, we will discuss some commonly used format specifiers and how to use them.

List of Format Specifiers in C

The below table contains the most commonly used format specifiers in C

Format Specifier

Description

%c

For character type.

%d

For signed integer type.

%e or %E

For scientific notation of floats.

%f

For float type.

%g or %G

For float type with the current precision.

%i

Unsigned integer

%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

Examples of Format Specifiers in C

1. Character Format Specifier – %c in C

The %c is the format specifier for the char data type in C language. It can be used for both formatted input and formatted output in C language.

Syntax:

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

Example:

C

// 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

We can use the signed integer format specifier %d in the scanf() and print() functions or other functions that use formatted string for input and output of int data type.

Syntax:

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

Example:

C

// 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 is the format specifier for the unsigned integer data type. If we specify a negative integer value to the %u, it converts the integer to its first complement.

Syntax:

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

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

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

The %f is the floating point format specifier in C language that can be used inside the formatted string for input and output of float data type. Apart from %f, we can use %e or %E format specifiers to print the floating point value in the exponential form.

Syntax:

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

Example:

C

// 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.670000Using %e: 1.267000e+01Using %E, 1.267000E+01

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

We can use the %o format specifier in the C program to print or take input for the unsigned octal integer number.

Syntax:

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

Example:

C

#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 used in the formatted string for hexadecimal integers. In this case, the alphabets in the hexadecimal numbers will be in lowercase. For uppercase alphabet digits, we use %X instead.

Syntax:

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

Example:

C

// 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

3c5e3C5E

7. String Format Specifier – %s in C

The %s in C is used to print strings or take strings as input.

Syntax:

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

Example:

C

// C program to illustrate the use of %s in C

#include <stdio.h>

int main()

{

char a[] = "Hi Geeks";

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

return 0;

}

Output

Hi Geeks

Example: The working of %s with scanf() is a little bit different from its working with printf(). Let’s understand this with the help of the following C program.

C

// C Program to illustrate the working of %s with scanf()

#include <stdio.h>

int main()

{

char str[50];

// taking string as input

scanf("Enter the String: %s", str);

printf("Entered String: %s", str);

return 0;

}

Input

Enter the string: Hi Geeks

Output

Hi

As we can see, the string is only scanned till a whitespace is encountered. We can avoid that by using scansets in C.

8. Address Format Specifier – %p in C

The C language also provides the format specifier to print the address/pointers. We can use %p to print addresses and pointers in C

Syntax

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

Example:

C

#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

C language provides some tools using which we can format the input and output. They are generally inserted between the % sign and the format specifier symbol Some of them are as follows:

  1. A minus(-) sign tells left alignment.
  2. A number after % specifies the minimum field width to be printed if the characters are less than the size of the width the remaining space is filled with space and if it is greater then it is printed as it is without truncation.
  3. A period( . ) symbol separates field width with precision.

Precision tells the minimum number of digits in an integer, the maximum number of characters in a string, and the number of digits after the decimal part in a floating value.

Example of I/O Formatting

C

// C Program to demonstrate the formatting methods.

#include <stdio.h>

int main()

{

char str[] = "geeksforgeeks";

printf("%20s\n", str);

printf("%-20s\n", str);

printf("%20.5s\n", str);

printf("%-20.5s\n", str);

return 0;

}

Output

 geeksforgeeksgeeksforgeeks geeksgeeks 

FAQs on C Format Specifiers

1. Does C have a format specifier for binary numbers?

No, the C language does not provide a format specifier for binary numbers.

2. What is the formatted string?

The input and output functions in C take a string as an argument that decides how the data is displayed on the screen or the data is retrieved to the memory. This string is called the formatted string.



him0000

Improve

Previous Article

Basic Input and Output in C

Next Article

printf in C

Please Login to comment...

Format Specifiers in C - GeeksforGeeks (2024)

FAQs

What are the format specifiers in C? ›

In C, format specifiers are represented by special characters that begin with the modulus/ percent symbol (%), followed by a character indicating the data type. For example, %d represents a decimal integer, %f represents a floating-point number, and %c represents a character.

What is %s and %d in C? ›

They are string format specifiers. Basically, %d is for integers, %f for floats, %c for chars and %s for strings.

What is %2d format specifier in C? ›

%2d outputs a decimal (integer) number that fills at least 2 character spaces, padded with empty space. E.g.: 5 → " 5", 120 → "120" %6.2f outputs a floating point number that fills at least 6 character spaces (including the decimal separator), with exactly 2 digits after the ".", padded with empty space.

What is the %2x in C? ›

"%2x" is a format specifier in C programming that is used for printing an unsigned integer (hexadecimal) value. In a formatted output string, the "%2x" sequence tells the compiler to replace it with the corresponding argument as a hexadecimal value, and to print it with at least 2 characters.

What is %f in C? ›

The %f is the floating point format specifier in C language that can be used inside the formatted string for input and output of float data type.

What are the type specifiers in C? ›

Main types
  • The C language provides the four basic arithmetic type specifiers char, int, float and double, and the modifiers signed, unsigned, short, and long. ...
  • The relation requirements are that the long long is not smaller than long, which is not smaller than int, which is not smaller than short.

Why do we use %s in C? ›

It's a format specifier that is used for strings. And also you can use it for characters in place of '%c'... Arin Bagul %s tells printf that the corresponding argument is to be treated as a string (in C terms, a 0-terminated sequence of char ); the type of the corresponding argument must be char * .

What is the difference between I and D format specifier in C? ›

What is the difference between the %i and %d format specifiers in C? In C, the %i and %d format specifiers are used to format integer values. The main difference between the two is that %i supports input in octal and hexadecimal notation, whereas %d does not. Both format specifiers output integers in decimal notation.

What is %s in printf? ›

Use %s as the printf String specifier in the text string being formatted. Use %S as the printf String specifier to output upper-case text. Precede the letter s with a number to specify field width. Put a negative sign after the % to left-justify the text.

What does 6.2 f mean in C? ›

"%6.2f" is called a format specifier. It describes how a number should be formatted for either input (scanf) or output (printf). The 6 is the overall field width, in other words how many characters total including all spaces, digits and even the decimal point.

What does 8.2 f mean in C? ›

%8.2f. This says you require a total field of 8 characters, within the 8 characters the last 2 will hold the decimal part. %.2f. The example above requests the minimum field width and the last two characters are to hold the decimal part.

What is 7.2 F in C? ›

the conversion specifier %7.2f tells printf to display the value as a 7-character wide field with 2 digits following the decimal point, or 9999.99 . If the type of the argument doesn't match the conversion specifier, the behavior is undefined.

Why do we use float in C? ›

In the C programming language, float is a data type used to represent single-precision floating-point numbers. Floating-point numbers are used to handle real numbers with decimal points, providing a way to represent a wide range of values, from very small to very large, and with fractional precision.

What is double F in C? ›

A double in c can be printed using both %f and %lf. Both the %f format specifier and the %lf format specifier represent float and double. The printf() in c treats both float and double the same.

What is the double * in C? ›

In this case, double means a variable of type double. double* means a pointer to a double variable. double** means a pointer to a pointer to a double variable. In the case of the function you posted, it is used to create a sort of two-dimensional array of doubles.

What is %p in C? ›

%p is a format specifier in c language which is used to print data of type (* void). For example , address of a pointer or variable . The output is displayed in hexadecimal value.

What is %lf in C? ›

Format Specifier for Double:

The format specifier for double in C is %lf. The l in %lf specifies that the argument is a long double, which is the data type that double gets promoted to when passed as a variadic argument in C. Let's see an example to understand how to use a format specifier for double in C language.

Why %U is used in C? ›

The %u is an unsigned integer format specifier. It is used inside the formatted string to specify the unsigned integer types in functions such as printf(), scanf(), etc. An unsigned Integer means the variable can hold only a positive value.

What is %g in C? ›

%g or %G Decimal floating point number Equivalent to %f or %e, whichever is shorter. The case of the 'g' in the format specifier indicates the case of the 'e' in the output, if scientific notation is used. Precision specifies the maximum number of significant digits to generate.

Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 6075

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.