Format specifiers in different Programming Languages
Last Updated :
01 Nov, 2023

In C language format specifiers are used to input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). Below are the some format specifiers in C:
- %d or %i: Integer Format Specifier
- %c: Character Format Specifier
- %f: Floating-Point Format Specifier.
- %s: String Format Specifier.
- %lf: Double Format Specifier.
- %e or %E: Floating-Point Format Specifier ( Exponential ).
In C programming, we use scanf() for formatted input, and printf() for formatted output, gets() or getchar() for unformatted input, and puts() or putchar() for unformatted output.
Below is the program to illustrate some format specifiers in C:
C
#include <stdio.h>
int main()
{
int N = 10;
double F = 42.152;
printf ( "%d \n" , N);
printf ( "%e \n" , F);
puts ( "Welcome to GeeksforGeeks!" );
}
|
Output
10
4.215200e+01
Welcome to GeeksforGeeks!

As C++ is an extension of C Language but still we use input and output streams in order to format the input or output. Below are some input/output used in C++:
- Standard Input Stream(cin): In C++, cin is an object of istream. It takes input from the standard input device i.e., keyboard. cin is used along with an extraction operator (>>) in order to get or receive a stream of characters.
- Standard Output Stream(cout): In C++, cout is an object of ostream. It is used to print the output to standard output device i.e., Monitor. cout is used along with insertion operator(<<). If we use “endl” it will produce a newline character just like “\n” but it also has an additional behavior i.e., the output is to be physically written into a device if it wasn’t already. it affects the fully buffered streams but cout isn’t fully buffered so it is a good practice to use endl with cout.
- Unbuffered Standard Error Stream(cerr): In C++ cerr is an object of ostream. cerr is used along with insertion operator (<<). Unlike Buffered Output Unbuffered Output keeps writing the data to disk. In critical errors where there is a chance of system crash Buffered output isn’t preferred. But cerr is slow since it keeps on writing data into the disk.
- Buffered Standard Error Stream (clog): In C++ clog is used for logging purposes. It is an object of ostream. clog is used along with insertion operator (<<). In some cases, Buffered output is more efficient than unbuffered Output. Incase of Buffered output, all the output errors are stored in a variable and writes to disk all at a time.
Below is some Input/Output stream functions:
- setw() or width(): It is used to set the width to a given value. The output will be displayed in the given width.
- setprecision() or precision(): In float value, if we need to set a number of values to be printed after the decimal point.
- setiosflags(): It is used to set flags for formatting output.
- setfill() or fill(): It is used to fill in the blank space of a field.
- resetiosflags(): It is used to remove the flags that have been set.
Below is the program to illustrate some formatting streams in C++:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "GeeksforGeeks!" ;
cout << str << endl;
float f = 12.4578452f;
cout << setprecision(4) << f << endl;
cout << hex << 42 << endl;
return 0;
}
|
Output
GeeksforGeeks!
12.46
2a

In Java, Formatting output can be done in 2 different ways:
- System.out.printf(): It takes multiple arguments unlike print() and println() as print() and println() takes single argument.
- System.out.format(): It is similar to printf(). Both printf() and format() belongs to PrintStream of java.io package. This printf() and format() are similar to printf() function in C, we can also use flags with format specifiers.
Below is the program to illustrate some format specifiers in Python:
Java
import java.util.Scanner;
import java.io.PrintStream;
public class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String name = "GeeksforGeeks" ;
int age = 5 ;
System.out.printf(
"Name: %s, Age: %d" ,
name, age);
System.out.println();
System.out.format(
"%nName: %s%nAge: %d%n" ,
name, age);
}
}
|
Output
Name: GeeksforGeeks, Age: 5
Name: GeeksforGeeks
Age: 5
Formatting in Python

Python is currently the most widely used multi-purpose, high-level programming language. It is a dynamic language and very easy for formatting. <a href=”https://www.geeksforgeeks.org/taking-input-in-python/”>input() function in python always returns a string, by converting them into our required datatype we can perform different operations.
- Unformatted Specifiers: The print() function in Python is used to print the arguments passed in this function. We can use sep parameter to print the passed arguments with a separator.
- Formatted Specifiers: For formatted Output, we use format() function in Python in print() function to format the output.
For String formatting, Python uses C-style string formatting to create new, formatted strings. The “%” operator is used to format a set of variables enclosed in a list, together with a format string, which contains normal text together with “argument specifiers”, special symbols like “%s” and “%d”.
Below is the program to illustrate some format specifiers in Python:
Python3
str = "GeeksforGeeks"
str1 = "Welcome to"
print ( "Welcome to GfG !" )
print (str1, str , sep = ", " );
print ( "Welcome to % s !" % str );
name = "GfG"
age = 4
print ( "% s is % d years old." % (name, age))
print ( "Hey, Welcome to {}!" . format ( str , age))
|
Output
Welcome to GfG !
Welcome to, GeeksforGeeks
Welcome to GeeksforGeeks!
GfG is 4 years old.
Hey, Welcome to GeeksforGeeks!
Similar Reads
C/C++ Program for String Search
C/C++ Program for Naive Pattern SearchingC/C++ Program for KMP AlgorithmC/C++ Program for Rabin-Karp AlgorithmC/C++ Program for A Naive Pattern Searching QuestionC/C++ Program for Finite AutomataC/C++ Program for Efficient Construction of Finite AutomataC/C++ Program for Boyer Moore Algorithm â Bad
1 min read
Difference between pair in Multiset and Multimap in C++ STL
Pairs in C++: The pair container is a simple container defined in <utility> header consisting of two data elements or objects. The first element is referenced as âfirstâ and the second element as âsecondâ and the order is fixed (first, second). Pair is used to combine together two values which
5 min read
Understanding Lvalues, PRvalues and Xvalues in C/C++ with Examples
LValue and RValue in C Background Quite a few of you who are about to read this article, might be looking for clarification of what used to be basic: rvalues were thingies that can pop up on the right of an assignment operator, and lvalues were thingies that belong on the left or right an assignment
15+ min read
Structure of C++ Program
The C++ program is written using a specific template structure. The structure of the program written in C++ language is as follows: Documentation Section:This section comes first and is used to document the logic of the program that the programmer going to code.It can be also used to write for purpo
5 min read
Unformatted input/output operations In C++
In this article, we will discuss the unformatted Input/Output operations In C++. Using objects cin and cout for the input and the output of data of various types is possible because of overloading of operator >> and << to recognize all the basic C++ types. The operator >> is overlo
4 min read
Comparing Python with C and C++
In the following article, we will compare the 3 most used coding languages from a beginner's perspective. It will help you to learn basics of all the 3 languages together while saving your time and will also help you to inter shift from one language you know to the other which you don't. Let's discu
6 min read
ASCII Value of a Character in C
In this article, we will discuss about the ASCII values that are bit numbers used to represent the character in the C programming language. We will also discuss why the ASCII values are needed and how to find the ASCII value of a given character in a C program. Table of Content What is ASCII Value o
4 min read
C/C++ Programs
Array C/C++ Programs String C/C++ Programs Linked List C/C++ Programs Stack and Queue C/C++ Programs Tree C/C++ Programs Graph C/C++ Programs Bit Magic C/C++ Programs Misc C/C++ Programs Mathematical C/C++ Programs Dynamic Programming C/C++ Programs Greedy Algorithm C/C++ Programs Backtracking C/C++
1 min read
Lexical Analyzer in C++
A lexical analyzer, also known as a lexer or tokenizer, is an integral part of the compiler whose main function is to divide the input source code into logical units called tokens. These tokens are then transferred to the next phase of the compilation process. In this article, we will learn how to i
6 min read
C Library Functions
The Standard Function Library in C is a huge library of sub-libraries, each of which contains the code for several functions. In order to make use of these libraries, link each library in the broader library through the use of header files. The actual definitions of these functions are stored in sep
10 min read