× Home
Next Lec → ← Previous Lec

C Format Specifiers and Escape Sequences With Examples

What is a Format Specifier

Some important Format Specifiers

☆ %c → prints character
☆ %d → prints interger
☆ %s → prints string
☆ %f → prints decimal numbers
☆ %l → prints long
☆ %lf → prints double
☆ %Lf → prints long double

Constants in C

//const keyword
#include <stdio.h>
int main()
{
const float b = 7.333;
b= 5.5;
return 0;
}
//error: assignment of read-only variable 'b'.
//This means that we cannot reassign value to a const variable.

//#define preprocessor
#include <stdio.h>
#deine PI 3.14
int main()
{
printf("%f",PI);
// prints 3.14000
return 0;
}
//There is no need to use ';' here.

Escape Sequences in C

☆ \t → Horizontal tab (space)
☆ \r → Vertical Tab
☆ \a → Alarm or Beep (Produces windows sound effect)
☆ \b → Backspace
☆ \f → Form Feed
☆ \n → New Line
☆ \r → Carriage Return
☆ \\ → Backslash
☆ \' → Single Quote
☆ \" → Double Quote
☆ \? → Question Mark
☆ \nnn → Octal Number
☆ \xhh → Hexadecimal Number
☆ \0 → Null

Comments

If we want some description of function that we are using in our programs then we can use comments to specify the function work.
The preprocessor removes all the comments and compresses the program code.

☆ single line comment → // this is single line comment.
☆ multi line comment → /* this is multi line comment
this is multi line comment */