C++ Basics: Essential Knowledge for Beginners

C++ Basics: Essential Knowledge for Beginners

Source: Content from the internet, thank you!

C++ Comments

Comments in a program are explanatory statements, and you can include comments in C++ code to enhance the readability of the source code. All programming languages allow some form of comments.

C++ supports single-line comments and multi-line comments. All characters in a comment will be ignored by the C++ compiler.

C++ comments start with /* and end with */. For example:

/* This is a comment */

/* C++ comments can also
 * span multiple lines
 */

Comments can also start with // and continue until the end of the line. For example:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World"; // prints Hello World
    return 0;
}</iostream>

When the above code is compiled, the compiler ignores // prints Hello World, and the result will be:

Hello World

Within /* and */ comments, // characters have no special meaning. Similarly, in // comments, /* and */ characters have no special meaning. Therefore, you can nest one type of comment within another. For example:

/* Comment for printing Hello World

cout << "Hello World"; // prints Hello World */

C++ Data Types

When programming in a programming language, you need various variables to store different types of information. A variable retains the memory location of the value it stores. This means that when you create a variable, some space is reserved in memory.

You may need to store information of various data types (such as character, wide character, integer, float, double, boolean, etc.), and the operating system allocates memory and determines what to store in the retained memory based on the variable’s data type.

Basic Built-in Types

C++ provides a rich variety of built-in data types and user-defined data types for programmers. The following table lists seven basic C++ data types:

Type Keyword
Boolean bool
Character char
Integer int
Float float
Double double
Void void
Wide Character wchar_t

Some basic types can be modified with one or more type modifiers:

  • signed

  • unsigned

  • short

  • long

The following table shows the memory occupied by various variable types when storing values, as well as the maximum and minimum values that can be stored by that type of variable.

Type Bit Width Range
char 1 byte -127 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -127 to 127
int 4 bytes -2147483648 to 2147483647
unsigned int 4 bytes 0 to 4294967295
signed int 4 bytes -2147483648 to 2147483647
short int 2 bytes -32768 to 32767
unsigned short int Range 0 to 65,535
signed short int Range -32768 to 32767
long int 4 bytes -2,147,483,647 to 2,147,483,647
signed long int 4 bytes Same as long int
unsigned long int 4 bytes 0 to 4,294,967,295
float 4 bytes +/- 3.4e +/- 38 (~7 digits)
double 8 bytes +/- 1.7e +/- 308 (~15 digits)
long double 8 bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t 2 or 4 bytes 1 wide character

From the table above, we can see that the size of variables may vary depending on the compiler and the computer being used.

The following example will output the size of various data types on your computer.

#include <iostream>
using namespace std;

int main() {
    cout << "Size of char : " << sizeof(char) << endl;
    cout << "Size of int : " << sizeof(int) << endl;
    cout << "Size of short int : " << sizeof(short int) << endl;
    cout << "Size of long int : " << sizeof(long int) << endl;
    cout << "Size of float : " << sizeof(float) << endl;
    cout << "Size of double : " << sizeof(double) << endl;
    cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
    return 0;
}

This example uses endl, which will insert a newline after each line, and the << operator is used to output multiple values to the screen. We also use the sizeof() function to get the size of various data types.

When the above code is compiled and executed, it will produce the following result, which may vary depending on the computer used:

Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4

typedef Declaration

You can use typedef to create a new name for an existing type. The syntax for defining a new type using typedef is as follows:

typedef type newname;

For example, the following statement tells the compiler that feet is another name for int:

typedef int feet;

Now, the following declaration is perfectly legal, creating an integer variable distance:

feet distance;

Enumeration Types

An enumeration type declares an optional type name and a set of identifiers to be used as values for that type. It can have zero or more identifiers that can be used as values for that type. Each enumerator is a constant of the enumeration type.

To create an enumeration, you need to use the keyword enum. The general form of an enumeration type is as follows:

enum enum-name { list of names } var-list;

Here, enum-name is the name of the enumeration type. The list of names { list of names } is separated by commas.

For example, the following code defines a color enumeration, where the variable c is of type color. Finally, c is assigned the value “blue”.

enum color { red, green, blue } c;
c = blue;

By default, the value of the first name is 0, the second name is 1, the third name is 2, and so on. However, you can also assign a special value to a name simply by adding an initial value. For example, in the following enumeration, green has the value 5.

enum color { red, green=5, blue };

Here, blue has the value 6 because, by default, each name is one greater than the name before it.

C++ Variable Scope

The scope is an area of the program where a variable is declared, and generally, there are three places where variables can be declared:

  • Variables declared inside a function or a code block are called local variables.

  • Variables declared in the definition of function parameters are called formal parameters.

  • Variables declared outside all functions are called global variables.

We will learn what functions and parameters are in later chapters. In this chapter, we will first explain the declaration of local and global variables.

Local Variables

Variables declared inside a function or a code block are called local variables. They can only be used by statements inside the function or the code block. The following example uses local variables:

#include <iostream>
using namespace std;

int main ()
{
  // Local variable declaration
  int a, b;
  int c;

  // Actual initialization
  a = 10;
  b = 20;
  c = a + b;

  cout << c;
  return 0;
}

Global Variables

Variables defined outside all functions (usually at the top of the program) are called global variables. The value of global variables remains valid throughout the program’s lifecycle.

Global variables can be accessed by any function. This means that once a global variable is declared, it is available throughout the entire program. The following example uses both global and local variables:

#include <iostream>
using namespace std;
// Global variable declaration
int g;

int main (){
  // Local variable declaration
  int a, b;
  // Actual initialization
  a = 10;
  b = 20;
  g = a + b;
  cout << g;
  return 0;
}

In the program, local and global variables can have the same name, but within the function, the value of the local variable will override the value of the global variable. Here is an example:

#include <iostream>
using namespace std;
// Global variable declaration
int g = 20;

int main (){
  // Local variable declaration
  int g = 10;
  cout << g;
  return 0;
}

When the above code is compiled and executed, it will produce the following result:

10

Initializing Local and Global Variables

When a local variable is defined, the system does not initialize it, and you must initialize it yourself. When a global variable is defined, the system automatically initializes it to the following values:

Data Type Default Initialization Value
int 0
char ‘\0’
float 0
double 0
pointer NULL

Correctly initializing variables is a good programming practice; otherwise, sometimes the program may produce unexpected results.

C++ Constants

A constant is a fixed value that does not change during the execution of the program. These fixed values are also called literals.

A constant can be of any basic data type and can be classified as integer numbers, floating-point numbers, characters, strings, and boolean values.

A constant behaves like a regular variable, except that the value of a constant cannot be modified after it is defined.

Integer Constants

Integer constants can be decimal, octal, or hexadecimal constants. The prefix specifies the base: 0x or 0X indicates hexadecimal, 0 indicates octal, and no prefix indicates decimal by default.

An integer constant can also have a suffix, which is a combination of U and L, where U indicates an unsigned integer and L indicates a long integer. The suffix can be uppercase or lowercase, and the order of U and L is arbitrary.

Here are some examples of integer constants:

212         // valid
215u        // valid
0xFeeL      // valid
078         // invalid: 8 is not an octal digit
032UU       // invalid: suffix cannot be repeated

Here are examples of various types of integer constants:

85         // decimal
0213       // octal
0x4b       // hexadecimal
30         // integer
30u        // unsigned integer
30l        // long integer
30ul       // unsigned long integer

Floating-Point Constants

A floating-point constant consists of an integer part, a decimal point, a fractional part, and an exponent part. You can represent a floating-point constant in decimal or exponential form.

When using decimal form, the decimal point, exponent, or both must be included. When using exponential form, the integer part, fractional part, or both must be included. The signed exponent is introduced by e or E.

Here are some examples of floating-point constants:

3.14159       // valid
314159E-5L    // valid
510E          // invalid: incomplete exponent
210f          // invalid: no decimal or exponent
.e55          // invalid: missing integer or fraction

Boolean Constants

There are two boolean constants, both of which are standard C++ keywords:

  • true represents true.

  • false represents false.

We should not consider the value of true as 1 and the value of false as 0.

Character Constants

Character constants are enclosed in single quotes. If the constant starts with L (only in uppercase), it indicates that it is a wide character constant (e.g., L’x’), and it must be stored in a wchar_t type variable. Otherwise, it is a narrow character constant (e.g., ‘x’), and it can be stored in a char type simple variable.

Character constants can be a regular character (e.g., ‘x’), an escape sequence (e.g., ‘\t’), or a universal character (e.g., ‘\u02C0’).

In C++, there are certain specific characters that have special meanings when preceded by a backslash, used to represent things like newline (‘\n’) or tab (‘\t’). The following table lists some of these escape sequences:

Escape Sequence Meaning
\ \ character
\’ ‘ character
” character
\? ? character
\a bell sound
\b backspace
\f form feed
\n newline
\r carriage return
\t horizontal tab
\v vertical tab
\ooo one to three digits octal
\xhh . . . one or more digits hexadecimal

The following example shows some escape sequence characters:

#include <iostream>
using namespace std;

int main() {
   cout << "Hello\tWorld\n\n";
   return 0;
}

When the above code is compiled and executed, it will produce the following result:

Hello   World

String Constants

String literals or constants are enclosed in double quotes “”. A string contains characters similar to character constants: ordinary characters, escape sequences, and universal characters.

You can use spaces as separators to break a long string constant into multiple lines.

The following example shows some string constants. The three forms shown below represent the same string.

"hello, dear"
"hello, \
deared"
"hello, " "d" "ear"

Defining Constants

In C++, there are two simple ways to define constants:

  • Using the #define preprocessor.

  • Using the const keyword.

#define Preprocessor

The following is the form for defining constants using the #define preprocessor:

#define identifier value

See the following example:

#include <iostream>
using namespace std;
#define LENGTH 10
#define WIDTH  5
#define NEWLINE '\n'

int main() {
   int area;

   area = LENGTH * WIDTH;
   cout << area;
   cout << NEWLINE;
   return 0;
}

When the above code is compiled and executed, it will produce the following result:

50

const Keyword

You can declare a constant of a specified type using the const prefix, as shown below:

const type variable = value;

See the following example:

#include <iostream>
using namespace std;

int main() {
   const int  LENGTH = 10;
   const int  WIDTH  = 5;
   const char NEWLINE = '\n';
   int area;

   area = LENGTH * WIDTH;
   cout << area;
   cout << NEWLINE;
   return 0;
}

When the above code is compiled and executed, it will produce the following result:

50

Please note that defining constants in uppercase letters is a good programming practice.

C++ Type Modifiers

C++ allows modifiers to be placed before the char, int, and double data types. Modifiers are used to change the meaning of basic types to better suit various situations.

The following data type modifiers are listed:

  • signed

  • unsigned

  • long

  • short

The modifiers signed, unsigned, long, and short can be applied to integers, while signed and unsigned can be applied to characters, and long can be applied to doubles.

The modifiers signed and unsigned can also be used as prefixes for long or short modifiers. For example: unsigned long int.

C++ allows shorthand notation to declare unsigned short integers or unsigned long integers. You can omit int and just write the words unsigned short or unsigned long, where int is implied. For example, the following two statements both declare unsigned integer variables.

unsigned x;
unsigned int y;

To understand the difference between signed and unsigned integer modifiers in C++, let’s run the following short program:

#include <iostream>
using namespace std;
/*
 * This program demonstrates the difference between signed and unsigned integers
*/
int main() {
   short int i;           // signed short integer
   short unsigned int j;  // unsigned short integer

   j = 50000;

   i = j;
   cout << i << " " << j;
   return 0;
}

When the above program runs, it will output the following result:

-15536 50000

In the result above, the unsigned short integer 50,000 is interpreted as the signed short integer -15,536.

C++ Type Qualifiers

Type qualifiers provide additional information about the variable.

Qualifier Meaning
const const type objects cannot be modified during program execution.
volatile The volatile modifier tells the compiler that the variable’s value may change in ways not explicitly specified by the program.
restrict Pointers modified by restrict are the only way to access the object they point to. Only C99 added the new type qualifier restrict.

C++ Storage Classes

Storage classes define the scope (visibility) and lifetime of variables/functions in a C++ program. These specifiers are placed before the type they modify. The following storage classes are available in C++ programs:

  • auto

  • register

  • static

  • extern

  • mutable

auto Storage Class

The auto storage class is the default storage class for all local variables.

{
   int mount;
   auto int month;
}

The above example defines two variables with the same storage class; auto can only be used within functions, meaning auto can only modify local variables.

register Storage Class

The register storage class is used to define local variables that are stored in registers instead of RAM. This means the maximum size of the variable equals the size of the register (usually a word), and you cannot apply the unary ‘&’ operator to it (because it has no memory location).

{
   register int  miles;
}

Registers are only used for variables that need fast access, such as counters. It should also be noted that defining ‘register’ does not mean the variable will be stored in a register; it means the variable may be stored in a register, depending on hardware and implementation constraints.

static Storage Class

The static storage class indicates to the compiler to maintain the existence of local variables throughout the program’s lifecycle, without needing to create and destroy them every time it enters and leaves the scope. Therefore, using static to modify local variables can maintain the value of local variables between function calls.

The static modifier can also be applied to global variables. When static modifies a global variable, it limits the variable’s scope to the file in which it is declared.

In C++, when static is applied to class data members, it causes only one copy of that member to be shared among all objects of the class.

#include <iostream>
// Function declaration
void func(void);
static int count = 10; /* Global variable */

int main() {
    while(count--) {
       func();
    }
    return 0;
}
// Function definition
void func(void) {
    static int i = 5; // Local static variable
    i++;
    std::cout << "Variable i is " << i;
    std::cout << " , Variable count is " << count << std::endl;
}

When the above code is compiled and executed, it will produce the following result:

Variable i is 6 , Variable count is 9
Variable i is 7 , Variable count is 8
Variable i is 8 , Variable count is 7
Variable i is 9 , Variable count is 6
Variable i is 10 , Variable count is 5
Variable i is 11 , Variable count is 4
Variable i is 12 , Variable count is 3
Variable i is 13 , Variable count is 2
Variable i is 14 , Variable count is 1
Variable i is 15 , Variable count is 0

extern Storage Class

The extern storage class is used to provide a reference to a global variable, which is visible to all program files. When you use ‘extern’, it points the variable name to a previously defined storage location for variables that cannot be initialized.

When you have multiple files and define a global variable or function that can be used in other files, you can use extern in other files to get a reference to the defined variable or function. You can understand extern as a way to declare a global variable or function in another file.

The extern modifier is typically used when two or more files share the same global variable or function, as shown below:

First file: main.cpp

#include <iostream>
int count;
extern void write_extern();

int main() {
   count = 5;
   write_extern();
}

Second file: support.cpp

#include <iostream>
extern int count;
void write_extern(void) {
   std::cout << "Count is " << count << std::endl;
}

Here, the extern keyword in the second file is used to declare the count that has already been defined in the first file main.cpp. Now, compile these two files as follows:

$g++ main.cpp support.cpp -o write

This will produce the write executable program, and attempting to execute write will produce the following result:

$ ./write
Count is 5

mutable Storage Class

The mutable qualifier applies only to class objects, which will be explained at the end of this tutorial. It allows the object’s members to override constants. In other words, mutable members can be modified through const member functions.

C++ Operators

Operators are symbols that tell the compiler to perform specific mathematical or logical operations. C++ has a rich set of built-in operators and provides the following types of operators:

  • Arithmetic Operators

  • Relational Operators

  • Logical Operators

  • Bitwise Operators

  • Assignment Operators

  • Miscellaneous Operators

This chapter will introduce arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and other operators one by one.

Arithmetic Operators

The following table shows all arithmetic operators supported by C++.

Assuming variable A has a value of 10 and variable B has a value of 20, then:

Operator Description Example
+ Adds two operands A + B will yield 30
Subtracts the second operand from the first A – B will yield -10
* Multiplies two operands A * B will yield 200
/ Divides numerator by denominator B / A will yield 2
% Modulus operator, remainder after division B % A will yield 0
++ Increment operator, increases integer value by 1 A++ will yield 11
Decrement operator, decreases integer value by 1 A– will yield 9

Example

See the following example to understand all available arithmetic operators in C++.

Copy and paste the following C++ program into test.cpp, compile, and run the program.

#include <iostream>
using namespace std;

int main() {
   int a = 21;
   int b = 10;
   int c;

   c = a + b;
   cout << "Line 1 - c's value is " << c << endl;
   c = a - b;
   cout << "Line 2 - c's value is " << c << endl;
   c = a * b;
   cout << "Line 3 - c's value is " << c << endl;
   c = a / b;
   cout << "Line 4 - c's value is " << c << endl;
   c = a % b;
   cout << "Line 5 - c's value is " << c << endl;
   c = a++;
   cout << "Line 6 - c's value is " << c << endl;
   c = a--;
   cout << "Line 7 - c's value is " << c << endl;
   return 0;
}

When the above code is compiled and executed, it will produce the following result:

Line 1 - c's value is 31
Line 2 - c's value is 11
Line 3 - c's value is 210
Line 4 - c's value is 2
Line 5 - c's value is 1
Line 6 - c's value is 21
Line 7 - c's value is 22

Relational Operators

The following table shows all relational operators supported by C++.

Assuming variable A has a value of 10 and variable B has a value of 20, then:

Operator Description Example
== Checks if the values of two operands are equal; if they are, the condition is true. (A == B) is not true.
!= Checks if the values of two operands are not equal; if they are not equal, the condition is true. (A != B) is true.
> Checks if the left operand’s value is greater than the right operand’s value; if so, the condition is true. (A > B) is not true.
< Checks if the left operand’s value is less than the right operand’s value; if so, the condition is true. (A < B) is true.
>= Checks if the left operand’s value is greater than or equal to the right operand’s value; if so, the condition is true. (A >= B) is not true.
<= Checks if the left operand’s value is less than or equal to the right operand’s value; if so, the condition is true. (A <= B) is true.

Example

See the following example to understand all available relational operators in C++.

Copy and paste the following C++ program into test.cpp, compile, and run the program.

#include <iostream>
using namespace std;

int main() {
   int a = 21;
   int b = 10;
   int c;
   if( a == b ) {
      cout << "Line 1 - a equals b" << endl;
   } else {
      cout << "Line 1 - a does not equal b" << endl;
   }
   if ( a < b ) {
      cout << "Line 2 - a is less than b" << endl;
   } else {
      cout << "Line 2 - a is not less than b" << endl;
   }
   if ( a > b ) {
      cout << "Line 3 - a is greater than b" << endl;
   } else {
      cout << "Line 3 - a is not greater than b" << endl;
   }
   /* Change the values of a and b */
   a = 5;
   b = 20;
   if ( a <= b ) {
      cout << "Line 4 - a is less than or equal to b" << endl;
   }
   if ( b >= a ) {
      cout << "Line 5 - b is greater than or equal to b" << endl;
   }
   return 0;
}

When the above code is compiled and executed, it will produce the following result:

Line 1 - a does not equal b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is less than or equal to b
Line 5 - b is greater than or equal to b

Logical Operators

The following table shows all logical operators supported by C++.

Assuming variable A has a value of 1 and variable B has a value of 0, then:

Operator Description Example
&& Called the logical AND operator. If both operands are non-zero, the condition is true. (A && B) is false.
|| Called the logical OR operator. If any one of the two operands is non-zero, the condition is true. (A || B) is true.
! Called the logical NOT operator. It reverses the logical state of its operand. If the condition is true, the logical NOT operator will make it false. !(A && B) is true.

Example

See the following example to understand all available logical operators in C++.

Copy and paste the following C++ program into test.cpp, compile, and run the program.

#include <iostream>
using namespace std;

int main() {
   int a = 5;
   int b = 20;
   int c;
   if ( a && b ) {
      cout << "Line 1 - condition is true" << endl;
   }
   if ( a || b ) {
      cout << "Line 2 - condition is true" << endl;
   }
   /* Change the values of a and b */
   a = 0;
   b = 10;
   if ( a && b ) {
      cout << "Line 3 - condition is true" << endl;
   } else {
      cout << "Line 4 - condition is not true" << endl;
   }
   if ( !(a && b) ) {
      cout << "Line 5 - condition is true" << endl;
   }
   return 0;
}

When the above code is compiled and executed, it will produce the following result:

Line 1 - condition is true
Line 2 - condition is true
Line 3 - condition is not true
Line 4 - condition is true

Bitwise Operators

Bitwise operators operate on bits and perform operations bit by bit. The truth table for &, |, and ^ is as follows:

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assuming A = 60 and B = 13, now represented in binary format, they are as follows:

A = 0011 1100

B = 0000 1101

—————–

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

The following table shows the bitwise operators supported by C++. Assuming variable A has a value of 60 and variable B has a value of 13, then:

Operator Description Example
& If present in both operands, the binary AND operator copies a bit to the result. (A & B) will yield 12, which is 0000 1100
| If present in either operand, the binary OR operator copies a bit to the result. (A | B) will yield 61, which is 0011 1101
^ If present in one operand but not both, the binary XOR operator copies a bit to the result. (A ^ B) will yield 49, which is 0011 0001
~ The binary complement operator is a unary operator that has the effect of

Leave a Comment