Loading...
Loading...
00:00:00

What Is Format Specifiers?

In C language, format specifiers are special sequences of characters that begin with the percent symbol (%) and are used in functions such as printf() and scanf() to specify the type and format of data being input or output. Some common format specifiers in C include %d for integer, %f for float, %c for character, %s for string, and %p for pointer. These specifiers indicate the type of data that is being printed or read, such as integers, characters, and strings, and also the precision, width, and other formatting options for the output or input. 

In c programming language we can use these types of format specifiers which are listed below

Format Specifiers Data Types
%c char (Character)
%d Signed int (integer)
%f Float
%s String
%e or %E Exponential Format in floats
%g or %G Scientific notation in floats
%hi Signed int (short)
%hu Unsigned int (short)
%i Unsigned int
%l or %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
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
% string field width and precision
. (period) field width and precision
- (minus) left alignment

Format Specifiers Examples

Using the format specifiers we can print data types of variables. we can convert the value of variables internaly and print in some cases.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    char ch1 = 'P';
    char str1[] = "Pandit Programmer";
    int num1 = 20, num2 = 68;
    float f1 = 12.67;
    printf("The value of ch1 is %c\n", ch1); //print character
    printf("The value of ch1 is %d\n", ch1); //print ASCII code of character
    printf("The value of f1 is %f\n", f1);   //print float value (decimal)
    printf("The value of f1 is %e\n", f1);   //print in exponential notation
    printf("The value of is str1 %s\n", str1);   //print the string
    printf("The value of num1 is %o\n", num1); //print in octal format
    printf("The value of is num2 %x\n", num2); //print in hex format
    printf("The value of is str1 %-10s\n", str1);   //left align
    printf("The value of is str1 %10.5s\n", str1);  //add  10 space before characters including the string and printing 5 characters place
    printf("The value of is str1 %-10.5s\n", str1); //left align and print string with 5 characters place

    return 0;
}

Escape Sequences In C

Escape sequences characters in C language are special characters that are used to represent characters that cannot be represented directly in a string or character constant. These escape sequences are used with a backslash \ followed by a character or a sequence of characters. Some common escape sequences in C language are:

Escape Sequences Meaning
\a Alarm or Beep
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
\\ Backslash
\' Single Quote
\" Double Quote
\? Question Mark
\nnn octal number
\xhh hexadecimal number
\0 Null

Examples Of Escape Sequences Character

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    printf("\tWelcome To\n \"programming World\"");  
    return 0;
}