Strings and Character Literals#
Adapted from: “Learn Modern C++” by cpptutor: Learn Modern C++: String and Character Literals
Program that Demonstrates Printing a Line of Text to the Console#
// 01-hellow.cpp : prints a line of text to the console
#include <print>
using namespace std;
int main()
{
println("Hello, World!"); // prints "Hello, World!" to the console
}
Explanation of the Above Code#
The above program is compiled and run using clang-18 in a Docker container. This is done so that the installation of a C++23 compiler is not required. The Docker container is based on the Ubuntu 22.04 image and includes clang-18, which supports C++23 features.
The program uses the print
library, which is a C++ library that provides a simple way to print formatted output to the console. The println
function is used to print a line of text followed by a newline character. The using namespace std;
statement allows us to use the standard library functions without prefixing them with std::
.
The #include <print>
directive includes the print
library, which is necessary for using the println
function. The main
function is the entry point of the program, and it returns an integer value to indicate the success or failure of the program. In this case, it returns 0 to indicate success.
The program is a simple example of how to print a line of text to the console in C++. It demonstrates the basic syntax and structure of a C++ program, including the use of header files, namespaces, and functions. The program can be compiled and run using clang to produce the desired output.
Compile and Run Code#
Use Python to Change to Working Directory#
import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Cpp_Code/01_String_and_Character_Literals"
os.chdir(code_dir)
!dir
01-hellow 01-hellow.cpp test.cpp
Use Docker to Compile the Code in a C++23 Environment#
!docker run --rm -v $(pwd):/app cpp23-clang18:latest clang++-18 -std=c++23 -stdlib=libc++ /app/01-hellow.cpp -o /app/01-hellow
Use Docker to Run Executable in a C++23 Environment#
!docker run --rm -v $(pwd):/app cpp23-clang18:latest ./01-hellow
Hello, World!