The syntax of C++ defines how statements and expressions should be structured in order to create valid programs. It includes rules for writing declarations, expressions, control structures, functions, and more.
A C++ program typically consists of preprocessor directives, global declarations, and the main function. The main function is the entry point of the program where the execution starts.
#include<iostream> int main() { // Output a message to the console std::cout << "Enter your name: "; // Input a string from the user std::string name; std::cin >> name; // Output a personalized greeting std::cout << "Hello, " << name << "! Welcome to C++!" << std::endl; return 0; }
cout
and cin
.int
and doesn't take any parameters.std::cout
statement is used to output (print) text to the console. In this case, it displays the message "Enter your name: ".std::cin
statement is used to read input from the user. It takes the input and stores it in the variable name
, which is of type std::string
(a string data type in C++).std::cout
to output a personalized greeting to the console. It combines the string literals ("Hello, " and "! Welcome to C++!") with the user's input stored in the name
variable using the <<
operator.In this example, the program prompts the user to enter their name, reads the input, and then outputs a personalized greeting message. The cout object is used for output, allowing you to display text and values on the console. The cin object is used for input, allowing you to read user input from the console.