Using some popular escape sequence characters. – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#2288 siteicon   siteicon   siteicon  

Using some popular escape sequence characters.

Using some escape sequences in c++ for common tasks.

Learning Objectives

  • Learning to use special character displays in string and character literals using escape sequences.

Source Code

TC++ #2288


Source Code

Run Output

Taj Mahal is in India
Marks in Computers 95
How are you?
\ is a special character
This is "string literal"
'C' is father of "C++"
\101 is octal representation of A
\x41 is hexadecimal representation of A
Null character is written as \0

Code Understanding

cout<<“Taj Mahal is in India\n”;
Here the \n character is called the new line character. It bring the cursor to one line down and then moves to starting of line in next page. Operating system will usually convert this character to two characters called \n and \r which are newline character (OA) and carriage return character (0D).

cout<<“Marks in Computers\t95″<<endl;
The \t character here is the tab character which moves the cursor to defined width towards the right side. Usually this defaults to 8 characters movement towards the right side. In this example 95 will be printed after 8 spaces from the previous string.

cout<<“How are you\?”<<endl;
The \? escape sequence is compiler dependent, however when given with \ character it will always print the ? character only. Mostly the ? character can be given directly also.

cout<<“\\ is a special character”<<endl;
Since character itself is for marking an escape sequence so if you want to print it in an string you will have to use it twice .

cout<<“This is \”string literal\””<<endl;
Since ” characters are string delimiters so if they have to be printed they need the escape sequence form as ” .

cout<<“\’C\’ is father of \”C++\””<<endl;
The escape sequence for is required for single quote also as \’

cout<<“\\101 is octal representation of “<<‘\101′<<endl;
The octal number given here as character literal ‘101’ means the decimal value 65 is used which is character A as per ascii code. To use two digit octal code we will still write 3 digits for e.g. for 71 octal we will write  71 (This will be decimal 57 – the ascii code for 9).

cout<<“\\x41 is hexadecimal representation of “<<‘\x41′<<endl;
The hexadecimal number can also be given by prefixing it with \x. Here the character literal ‘\x41’ is the ascii code of character A, so it will be printed.

cout<<“Null character is written as \\0″<<endl;
The null character is used in case of character arrays to check for null-terminated strings. It is a non-printable character and it is the first character of the ascii code.


Common Errors

  • This is a very error prone code so extreme care is required in putting character at every place.

Suggested Filename(s): esc-seq.cpp


sunmitra| Created: 6-Jan-2018 | Updated: 15-Nov-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues