C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C programming language

Octal and Hexadecimal Escape Sequences in C

Traditionally, an Escape Sequence starts with backsplash (\) and a single character like '\n', '\t' etc. That are used for some specific formatting like '\n' is used set cursor at the beginning of next line and '\t' is used to set cursor on next tab stop.

We can also use some Octal and Hexadecimal values after the backsplash, that are known as Octal and Hexadecimal Escape Sequences.

New line using Octal and Hexadecimal Escape Sequence

"New line" or "Line feed" is a special character that has an ASCII value, The ASCII value of "New line" Escape sequence is 10 in Decimal; we can use its Octal value (12) and Hexadecimal value (0A) with backslash.

Octal Escape Sequence for New line - \012
Hexadecimal Escape Sequence for New line - \x0A

Example:

#include <stdio.h>
int main()
{
	printf("Hello\012world");
	printf("\n");
	printf("Hello\x0Aworld");
	printf("\n");
	return 0;
}

Output

Hello
world
Hello
world





TAB using Octal and Hexadecimal Escape Sequence

"Tab" or "Horizontal Tab" is a special character that has an ASCII value, The ASCII value of "Tab" Escape sequence is 9 in Decimal; we can use its Octal value (11) and Hexadecimal value (9) with backslash.

Octal Escape Sequence for New line - \011
Hexadecimal Escape Sequence for New line - \x09

Example:

#include <stdio.h>
int main()
{
	printf("Hello\011world");
	printf("\n");
	printf("Hello\x09world");
	printf("\n");
	return 0;
}

Output

Hello	world
Hello	world

Print any character like ‘A’ using Octal and Hexadecimal Escape Sequence

By using Octal and Hexadecimal values of any character, we can print any character. For example to print ‘A’ we can use \101 (Octal Escape Sequence), or \x41 (Hexadecimal Escape Sequence).

Remember: ASCII value of 'A' in Decimal is 65, in Octal is 101 and in Hexadecimal is 41.

Example:

#include <stdio.h>
int main()
{
	printf("\101");
	printf("%c",'\101');
	printf("\n");

	printf("\x41");
	printf("%c",'\x41');
	printf("\n");

	return 0;
}

Output

AA
AA

COMMENTS