C++ red text output
To output red text in C++, you can use escape sequences with the help of the "iostream" library. Here's an example:
#include <iostream>
int main() {
// Print red text
std::cout << "\033[1;31mHello, World!\033[0m" << std::endl;
return 0;
}
Explanation:
The
#include <iostream>
statement includes the necessary library for input/output operations in C++.The
main()
function is the entry point of the program.The
std::cout
is used to perform output operations to the standard output stream, which is usually the console.The escape sequence
"\033[1;31m"
is used to change the text color to red.\033
is the escape character,[1;31m
represents the color code for red.The text you want to print in red is enclosed within the escape sequence and normal text is written as usual.
To reset the text color back to default, the escape sequence
"\033[0m"
is used.The
<< std::endl
is used to insert a newline after the text is printed.The
return 0;
statement indicates that the program has executed successfully.
Remember, the escape sequences for text color may vary depending on the platform and terminal being used.