cpp print variable value

Code Example:

#include <iostream>

int main() {
    int number = 10;
    std::cout << "The value of the number is: " << number << std::endl;
    return 0;
}

Explanation:

  • #include <iostream>: This line includes the input/output stream library, which allows us to perform input and output operations in C++.

  • int main() { ... }: This is the main function where the program execution begins. It returns an integer value indicating the exit status of the program.

  • int number = 10;: This declares an integer variable named number and initializes it with the value 10.

  • std::cout << "The value of the number is: " << number << std::endl;: This line uses the std::cout object to print the text "The value of the number is: " followed by the value of the number variable, and then a newline character to the standard output stream.

  • return 0;: This statement indicates that the program has executed successfully and is returning 0 to the operating system.