This content originally appeared on DEV Community and was authored by ericaeducative
C++ is a multi-paradigm programming language that gives you incredible flexibility. In order to harness the power of the C++ language, you need to know how to handle commands. Every C++ program is written using commands. Commands are the directives that instruct a computer program on which tasks to perform.
In this article, we’ll cover nine basic commands to help you get started as a C++ programmer.
We’ll cover:
- Getting started with C++ commands
- 9 C++ commands to kick off your C++ programming journey
- Wrapping up and next steps
Getting started with C++ commands
What are commands?
(Hint: They’re a lot like they sound.)
Commands are directives instructing a computer to perform a certain task. Any C++ program operates off source code which consists of a culmination of commands determining your computer’s actions. There are many C++ commands to learn. As you start mastering the basics, you can even create your own C++ commands.
Commands can take various forms. Both keywords and functions can act as commands. There are also different types of commands. For instance, some commands are called conditional commands, because they define a condition upon which a certain task should be performed. This article also includes many preprocessing commands. Preprocessing commands are owned by the preprocessor rather than the compiler. The preprocessor processes the data and produces an output for use in the compiler. These commands are also known as preprocessor directives and are preceded by the hash sign (#
).
Conditional compilation commands are helpful because they specify conditions for compilation of part of a program. If those conditions aren’t met, the program can skip unnecessary compilations, saving time and resources.
C++ compilers
If you don’t have a compiler already, you’ll need one to start running C++ code. A compiler is a program that translates your written source code into executable machine code. Compilers are included in applications known as integrated development environments (IDEs) and code or text editors. Your choice of IDE or code editor might depend on your operating system. For instance, Dev-C++ only runs on Windows, while Visual Studio Code runs on Windows, Linux, and macOS.
9 C++ commands to kick off your C++ programming journey
1. #include
The #include
command tells your preprocessor to include a specified file in the program These files are usually header files from the standard library or a current, user-defined library. Header files contain C++ declarations and macro definitions. Including a header file in your program provides the definitions and declarations you need to use related system calls or libraries.
There are two types of include
commands, with distinct syntax and uses:
-
#include<filename>
- Use this command when including standard files from the standard library. These files contain definitions for pre-defined functions, and they must be included before using functions. For example, I/O functions are contained in an
iostream
file in the standard library. As such, you need to include theiostream file
before you use an I/O function such ascout
andcin
.
- Use this command when including standard files from the standard library. These files contain definitions for pre-defined functions, and they must be included before using functions. For example, I/O functions are contained in an
-
#include "filename"
- Use this command when including user-defined files from the current directory.
2. #define
The #define
command is used to define macros. A macro is a block of code in a program that is assigned a name. Whenever a defined macro’s name appears in your source code, the compiler will replace it with the block of code you assigned to that macro’s name. Leveraging macros makes the process of writing code less tedious.
The syntax for the #define
command is as follows:
#define macro_name replacement_text
Note: No semicolon is needed after a #define
statement.
3. using
The using
command tells your compiler to bring a specified member into a current scope, or a specified base class method or variable into the current class’ scope. The using
keyword is followed by whichever member, class method, or variable needs to be brought into scope.
For instance, we often see the using
keyword when we’re bringing a namespace such as std
into scope. The syntax for this scenario is the following:
#include <iostream>
using namespace std;
4. Main function definition
Defining the main function requires setting parameters within the parentheses. If no parameters are needed, the parentheses can be empty. You’ll use curly braces {}
to enclose the statements determining your function’s behavior. No semicolon is needed after function definition.
The general syntax for defining a function is:
Type Name ( Parameter1,Parameter2, ...) { Statements }
The following code shows an example of defining a main function of type int
:
#include <iostream>
using namespace std;
int main()
{
// function commands
}
5. Variable declaration
To declare a variable, begin by specifying the variable’s data type, followed by its variable name.
The following is the syntax for declaring an integer variable x
:
int x
6. if
if
is a conditional command. You’ll follow the if
keyword with a condition. If that condition is met, your compiler will then move on to perform the next task you define (in other words, the then of your if-then statement).
The following example demonstrates the syntax for if
, where $30 > 10$ is the condition upon which the program should display the cout
statement.
if (30 > 10) {
cout << “30 is greater than 10”;
}
7. cout
The cout
command is used with an insertion operator or output operator <<
. cout
prints the code following the insertion operator onto the console.cout
is defined in the iostream header file.
An example of the use of cout
is the following variation of “Hello world”:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world, ";
cout << "and all who inhabit it!";
}
==> Hello world, and all who inhabit it!
8. endl
endl
can be used with cout
to add a new line after text. With this new knowledge, let’s see how we can affect the output of the previous code for cout
.
#include <iostream>
using namespace std;
int main() {
cout << "Hello world," << endl;
cout << "and all who inhabit it!";
}
==> Hello world,
==> and all who inhabit it!
9. cin
The cin
command is used with the extraction operator or input operator >>
. cin
will assign an inputted value to the variable following the extraction operator. You can use cin
after variable declaration, and write a cin
statement to define a value for the variable. When the program runs, the input you’ve entered will be assigned to the variable. cin
is defined in the iostream header file.
cin
and cout
are often used together. In the following example, a cout
command displays a message prompting a user to input a value. The cin
command then assigns the value to the variable.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age"; // Prompts user to input value
cin >> age; // Assigns user input to variable age
}
Wrapping up and next steps
Congratulations! You now know nine basic C++ commands. There’s so much more to learn. The best way to learn is to practice using these commands in your code.
To help you hit the ground running as a C++ programmer, Educative has created the C++ from Scratch course. You’ll find several C++ tutorials and C++ templates. And with a cloud-based IDE, you can practice coding immediately from any computer without installing an IDE or code editor.
Happy learning!
Continue learning about C++
- Why C++ is a good first language to learn
- Learn C++ from scratch: The complete guide for beginners
- Is C++ still a good language to learn for 2022?
Start a discussion
Was this guide helpful for you? How have you been starting your C++ programming journey? Let us know in the comments below!
This content originally appeared on DEV Community and was authored by ericaeducative

ericaeducative | Sciencx (2021-12-22T17:28:09+00:00) 9 C++ commands to kick off your C++ programming journey. Retrieved from https://www.scien.cx/2021/12/22/9-c-commands-to-kick-off-your-c-programming-journey/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.