Open In App

Format specifiers in different Programming Languages

Formatting in C

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: 
 



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 program to illustrate format
// specifiers in C
#include <stdio.h>
 
// Driver Code
int main()
{
    int N = 10;
    double F = 42.152;
 
    // Integer formatted output
    printf("%d \n", N);
 
    // Exponential formatted output
    printf("%e \n", F);
 
    // Unformatted String Output
    puts("Welcome to GeeksforGeeks!");
}

Output

10 
4.215200e+01 
Welcome to GeeksforGeeks!

Formatting in C++

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++: 

  1. 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.
  2. 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.
  3. 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.
  4. 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:  

Below is the program to illustrate some formatting streams in C++: 




// C++ program to illustrate format
// specifiers in C++
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
 
    string str = "GeeksforGeeks!";
 
    // Output stream to print string
    cout << str << endl;
 
    float f = 12.4578452f;
    // Print floating value upto N digits
    cout << setprecision(4) << f << endl;
 
    // To print the hexadecimal value of 42
    cout << hex << 42 << endl;
    return 0;
}

Output
GeeksforGeeks!
12.46
2a

Formatting in Java

In Java, Formatting output can be done in 2 different ways: 

Below is the program to illustrate some format specifiers in Python: 




// Java program to illustrate
// some format specifiers
 
import java.util.Scanner;
import java.io.PrintStream;
 
// Class Main
public class Main {
 
    // Driver Code
    public static void main(String[] args)
    {
 
        Scanner sc = new Scanner(System.in);
 
        // Name and Age
        String name = "GeeksforGeeks";
        int age = 5;
 
        // Formatted String using printf()
        System.out.printf(
            "Name: %s, Age: %d",
            name, age);
 
        System.out.println();
 
        // Formatted String using format()
        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.

Below is the program to illustrate some format specifiers in Python: 




# Python program to illustrate
# format specifiers in Python
 
str = "GeeksforGeeks"
str1 = "Welcome to"
 
# Unformatted Output in Python
print("Welcome to GfG !")
 
# Unformatted Output using separator
# print("Welcome to GfG !", sep = ", ")
print(str1, str, sep = ", ");
 
# String Formatting
print("Welcome to % s !" % str);
 
# String Formatting
name = "GfG"
age = 4
print("% s is % d years old." % (name, age))
 
# Formatting using format()
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!

Article Tags :