Difference between C and C++
C++ was developed as an extension of c, and both languages have almost the same syntax.
The main difference between C and C++ is that C++ support classes and objects, while C does not.
C++ was developed as an extension of c, and both languages have almost the same syntax.
The main difference between C and C++ is that C++ support classes and objects, while C does not.
Get Started
This tutorial will teach you the basics of C++.
It is not necessary to have any prior programming experience.
To start using C++, you need two things:
- A text editor, like Notepad, to write C++ code
- A compiler, like GCC, to translate the C++ code into a language that the computer will understand
There are many text editors and compilers to choose from. In this tutorial, we will use an IDE (see below).
👍
This tutorial will teach you the basics of C++.
It is not necessary to have any prior programming experience.
To start using C++, you need two things:
- A text editor, like Notepad, to write C++ code
- A compiler, like GCC, to translate the C++ code into a language that the computer will understand
There are many text editors and compilers to choose from. In this tutorial, we will use an IDE (see below).
👍
myfirstprogram.cpp
using namespace std;
int main()
cout<< "Hello World!";
return 0;
}
output
C++ Syntax
Let's break up the following code to understand it better:
Let's break up the following code to understand it better:
Example
using namespace std;
int main()
cout<< "Hello World!";
return 0;
}
Example explained
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.
Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.
Line 4: Another thing that always appear in a C++ program, is int main()
. This is called a function. Any code inside its curly brackets {}
will be executed.
Line 5: cout
(pronounced "see-out") is an object used together with the insertion operator (<<
) to output/print text. In our example it will output "Hello World!".
Note: Every C++ statement ends with a semicolon ;
.
Note: The body of int main()
could also been written as:int main () { cout << "Hello World! "; return 0; }
Remember: The compiler ignores white spaces. However, multiple lines makes the code more readable.
Line 6: return 0
ends the main function.
Line 7: Do not forget to add the closing curly bracket }
to actually end the main function.
Omitting Namespace
You might see some C++ programs that runs without the standard namespace library. The using namespace std
line can be omitted and replaced with the std
keyword, followed by the ::
operator for some objects:
Example
int main()
std::cout<< "Hello World!";
return 0;
}
C++ Output (Print Text)
The cout
object, together with the <<
operator, is used to output values/print text:
using namespace std;
int main()
cout << "Hello World!";
return 0;
}
Example
using namespace std;
int main()
cout<< "Hello World!";
cout<< "I am learning C++";
return 0;
}
New Lines
To insert a new line, you can use the \n
character:
Example
using namespace std;
int main()
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
}
Both \n
and endl
are used to break lines. However, \n
is most used.
But what is \n
exactly?
The newline character (\n
) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.
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.
Single-line Comments
Single-line comments start with two forward slashes (//
).
Any text between //
and the end of the line is ignored by the compiler (will not be executed).
This example uses a single-line comment before a line of code:
Example
cout<< "Hello World!";
Example
cout << "Hello World!"; // This is a commen
C++ Multi-line Comments
Multi-line comments start with /*
and ends with */
.
Any text between /*
and */
will be ignored by the compiler:
Example
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";
0 Comments