HTML/JavaScript

Notes on Format Specifiers and Escape Sequences in C

 


C Format Specifiers and Escape Sequences With Examples: C Tutorial In Hindi

In today’s tutorial, we will learn about the format specifiers and escape characters as it is one of the most important concepts in C programming. Whenever we write a code in C, we have to use the format specifiers to define the variable type in input and output and escape characters to format the output. So before exploring more C language concepts, first it will be better to have a strong grip on these concepts.

Format specifier in C:-

The format specifier in C programming is used for input and output purposes. Through this, we tell the compiler what type of variable we are using for input using scanf() or printing using printf(). Some examples are %d, %c, %f, etc.

The %c and %d used in the printf( ) are called format specifiers. The format specifier tells printf( ) to print the value, for %c, a character will be printed, and for %d, a decimal will be printed. Here is a list of format specifiers.

Format Specifier

Type

%c

Used to print the character

%d

Used to print the signed integer

%f

Used to print the float values

%i

Used to print the unsigned integer

%l

Used to a long integer

%lf

Used to print the double integer

%lu

Used to print the unsigned int or unsigned long integer

%s

Used to print the String

%u

Used to print the unsigned integer


Following are the few examples of format specifier:

  • To print the integer value:
printf("\n The value of integer is %d", d);
  • To print the float value:
printf("The value of float is %f", f);      
  • To print the character:
printf("\n The value of character is %c", c);      
  •  To print the string:
printf("\n The value of string is %s", s); 

What is Escape Sequence in C?

Many programming languages support the concept of Escape Sequence. An escape sequence is a sequence of characters that are used in formatting the output. They are not displayed on the screen while printing. Each character has its own specific function like \t is used to insert a tab and \n is used to add a newline.

Types of Escape Sequence in C

Escape Sequence

Description

\t

Inserts a tab

\b

Inserts a backspace

\n

Inserts a newline.

\r

Inserts a carriage return.

\f

Inserts a form feed.

\’

Inserts a single quote character.

\”

Inserts a double quote character.

\\

Inserts a backslash character.


 Following are some examples of escape sequence:

  • Print character backslash(\) using printf function
printf("\n C programming \m/ ");
  • Prints a newline before and after the text
printf("\n This is my C program\n"); · 
  • Use \" to print double quote and \' for a single quote
 printf("\n Welcome to \"The C Programming tutorial\"");
printf("\n Welcome to \'C programming series \'") ;    
  • To provide tab space between two words
printf("Hello \t Viewers"); 
  • To add a vertical tab character.
printf("Hello Viewers");
printf("\v Welcome to C Programming");

Post a Comment

0 Comments