Skip to main content

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.

C++ supports two ways of commenting code:

1
2
// line comment
/* 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.

Let's add comments to our second program:

/* my second program in C++
with more comments */

#include

int main ()
{
std::cout << "Hello World! "; // prints Hello World!
std::cout << "I'm a C++ program"; // prints I'm a C++ program
}



Output

Hello World! I'm a C++ program

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.

... Qweit ...

Popular posts from this blog

Best Ways to Initialize variables in Java programming - 2022 reviewed

When the variables in the example above are declared, they have an undetermined value until they are assigned a value for the first time. But it is possible for a variable to have a specific value from the moment it is declared. This is called the initialization of the variable. In C++, there are three ways to initialize variables. They are all equivalent and are reminiscent of the evolution of the language over the years: The first one, known as c-like initialization (because it is inherited from the C language), consists of appending an equal sign followed by the value to which the variable is initialized: type identifier = initial_value; For example, to declare a variable of type int called x and initialize it to a value of zero from the same moment it is declared, we can write:   int x = 0; A second method, known as constructor initialization (introduced by the C++ language), encloses the initial value between parentheses ( () ): type identifier

Java Literals

Literals are the most obvious kind of constants. They are used to express particular values within the source code of a program. We have already used some in previous chapters to give specific values to variables or to express messages we wanted our programs to print out, for example, when we wrote:   a = 5; The 5 in this piece of code was a literal constant . Literal constants can be classified into: integer, floating-point, characters, strings, Boolean, pointers, and user-defined literals. Integer Numerals 1 2 3 1776 707 -273 These are numerical constants that identify integer values. Notice that they are not enclosed in quotes or any other special character; they are a simple succession of digits representing a whole number in decimal base; for example, 1776 always represents the value one thousand seven hundred seventy-six . In addition to decimal numbers (those that most of us use every day), C++ allows the use of octal numbers (base 8)

Introduction to strings

Fundamental types represent the most basic types handled by the machines where the code may run. But one of the major strengths of the C++ language is its rich set of compound types, of which the fundamental types are mere building blocks. An example of compound type is the string class. Variables of this type are able to store sequences of characters, such as words or sentences. A very useful feature! A first difference with fundamental data types is that in order to declare and use objects (variables) of this type, the program needs to include the header where the type is defined within the standard library (header <string> ): // my first string #include <iostream> #include <string> using namespace std; int main () { string mystring; mystring = "This is a string" ; cout << mystring; return 0; }   printout:   This is a string As you can see in the previous example, strings can be initialized with any valid string l