C - Notes - 1
C - Notes - 1
The best way to learn a programming language is by writing programs. Typically, the first
program beginners write is a program called "Hello World", which simply prints "Hello World"
to your computer screen. Although it is very simple, it contains all the fundamental components
C++ programs have:
The left panel above shows the C++ code for this program. The right panel shows the result
when the program is executed by a computer. The grey numbers to the left of the panels are line
numbers to make discussing programs and researching errors easier. They are not part of the
program.
The function named main is a special function in all C++ programs; it is the function
called when the program is run. The execution of all C++ programs begins with
the main function, regardless of where the function is actually located within the code.
Notice that the statement ends with a semicolon ( ;). This character marks the end of the
statement, just as the period ends a sentence in English. All C++ statements must end
with a semicolon character. One of the most common syntax errors in C++ is forgetting
to end a statement with a semicolon.
You may have noticed that not all the lines of this program perform actions when the code is
executed. There is a line containing a comment (beginning with //). There is a line with a
directive for the preprocessor (beginning with #). There is a line that defines a function (in this
case, the main function). And, finally, a line with a statements ending with a semicolon (the
insertion into cout), which was within the block delimited by the braces ( { } ) of
the main function.
The program has been structured in different lines and properly indented, in order to make it
easier to understand for the humans reading it. But C++ does not have strict rules on indentation
or on how to split instructions in different lines. For example, instead of
1 int main ()
2{
3 std::cout << " Hello World!"; Edit & Run
4}
all in a single line, and this would have had exactly the same meaning as the preceding code.
In C++, the separation between statements is specified with an ending semicolon ( ;), with the
separation into different lines not mattering at all for this purpose. Many statements can be
written in a single line, or each statement can be in its own line. The division of code in different
lines serves only to make it more legible and schematic for the humans that may read it, but has
no effect on the actual behavior of the program.
In this case, the program performed two insertions into std::cout in two different statements.
Once again, the separation in different lines of code simply gives greater readability to the
program, since main could have been perfectly valid defined in this way:
int main () { std::cout << " Hello World! "; std::cout << " I'm a Edit &
C++ program "; } Run
The source code could have also been divided into more code lines instead:
1 int main ()
2{
3 std::cout <<
4 "Hello World!"; Edit & Run
5 std::cout
6 << "I'm a C++ program";
7}
And the result would again have been exactly the same as in the previous examples.
Preprocessor directives (those that begin by #) are out of this general rule since they are not
statements. They are lines read and processed by the preprocessor before proper compilation
begins. Preprocessor directives must be specified in their own line and, because they are not
statements, do not have to end with a semicolon ( ;).
Comments
As noted above, comments do not affect the operation of the program; however, they provide an
important tool to document directly within the source code what the program does and how it
operates.
1 // line comment
2 /* block comment */
The first of them, known as line comment, discards everything from where the pair of slash signs
(//) are found up to the end of that same line. The second one, known as block comment,
discards everything between the /* characters and the first appearance of the */ characters, with
the possibility of including multiple lines.
If comments are included within the source code of a program without using the comment
characters combinations //, /* or */, the compiler takes them as if they were C++ expressions,
most likely causing the compilation to fail with one, or several, error messages.
cout is part of the standard library, and all the elements in the standard C++ library are declared
within what is called a namespace: the namespace std.
In order to refer to the elements in the std namespace a program shall either qualify each and
every use of elements of the library (as we have done by prefixing cout with std::), or
introduce visibility of its components. The most typical way to introduce visibility of these
components is by means of using declarations:
The above declaration allows all elements in the std namespace to be accessed in
an unqualified manner (without the std:: prefix).
With this in mind, the last example can be rewritten to make unqualified uses of cout as:
1 // my second program in C++ Hello World! I'm a C++ program
2 #include <iostream>
3 using namespace std;
4
5 int main () Edit &
Run
6 {
7 cout << "Hello World! ";
8 cout << "I'm a C++ program";
9 }
Both ways of accessing the elements of the std namespace (explicit qualification
and using declarations) are valid in C++ and produce the exact same behavior. For simplicity,
and to improve readability, the examples in these tutorials will more often use this latter
approach with using declarations, although note that explicit qualification is the only way to
guarantee that name collisions never happen.
However, programming is not limited only to printing simple texts on the screen. In order to go a
little further on and to become able to write programs that perform useful tasks that really save
us work, we need to introduce the concept of variables.
Let's imagine that I ask you to remember the number 5, and then I ask you to also memorize the
number 2 at the same time. You have just stored two different values in your memory (5 and 2).
Now, if I ask you to add 1 to the first number I said, you should be retaining the numbers 6 (that
is 5+1) and 2 in your memory. Then we could, for example, subtract these values and obtain 4 as
result.
The whole process described above is a simile of what a computer can do with two variables.
The same process can be expressed in C++ with the following set of statements:
1 a = 5;
2 b = 2;
3 a = a + 1;
4 result = a - b;
Obviously, this is a very simple example, since we have only used two small integer values, but
consider that your computer can store millions of numbers like these at the same time and
conduct sophisticated mathematical operations with them.
Each variable needs a name that identifies it and distinguishes it from the others. For example, in
the previous code the variable names were a, b, and result, but we could have called the
variables any names we could have come up with, as long as they were valid C++ identifiers.
Identifiers
A valid identifier is a sequence of one or more letters, digits, or underscore characters ( _).
Spaces, punctuation marks, and symbols cannot be part of an identifier. In addition, identifiers
shall always begin with a letter. They can also begin with an underline character ( _), but such
identifiers are -on most cases- considered reserved for compiler-specific keywords or external
identifiers, as well as identifiers containing two successive underscore characters anywhere. In
no case can they begin with a digit.
C++ uses a number of keywords to identify operations and data descriptions; therefore,
identifiers created by a programmer cannot match these keywords. The standard reserved
keywords that cannot be used for programmer created identifiers are:
alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case,
catch, char, char16_t, char32_t, class, compl, const, constexpr, const_cast,
continue, decltype, default, delete, do, double, dynamic_cast, else, enum,
explicit, export, extern, false, float, for, friend, goto, if, inline, int,
long, mutable, namespace, new, noexcept, not, not_eq, nullptr, operator, or,
or_eq, private, protected, public, register, reinterpret_cast, return, short,
signed, sizeof, static, static_assert, static_cast, struct, switch, template,
this, thread_local, throw, true, try, typedef, typeid, typename, union,
unsigned, using, virtual, void, volatile, wchar_t, while, xor, xor_eq
Very important: The C++ language is a "case sensitive" language. That means that an identifier
written in capital letters is not equivalent to another one with the same name but written in small
letters. Thus, for example, the RESULT variable is not the same as the result variable or
the Result variable. These are three different identifiers identifiying three different variables.