0% found this document useful (0 votes)
50 views23 pages

Escape Sequences in C

The document provides an overview of escape sequences in the C programming language, detailing various escape characters and their usage with examples. It also covers arithmetic, increment/decrement, assignment, relational, and logical operators, including their meanings and examples of how they work in C code. Additionally, it explains the output of various code snippets demonstrating these concepts.

Uploaded by

Ankit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views23 pages

Escape Sequences in C

The document provides an overview of escape sequences in the C programming language, detailing various escape characters and their usage with examples. It also covers arithmetic, increment/decrement, assignment, relational, and logical operators, including their meanings and examples of how they work in C code. Additionally, it explains the output of various code snippets demonstrating these concepts.

Uploaded by

Ankit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

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

Escape sequence Description Example Output

\n New line printf("Hello \n World"); Hello


World

\t Horizontal tab printf("Hello \t World"); Hello World

\' Single quote printf("Hello \'World\' "); Hello 'World'

\" Double quote printf("Hello \"World\" "); Hello "World"

\\ Backslash printf("Hello \\World"); Hello \World

Some coding examples of escape characters

// C program to illustrate

// \a escape sequence

#include <stdio.h>

int main(void)

printf("My mobile number "


"is 7\a8\a7\a3\a9\a2\a3\a4\a0\a8\a");

return (0);

Output:
My mobile number is 7873923408.

// C program to illustrate

// \b escape sequence

#include <stdio.h>

int main(void)

// \b - backspace character transfers

// the cursor one character back with

// or without deleting on different

// 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)

// Here we are using \n, which

// is a new line character.

printf("Hello\n");

printf("GeeksforGeeks");

return (0);

Output:
Hello
GeeksforGeeks

// C program to illustrate

// \t escape sequence

#include <stdio.h>

int

main(void)
{

// Here we are using \t, which is

// a horizontal tab character.

// It will provide a tab space

// between two words.

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)

// Here we are using \v, which

// is vertical tab character.

printf("Hello friends");
printf("\v Welcome to GFG");

return (0);

Output:
Hello Friends
Welcome to GFG

// C program to illustrate \r escape

// sequence

#include <stdio.h>

int main(void)

// Here we are using \r, which

// is carriage return character.

printf("Hello fri \r ends");

return (0);

Output: (Depend upon compiler)


ends
// C program to illustrate \\(Backslash)

// escape sequence to print backslash.

#include <stdio.h>

int main(void)

// Here we are using \,

// It contains two escape sequence

// means \ and \n.

printf("Hello\\GFG");

return (0);

Output: (Depend upon compiler)


Hello\GFG
Explanation : It contains two escape sequence means it after printing the \ the compiler
read the next \ as as new line character i.e. \n, which print the GFG in the next line

// C program to illustrate \' escape

// sequence/ and \" escape sequence to

// print single quote and double quote.

#include <stdio.h>

int main(void)
{

printf("\' Hello Geeks\n");

printf("\" Hello Geeks");

return 0;

Output:
' Hello Geeks
" Hello Geeks

// C program to illustrate

// \? escape sequence

#include <stdio.h>

int main(void)

// Here we are using \?, which is

// used for the presentation of trigraph

// in the early of C programming. But

// now we don't have any use of it.

printf("\?\?!\n");

return 0;

}
Output:
??!

// C program to illustrate \OOO escape sequence

#include <stdio.h>

int main(void)

// we are using \OOO escape sequence, here

// each O in "OOO" is one to three octal

// 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.

// C program to illustrate \XHH escape

// sequence
#include <stdio.h>

int main(void)

// We are using \xhh escape sequence.

// Here hh is one or more hexadecimal

// digits(0....9, a...f, A...F).

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.

Escape Sequences Program


#include <stdio.h>

int main ()

printf("HELLO WORLD!\n");

printf("\n"); // creating a new line!

printf ("Have a great day!\n");// creating a new line!

printf("Thank\tYou!");

printf("Thank\b\bYou!\a");
printf("\n\'Thank You!\'");

printf("\n\"Thank\tYou!\"");

printf("\nAre you having a great day\?");

printf("\nuser \\ document");

printf("\nHow\fYou\fdoing!");

printf("\nJust null values! \0 gone \0 gone");

return 0;

OUTPUT

HELLO WORLD!

Have a great day!

Thank You!ThaYou!

'Thank You!'

"Thank You!"

Are you having a great day?

user \ document

How

You

doing!

Just null values!

ASCII VALUE

#include <stdio.h>

int main()

char ch; // variable declaration

printf("Enter a character");

scanf("%c",&ch); // user input


printf("\n The ascii value of the ch variable is : %d", ch);

return 0;

OUTPUT

Enter a character

The ascii value of the ch variable is : 65

An operator is a symbol that operates on a value or a variable. For


example: + is an operator to perform addition.
C has a wide range of operators to perform various operations.

C Arithmetic Operators

An arithmetic operator performs mathematical operations such as addition,


subtraction, multiplication, division etc on numerical values (constants and
variables).

Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus


Operator Meaning of Operator

* multiplication

/ division

% remainder after division (modulo division)

Example 1: Arithmetic Operators

// Working of arithmetic operators


#include <stdio.h>
int main()
{
int a = 9,b = 4, 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("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 ,

the remainder is 1 . The % operator can only be used with integers.


Suppose a = 5.0 , b = 2.0 , c = 5 and d = 2. Then in C programming,

// Either one of the operands is a floating-point number

a/b = 2.5

a/d = 2.5

c/b = 2.5

// Both operands are integers

c/d = 2

C Increment and Decrement Operators

C programming has two operators increment ++ and decrement -- to change


the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the
value by 1. These two operators are unary operators, meaning they only
operate on a single operand.
Example 2: Increment and Decrement Operators

// Working of increment and decrement operators


#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);


printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);

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

An assignment operator is used for assigning a value to a variable. The most


common assignment operator is =
Operator Example Same as

= 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

Example 3: Assignment Operators

// Working of assignment operators


#include <stdio.h>
int main()
{
int a = 5, c;

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

A relational operator checks the relationship between two operands. If the


relation is true, it returns 1; if the relation is false, it returns value 0.

Relational operators are used in decision making and loops.


Operator Meaning of Operator Example

== Equal to 5 == 3 is evaluated to 0

> Greater than 5 > 3 is evaluated to 1

< Less than 5 < 3 is evaluated to 0

!= Not equal to 5 != 3 is evaluated to 1

>= Greater than or equal to 5 >= 3 is evaluated to 1

<= Less than or equal to 5 <= 3 is evaluated to 0


Example 4: Relational Operators

// Working of relational operators


#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);


printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);

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

An expression containing logical operator returns either 0 or 1 depending


upon whether expression results true or false. Logical operators are
commonly used in decision making in C programming.
Operator Meaning Example

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.

Logical NOT. True only if the operand


! If c = 5 then, expression !(c==5) equals to 0.
is 0

Example 5: Logical Operators

// Working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", 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

Explanation of logical operator program


• (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c >

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

During computation, mathematical operations like: addition, subtraction,


multiplication, division, etc are converted to bit-level which makes processing
faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.

Operators Meaning of operators

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left

>> Shift right

Visit bitwise operator in C to learn more.

Other Operators

Comma Operator

Comma operators are used to link related expressions together. For example:
int a, c = 5, d;

The sizeof operator

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

Size of float=4 bytes

Size of double=8 bytes


Size of char=1 byte

Size of long=8 byte

Size of long double=16 byte

Other operators such as ternary operator ?: , reference operator & ,


dereference operator * and member selection operator -> will be discussed in
later tutorials.

Output--Hello world!12
Ans=42

You might also like