Escape Sequence
Escape Sequence
Escape Sequence
First of all, COMPUTER is printed and\n shifts the cursor to the next
line. Then SCIENCE is printed on second line. A screenshot of output is
shown below:
Tab (\t):
A TAB is equal to eight spaces. Whenever TAB button is pressed from the
keyboard, then 8 spaces are left blank. This escape sequence performs the
functionality of TAB key in the output stream. The code given below will
insert a TAB between two words.
cout<<COMPUTER\tSCIENCE;
As you can see after printing COMPUTER, 8 spaces are left blank and
then SCIENCE is printed on the screen.
First of all, COMPUTER is printed then a beep is played and after that
SCIENCE is printed.
Backspace (\b):
Whenever we want to delete a single character, we press the button
backspace from our keyboard. The same functionality can be achieved in
C++ output with this escape sequence. For example:
cout<<COMPUTER\bSCIENCE;
First of all, COMPUTER is printed and after that \b comes which deletes
the last character i.e. R. After that, SCIENCE is printed.
Single Quote (\):
To insert a single quote in the output, this escape sequence is used. Look
at the code written below:
cout<<\COMPUTER SCIENCE\;
This code prints single quotes at the start and end of the string.
First of all, COMPUTER is printed and after that \r comes which moves
the cursor at the beginning of the line and SCIENCE is printed which
overwrites the word written before.
After printing COMPUTER, the computer will include a blank paper and
then print SCIENCE.
Conclusion
This article helped us understand some of the important escape sequences
in C++. This is a kind of information a C++ beginner is expected to
understand and know.
This is a guest post by Kamal Choudhary:
Kamal Choudhary is a tech geek who writes C++ programming tutorials on his blog.
He is a student of computer science in the University of Gujrat, Pakistan. He loves to
write about computer programming. You can find his full bio here. Follow him on
twitter @ikamalchoudhary
Definition
C String
PREV NEXT
C Strings are nothing but array of characters ended with null character (\0).
This null character indicates the end of the string.
Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in C.
EXAMPLE FOR C STRING:
char string[20] = {f, r, e, s, h, 2, r, e, f, r, e, s, h, \0};
(or)
char string[20] = fresh2refresh;
(or)
char string [] = fresh2refresh;
Difference between above declarations are, when we declare char as string[20], 20 bytes of memory space
is allocated for holding the string value.
When we declare char as string[], memory space will be allocated as per the requirement during execution
of the program.