Ads

Escape sequences and format specifier

ESCAPE SEQUENCES:

- Escape sequences in C language are mostly used in printf() function to take the control to specified point.
- For example \n takes the control to new line, and \t prints a tab space.

- Some of the escape sequences are mentioned in below table.

Characters used to denote
Function
\n
New line
\t
Horizontal tab space
\0
Null character, Terminates the string
\r
Carriage return, returns the control to starting position of present line
\b
Backspace
\v
Vertical tab space
\f
Form feed
\a
Beep sound
\’
Single quote
\”
Double quote
\\
Backslash
%%
Inserts a % symbol
NOTE: Some call them Control Characters and some call them Escape sequences


Program:

/* A program which explains the usage of printf function with escape sequence \n */

#include<stdio.h>
int main()
{
 printf("This is my first program\n");
 printf("\nThis is second line of my first program\n");

 return 0;
}

Output:
This is my first program
This is second line of my first program


Program:

/* How to print character backslash(\) using printf function */

#include<stdio.h>
int main()
{
 printf("\n We rock \m/ ");
 return 0;
}

Output:
Warning: Unknown escape sequence \m


Program:

/*Thus to print back slash we go for such type of method as shown below*/

#include<stdio.h>
int main()
{
 printf("\n We rock \\m/ ");
 return 0;
}

Output:
We rock \m/


Program:

/* Usage of quotation marks in printf*/

#include<stdio.h>
int main()
{
 printf("\n It's called "The C Programming Language"" );
 return 0;
}

Output:
Error!!


Program:

/* To avoid errors we use \" to print double quote and \' for single quote*/

#include<stdio.h>
int main()
{
 printf("\n It's called \"The C Programming Language\"");
 printf("\n You are in \'c-programminglanguage.com\'");

 return 0;
} 

Output:
It's called "The C Programming Language"
You are in 'c-programminglanguage.com'


FORMAT SPECIFIERS:

- The format specifier,  specifies the type of variable used.
- In C format specifers are used in printf and scanf functions to specify the type of variable the function is dealing with.
- Explained in below mentioned examples.

Some of the format specifiers mentioned in below table.
Format  Specifier Printf() (print) Scanf() (read)
%c Prints a single character Read a character
%d Prints a decimal integer Read a signed decimal integer
%i Prints a decimal integer Read a signed integer depending upon the prefix
- a hexadecimal integer if prefix is 0x
- a signed octal if prefix is 0
- if no prefix read signed decimal integer
%u A unsigned decimal integer Unsigned decimal integer
%o Unsigned octal integer Unsigned octal integer
%x Unsigned hexadecimal using a,b,c,d,e,f Read unsigned hexadecimal integer
%X Unsigned hexadecimal using A,B,C,D,E,F
%f A floating point with 6 digits after decimal point Read floating point number
%e Floating point in exponential format Read floating point number
%E Float in exponential format with E
%g Float in %f or %e whichever is shorter Floating point number
%G Float in %f or %E whichever is shorter
%s String Read a string (till null character)
%% Prints % sign Not applicable
%p Prints address in hexadecimal format Not applicable

- In printf function we can also use format specifiers like %d, %c, %f etc. to print the value of a variable. As shown in below examples where first we will create a variable and store a value in it and will print the value stored.


Program:

/* Program to print a integer value which is stored in memory*/

#include<stdio.h>
int main()
{
 int i=5; 
 printf("\n The value of i is %d", i);

 return 0;
}

Output:
The value of i is 5

Program:

/* Program to print a float value which is stored in memory*/

#include<stdio.h>
int main()
{
 float f=8.934;
 printf("The value of f is %f", f);

 return 0;
}

Output:

       The value of f is 8.934000


Program:

/* Program to print a character value stored in memory*/

#include<stdio.h>
int main()
{
 char c='a';
 printf("\n The value of c is %c", c);
 printf("\nThe ascii value is %d",c);

 return 0;
}

Output:

 The value of c is a
The ascii value is 97


Program:

/* A program to print the value returned by printf */

#include<stdio.h>
int main()
{
 int x;
 x=printf("This sentence has 31 characters");
 printf("\n The above printf returned a value and the value is %d",x);

 return 0;
}

Output:
This sentence has 31 characters
The above printf returned a value and the value is 31




No comments:

Post a Comment