Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a string variable with its special characters

Tags:

c++

printf

When I want to special characters in a string I should use "\":

std::string text("item \t new_item\n")

But, if I print this string, obviously, it will print:

item      new_item

Is there a way set std::cout to print all special characters:

item \t new_item \n
like image 381
rph Avatar asked Nov 29 '25 03:11

rph


1 Answers

\t and \n will print a tab(4 white spaces) and a new line respectively.

If you want to print \t and \n as output. You have to add an extra \ as prefix before \t and \n.

std::string text("item \\t new_item \\n")
like image 155
Shravan40 Avatar answered Dec 01 '25 16:12

Shravan40



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!