Say "Hello, World!" With C++
Download and Install CLion:
Launch CLion:
Create a New Project:
- On the CLion welcome screen, click on "New Project."
- Choose the "C++ Executable" template and click "Next."
- Enter a name for your project and choose a location to save it. Click "Create."
Configure CMake:
- CLion uses CMake to manage your project's build configuration.
- CLion will generate a CMakeLists.txt file for you.
- You can customize CMake settings in the "CMakeLists.txt" file as needed.
Write Your C++ Code:
- In the project window, you'll see the "src" folder. Right-click on it and select "New" > "C++ Source File."
- Enter a name for your source file (e.g., "main.cpp") and click "OK."
- CLion will open the newly created source file in the editor.
Write and Run Your Program:
- Write your C++ code in the editor. For example, you can start with a simple "Hello, World!" program.
- After writing the code, you can build and run it by clicking the green "Run" button on the top toolbar or pressing Shift + F10.
//My First C++ Program
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
View Output:
The output of your program will be displayed in the "Run" tool window at the bottom of the CLion interface.
Debugging:
CLion offers powerful debugging features. You can set breakpoints, inspect variables, and step through your code to diagnose issues.
Working of C++ "Hello World!" Program
//My First C++ Program#include <iostream>int main() {std::cout << "Hello, World!" << std::endl;return 0;}
// Your First C++ Program
In C++, any line starting with // is a comment. Comments are intended for the person reading the code to better understand the functionality of the program. It is completely ignored by the C++ compiler.
#include <iostream>
The #include is a preprocessor directive used to include files in our program. The above code is including the contents of the iostream file.
This allows us to use cout in our program to print output on the screen.
For now, just remember that we need to use #include <iostream> to use cout that allows us to print output on the screen.
int main() {...}
A valid C++ program must have the main() function. The curly braces indicate the start and the end of the function.
The execution of code beings from this function.
std::cout << "Hello World!";
std::cout prints the content inside the quotation marks. It must be followed by << followed by the format string. In our example, "Hello World!" is the format string.
Note: ; is used to indicate the end of a statement.
return 0;
The return 0; statement is the "Exit status" of the program. In simple terms, the program ends with this statement.
Remember that C++ development involves learning the language syntax, concepts, and best practices. You can use online resources, tutorials, and courses to enhance your C++ programming skills. As you become more comfortable with CLion and C++, you can start working on more complex projects and exploring advanced features of the IDE.
Prepared by: Malinda Gamage -> LinkedIn Profile
Comments
Post a Comment