C++ Comments
Comments can be used to explain C++ code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Comments can be singled-lined or multi-lined.
Comments can be used to explain C++ code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Comments can be singled-lined or multi-lined.
To insert a new line in your output, you can use the \n
character:
#include <iostream> using namespace std; int main() { cout << "Hello World! \n"; cout << "I am learning C++"; return 0; }
You can also use another <<
operator and place the \n
character after the text, like this : Continue reading C++ New Lines
You can also use cout()
to print numbers.
However, unlike text, we don’t put numbers inside double quotes:
#include <iostream>
using namespace std;
int main() {
cout << 3;
return 0;
}
You can also perform mathematical calculations: Continue reading C++ Output Numbers
The cout
object, together with the <<
operator, is used to output values and print text.
Just remember to surround the text with double quotes (""
): Continue reading C++ Output
A computer program is a list of “instructions” to be “executed” by a computer.
In a programming language, these programming instructions are called statements.
The following statement “instructs” the compiler to print the text “Hello World” to the screen: Continue reading C++ Statements
Let’s break up the following code to understand it better:
#include <iostream> using namespace std; int main() { cout << "Hello World!"; return 0; }
Line 1: #include <iostream>
is a header file library that lets us work with input and output objects, such as cout
(used in line 5). Header files add functionality to C++ programs.
Line 2: using namespace std
means that we can use names for objects and variables from the standard library. Continue reading C++ Syntax
To start using C++, you need two things:
There are many text editors and compilers to choose from. In this tutorial, we will use an IDE (see below).
To demonstrate a practical example of dynamic memory, we created a program that can make a list of any length.
Regular arrays in C have a fixed length and cannot be changed, but with dynamic memory we can create a list as long as we like : Continue reading C Memory Management Example
When you no longer need a block of memory you should deallocate it. Deallocation is also referred to as “freeing” the memory.
Dynamic memory stays reserved until it is deallocated or until the program ends.
Once the memory is deallocated it can be used by other programs or it may even be allocated to another part of your program.
If the amount of memory you reserved is not enough, you can reallocate it to make it larger.
Reallocating reserves a different (usually larger) amount of memory while keeping the data that was stored in it.
You can change the size of allocated memory with the realloc()
function.
The realloc()
function takes two parameters:
int *ptr2 = realloc(ptr1, size);