To compile your C++ code using GNU, you use the `g++` command followed by the name of your source file. For instance, if your file is named `hello.cpp`, you would use `g++ hello.cpp`.
g++ hello.cpp
After compiling, you can run your program using `./` followed by the name of the executable file. By default, the executable is named `a.out`.
./a.out
Single-line comments start with `//`. Everything after `//` on that line is ignored by the compiler.
// This is a single-line comment
Multi-line comments start with `/*` and end with `*/`. Everything between these markers is ignored by the compiler.
/* This is a multi-line comment. It spans multiple lines. */
A basic C++ program runs from top to bottom. Here’s a simple example:
#include <iostream> int main() { std::cout << "1\n"; std::cout << "2\n"; std::cout << "3\n"; return 0; }</iostream>
Use `std::cout` to print text to the screen. You use the `<<` operator to send the text to `std::cout`.
std::cout << "Hello World!\n";
Use `\n` to add a new line in your output. It moves the cursor to the next line.
std::cout << "Hello\n"; std::cout << "World\n";
Use `std::cin` to get input from the user. The `>>` operator reads the input and stores it in a variable.
int amount = 0; std::cout << "Enter amount: "; std::cin >> amount;
Variables store data. You declare a variable and then assign a value to it. For example, `int score = 0;` creates a variable `score` and sets it to 0.
int score; score = 0;
Arithmetic operators perform math operations. For example, `+` adds, `-` subtracts, `*` multiplies, `/` divides, and `%` gives the remainder.
int x = 4 + 2; // x is now 6 x = 4 - 2; // x is now 2 x = 4 * 2; // x is now 8 x = 4 / 2; // x is now 2 x = 4 % 2; // x is now 0
Use `double` for decimal numbers. For example, `double price = 8.99;` stores a decimal number.
double price = 8.99; double pi = 3.14159;
You can output multiple values in a single statement by chaining them with `<<`.
int age = 28; std::cout << "I'm " << age << ".\n";
Use `int` for whole numbers. For example, `int year = 1991;` stores an integer value.
int year = 1991; int age = 28;
Use `char` to store a single character. Characters are enclosed in single quotes, like `'A'`.
char grade = 'A'; char punctuation = '?';
Use `std::string` to store a sequence of characters (text). For example, `std::string name = "Alice";` stores a name.
std::string message = "Good night"; std::string user = "Codey";
Use `bool` to store `true` or `false` values. For example, `bool isValid = true;`.
bool isStudent = true; bool hasPaid = false;
An `if` statement checks if a condition is true. If it is, the code inside the `if` block runs.
if (a == 10) { // This code runs if a is 10 }
An `else` clause provides an alternative block of code that runs if the `if` condition is false.
if (year == 1991) { // Code runs if year is 1991 } else { // Code runs if year is not 1991 }
Relational operators compare values and return true or false. For example, `a > 10` checks if `a` is greater than 10.
if (a > 10) { // Code runs if a is greater than 10 }
An `else if` statement allows you to check multiple conditions. If the first condition is false, it checks the next one.
if (apple > 8) { // Code runs if apple is more than 8 } else if (apple > 6) { // Code runs if apple is more than 6 but not more than 8 } else { // Code runs if apple is 6 or less }
A `switch` statement allows you to choose between multiple options based on the value of a variable.
switch (grade) { case 9: std::cout << "Freshman\n"; break; case 10: std::cout << "Sophomore\n"; break; case 11: std::cout << "Junior\n"; break; case 12: std::cout << "Senior\n"; break; default: std::cout << "Invalid\n"; break; }
Logical operators combine multiple conditions. `&&` means 'and', `||` means 'or', and `!` means 'not'.
if (coffee > 0 && donut > 1) { // Runs if both conditions are true } if (coffee > 0 || donut > 1) { // Runs if either condition is true } if (!tired) { // Runs if tired is false }
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!