Escape Sequences in C
Escape Sequences in C
In C programming language, there are 256 numbers of characters in character set. The
entire character set is divided into 2 parts i.e. the ASCII characters set and the extended
ASCII characters set. But apart from that, some other characters are also there which are
not the part of any characters set, known as ESCAPE characters.
List of Escape Sequences
// C program to illustrate
// \a escape sequence
#include <stdio.h>
int main(void)
return (0);
Output:
My mobile number is 7873923408.
// C program to illustrate
// \b escape sequence
#include <stdio.h>
int main(void)
// compilers.
printf("Hello Geeks\b\b\b\bF");
return (0);
Output:
The output is dependent upon compiler.
// C program to illustrate
// \n escape sequence
#include <stdio.h>
int main(void)
printf("Hello\n");
printf("GeeksforGeeks");
return (0);
Output:
Hello
GeeksforGeeks
// C program to illustrate
// \t escape sequence
#include <stdio.h>
int
main(void)
{
printf("Hello \t GFG");
return (0);
Output:
Hello GFG
The escape sequence “\t” is very frequently used in loop based pattern printing programs.
// C program to illustrate
// \v escape sequence
#include <stdio.h>
int main(void)
printf("Hello friends");
printf("\v Welcome to GFG");
return (0);
Output:
Hello Friends
Welcome to GFG
// sequence
#include <stdio.h>
int main(void)
return (0);
#include <stdio.h>
int main(void)
printf("Hello\\GFG");
return (0);
#include <stdio.h>
int main(void)
{
return 0;
Output:
' Hello Geeks
" Hello Geeks
// C program to illustrate
// \? escape sequence
#include <stdio.h>
int main(void)
printf("\?\?!\n");
return 0;
}
Output:
??!
#include <stdio.h>
int main(void)
// digits(0....7).
char* s = "A\0725";
printf("%s", s);
return 0;
Output:
A:5
Explanation : Here 000 is one to three octal digits(0….7) means there must be atleast
one octal digit after \ and maximum three.Here 072 is the octal notation, first it is
converted to decimal notation that is the ASCII value of char ‘:’. At the place of \072
there is : and the output is A:5.
// sequence
#include <stdio.h>
int main(void)
char* s = "B\x4a";
printf("%s", s);
return 0;
Output:
BJ
Explanation : Here hh is one or more hexadecimal digits(0….9, a…f, A…F).There can
be more than one hexadecimal number after \x. Here, ‘\x4a’ is a hexadecimal number and
it is a single char. Firstly it will get converted into decimal notation and it is the ASCII
value of char ‘J’. Therefore at the place of \x4a, we can write J. So the output is BJ.
int main ()
printf("HELLO WORLD!\n");
printf("Thank\tYou!");
printf("Thank\b\bYou!\a");
printf("\n\'Thank You!\'");
printf("\n\"Thank\tYou!\"");
printf("\nuser \\ document");
printf("\nHow\fYou\fdoing!");
return 0;
OUTPUT
HELLO WORLD!
Thank You!ThaYou!
'Thank You!'
"Thank You!"
user \ document
How
You
doing!
ASCII VALUE
#include <stdio.h>
int main()
printf("Enter a character");
return 0;
OUTPUT
Enter a character
C Arithmetic Operators
* multiplication
/ division
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
The operators + , - and * computes addition, subtraction, and multiplication
respectively as you might have expected.
In normal calculation, 9/4 = 2.25 . However, the output is 2 in the program.
It is because both the variables a and b are integers. Hence, the output is also
an integer. The compiler neglects the term after the decimal point and shows
answer 2 instead of 2.25 .
The modulo operator % computes the remainder. When a=9 is divided by b=4 ,
a/b = 2.5
a/d = 2.5
c/b = 2.5
c/d = 2
return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
Here, the operators ++ and -- are used as prefixes. These two operators can
also be used as postfixes like a++ and a-- . Visit this page to learn more about
how increment and decrement operators work when used as postfix.
C Assignment Operators
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Output
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
C Relational Operators
== Equal to 5 == 3 is evaluated to 0
return 0;
}
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
C Logical Operators
Logical AND. True only if all operands If c = 5 and d = 2 then, expression ((c==5) &&
&&
are true (d>5)) equals to 0.
Logical OR. True only if either one If c = 5 and d = 2 then, expression ((c==5) ||
||
operand is true (d>5)) equals to 1.
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
b) is 1 (true).
• (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
• (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
• (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c <
b) are 0 (false).
• !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b)
is 1 (true).
• !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0
(false).
C Bitwise Operators
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
Other Operators
Comma Operator
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
The sizeof is a unary operator that returns the size of data (constants,
variables, array, structure, etc).
Example 6: sizeof Operator
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
long e;
long double f;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
printf("Size of long=%lu byte\n",sizeof(e));
printf("Size of long double=%lu byte\n",sizeof(f));
return 0;
}
Output
Size of int=4 bytes
Output--Hello world!12
Ans=42