Format Specifiers In C - GeeksforGeeks

Skip to content geeksforgeeks
  • Courses
    • DSA Courses
    • Programming Languages
  • Tutorials
    • Python Tutorial
      • Python Data Types
      • Python Loops and Control Flow
      • Python Data Structures
      • Python Exercises
    • Java
      • Java Programming Language
        • OOPs Concepts
      • Java Collections
      • Java Programs
      • Java Interview Questions
      • Java Quiz
      • Advance Java
    • Programming Languages
    • System Design
      • System Design Tutorial
    • Interview Corner
    • Computer Science Subjects
    • DevOps
    • Linux
    • Software Testing
    • Databases
    • Android
    • Excel
    • Mathematics
  • DSA
    • Data Structures
      • Linked List
      • Tree
    • Algorithms
      • Analysis of Algorithms
      • Searching Algorithms
      • Sorting Algorithms
    • Practice
      • Company Wise Coding Practice
      • Practice Problems Difficulty Wise
      • Language Wise Coding Practice
      • Curated DSA Lists
    • Company Wise SDE Sheets
    • DSA Cheat Sheets
    • Puzzles
  • Data Science
    • Data Science Packages
    • Data Visualization
    • Data Analysis
  • Web Tech
    • Web Development Using Python
      • Django
      • Flask
    • Cheat Sheets
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App Format Specifiers in C Last Updated : 11 Oct, 2024 Summarize Comments Improve Suggest changes Like Article Like Save Share Report Follow

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

signed 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

For those who want to explore how format specifiers are used with data structures and more complex operations, theC Programming Course Online with Data Structures covers this topic in detail with practical examples.

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> intmain() { charc; // using %c for character input scanf("Enter some character: %c",&c); // using %c for character output printf("The entered character: %c",&c); return0; }

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 intmain() { intx; // 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); return0; }

Input:

Enter the integer: 45

Output:

Printed using %d: 45Printed 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 intmain() { unsignedintvar; 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); return0; }

Input:

Enter an integer: 25

Output:

Entered unsigned integer: 25Printing -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 intmain() { floata=12.67; printf("Using %%f: %f\n",a); printf("Using %%e: %e\n",a); printf("Using %%E, %E",a); return0; } OutputUsing %f: 12.670000 Using %e: 1.267000e+01 Using %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> intmain() { inta=67; printf("%o\n",a); return0; } Output103

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> intmain() { inta=15454; printf("%x\n",a); printf("%X",a); return0; } Output3c5e 3C5E

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> intmain() { chara[]="Hi Geeks"; printf("%s\n",a); return0; } OutputHi 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> intmain() { charstr[50]; // taking string as input scanf("Enter the String: %s",str); printf("Entered String: %s",str); return0; }

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> intmain() { inta=10; printf("The Memory Address of a: %p\n",(void*)&a); return0; } OutputThe 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> intmain() { charstr[]="geeksforgeeks"; printf("%20s\n",str); printf("%-20s\n",str); printf("%20.5s\n",str); printf("%-20.5s\n",str); return0; } Output geeksforgeeks geeksforgeeks geeks geeks

Format Specifiers in C – FAQs

Does C have a format specifier for binary numbers?

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

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.

author him0000 Follow Improve Previous Article Basic Input and Output in C Next Article printf in C

Similar Reads

C program to print characters without using format specifiers As we know that there are various format specifiers in C like %d, %f, %c etc, to help us print characters or other data types. We normally use these specifiers along with the printf() function to print any variables. But there is also a way to print characters specifically without the use of %c format specifier. This can be obtained by using the be 1 min read Difference between %d and %i format specifier in C language A format specifier is a special character or sequence of characters used to define the type of data to be printed on the screen or the type of data to be scanned from standard input. A format specifier begins with a '%' character followed by the sequence of characters for different types of data. In short, it tells us which type of data to store an 3 min read Format String Vulnerability and Prevention with Example A format string is an ASCII string that contains text and format parameters. Example: // A statement with format stringprintf("my name is : %s\n", "Akash");// Output// My name is : Akash There are several format strings that specify output in C and many other programming languages but our focus is on C. Format string vulnerabilities are a class of 3 min read Encode an ASCII string into Base-64 Format Base 64 is an encoding scheme that converts binary data into text format so that encoded textual data can be easily transported over network un-corrupted and without any data loss. Base64 is used commonly in a number of applications including email via MIME, and storing complex data in XML. Problem with sending normal binary data to a network is th 15+ min read C Programming Language Tutorial In this C Tutorial, you’ll learn all C programming basic to advanced concepts like variables, arrays, pointers, strings, loops, etc. This C Programming Tutorial is designed for both beginners as well as experienced professionals, who’re looking to learn and enhance their knowledge of the C programming language. What is C?C is a general-purpose, pro 8 min read Top 50 C Coding Interview Questions and Answers (2024) C is the most popular programming language developed by Dennis Ritchie at the Bell Laboratories in 1972 to develop the UNIX operating systems. It is a general-purpose and procedural programming language. It is faster than the languages like Java and Python. C is the most used language in top companies such as LinkedIn, Microsoft, Opera, Meta, and N 15+ min read Top 25 C Projects with Source Codes for 2025 If you’re ready to improve your C programming skills, you’re in the right place! C, created by Dennis Ritchie in 1972, is the foundation of many modern programming languages like C++ and Java. Its simplicity, versatility, and widespread use make it a great choice for beginners and a must-know for programmers. This article is packed with C project i 13 min read Difference Between Call by Value and Call by Reference in C Functions can be invoked in two ways: Call by Value or Call by Reference. These two ways are generally differentiated by the type of values passed to them as parameters. The parameters passed to the function are called actual parameters whereas the parameters received by the function are called formal parameters. Call By Value in CIn call by value 5 min read printf in C In C language, printf() function is used to print formatted output to the standard output stdout (which is generally the console screen). The printf function is a part of the C standard library <stdio.h> and it can allow formatting the output in numerous ways. Syntax of printfprintf ( "formatted_string", arguments_list);Parametersformatted_st 5 min read Pattern Programs in C Printing patterns using C programs has always been an interesting problem domain. We can print different patterns like star patterns, pyramid patterns, Floyd's triangle, Pascal's triangle, etc. in C language. These problems generally require the knowledge of loops and if-else statements. In this article, we will discuss the following example progra 15+ min read C Programs To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and more. These C Examples cover a range of questions, 9 min read Switch Statement in C Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. Basically, it is used to perform different actions based on different conditions(cases). Switch case statements follow a selection-control mechanism and allow a value to change control of 8 min read C Programming Interview Questions (2024) At Bell Labs, Dennis Ritchie developed the C programming language between 1971 and 1973. C is a mid-level structured-oriented programming and general-purpose programming. It is one of the oldest and most popular programming languages. There are many applications in which C programming language is used, including language compilers, operating system 15+ min read Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() Since C is a structured language, it has some fixed rules for programming. One of them includes changing the size of an array. An array is a collection of items stored at contiguous memory locations. As can be seen, the length (size) of the array above is 9. But what if there is a requirement to change this length (size)? For example, If there is a 9 min read Prime Number Program in C A prime number is a natural number greater than 1 and is completely divisible only by 1 and itself. In this article, we will learn how to check whether the given number is a prime number or not in C. Examples Input: n = 29Output: 29 is primeExplanation: 29 has no divisors other than 1 and 29 itself. Hence, it is a prime number. Input: n = 15Output: 3 min read Format Specifiers in C 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 6 min read fgets() and gets() in C language For reading a string value with spaces, we can use either gets() or fgets() in C programming language. Here, we will see what is the difference between gets() and fgets(). fgets()The fgets() reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character i 3 min read Basics of File Handling in C File handling in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program. Why do we need File Handling in C?So far the operations using the 15 min read Substring in C++ The substring function is used for handling string operations like strcat(), append(), etc. It generates a new string with its value initialized to a copy of a sub-string of this object. In C++, the header file which is required for std::substr(), string functions is <string>. The substring function takes two values pos and len as an argument 8 min read Strings in C A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is stored as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'. C String Declaration SyntaxDeclaring a string in C is as simple as declaring a one- 8 min read Multidimensional Arrays in C - 2D and 3D Arrays Prerequisite: Arrays in C A multi-dimensional array can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays are 2D arrays and 3D arrays. In this article, we will learn about multidimensional arrays in C programming language. Synta 10 min read Decision Making in C (if , if..else, Nested if, if-else-if ) The conditional statements (also known as decision control structures) such as if, if else, switch, etc. are used for decision-making purposes in C programs. They are also known as Decision-Making Statements and are used to evaluate one or more conditions and make the decision whether to execute a set of statements or not. These decision-making sta 11 min read Operators in C In C language, operators are symbols that represent operations to be performed on one or more operands. They are the basic components of the C programming. In this article, we will learn about all the built-in operators in C with examples. What is a C Operator?An operator in C can be defined as the symbol that helps us to perform some specific math 14 min read C Pointers Pointers are one of the core components of the C programming language. A pointer can be used to store the memory address of other variables, functions, or even other pointers. The use of pointers allows low-level memory access, dynamic memory allocation, and many other functionality in C. In this article, we will discuss C pointers in detail, their 14 min read Data Types in C Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc. Each data type requires different amounts of memory and has some specific operations which can be performed over it. The data types in C can be classified as follows: Types Description Data Types 7 min read C Arrays Array in C is one of the most used data structures in C programming. It is a simple and fast way of storing multiple values under a single name. In this article, we will study the different aspects of array in C language such as array declaration, definition, initialization, types of arrays, array syntax, advantages and disadvantages, and many more 15+ min read C Structures The structure in C is a user-defined data type that can be used to group items of possibly different types into a single type. The struct keyword is used to define the structure in the C programming language. The items in the structure are called its member and they can be of any valid data type. Additionally, the values of a structure are stored i 10 min read Bitwise Operators in C In C, the following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise operations in C. The & (bitwise AND) in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. The | (bitwise OR) in C takes two nu 7 min read C Language Introduction C is a procedural programming language initially developed by Dennis Ritchie in the year 1972 at Bell Laboratories of AT&T Labs. It was mainly developed as a system programming language to write the UNIX operating system. The main features of the C language include: General Purpose and PortableLow-level Memory AccessFast SpeedClean SyntaxThese 6 min read Exception Handling in C++ In C++, exceptions are runtime anomalies or abnormal conditions that a program encounters during its execution. The process of handling these exceptions is called exception handling. Using the exception handling mechanism, the control from one part of the program where the exception occurred can be transferred to another part of the code. So basica 11 min read Article Tags :
  • C Language
  • c-input-output
Like three90RightbarBannerImg Explore More We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It ! Lightbox Improvement Suggest changes Suggest Changes Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal. geeksforgeeks-suggest-icon Create Improvement Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all. geeksforgeeks-improvement-icon Suggest Changes min 4 words, max CharLimit:2000

What kind of Experience do you want to share?

Interview Experiences Admission Experiences Career Journeys Work Experiences Campus Experiences Competitive Exam Experiences

Từ khóa » C 0x 02x