Chapter 2: Variable Initialization and Macros

Variable Initialization

Legal and Illegal Declarations

Here are some examples of variable declarations in C++:

int i;                  // an int
int *p;                 // an int pointer (pointer to an int)
int a[];                // an array of ints
int f();                // a function returning an int
int **pp;               // a pointer to an int pointer (pointer to a pointer to an int)
int (*pf)();            // a pointer to a function returning an int
int *ap[];              // an array of int pointers (array of pointers to ints)
int aa[][];             // an array of arrays of ints
int af[]();             // an array of functions returning an int (ILLEGAL)
int *fp();              // a function returning an int pointer
int fa()[];             // a function returning an array of ints (ILLEGAL)
int (**ppa)[];          // a pointer to a pointer to an array of ints
int (**ppf)();          // a pointer to a pointer to a function returning an int
int *(*pap)[];          // a pointer to an array of int pointers
int (*paa)[][];         // a pointer to an array of arrays of ints
int (*paf)[]();         // a pointer to an array of functions returning an int (ILLEGAL)
int *(*pfp)();          // a pointer to a function returning an int pointer
int (*pfa)()[];         // a pointer to a function returning an array of ints (ILLEGAL)
int (*pff)()();         // a pointer to a function returning a function returning an int (ILLEGAL)
int **app[];            // an array of pointers to int pointers
int (*apa[])[];         // an array of pointers to arrays of ints
int (*apf[])();         // an array of pointers to functions returning an int
int *aap[][];           // an array of arrays of int pointers
int *afp[]();           // an array of functions returning int pointers (ILLEGAL)
int afa[]()[];          // an array of functions returning an array of ints (ILLEGAL)
int aff[]()();          // an array of functions returning functions returning an int (ILLEGAL)
int **fpp();            // a function returning a pointer to an int pointer
int (*fpa())[];         // a function returning a pointer to an array of ints
int *fap()[];           // a function returning an array of int pointers (ILLEGAL)
int faa()[][];          // a function returning an array of arrays of ints (ILLEGAL)
int faf()[]();          // a function returning an array of functions returning an int (ILLEGAL)

Initialization Types

int a;             // Default initialization
int a = 5;         // Copy initialization
int b(5);          // Direct initialization
int c {5};         // List initialization
int d = {5};       // List initialization (alternative syntax)

[[maybe_unused]] int e {232};  // Variable marked as maybe_unused

Keywords

Refer to the list of C++ keywords here: C++ Keywords

Operator Arity

Statements and Expressions

int a;          // Declaration statement
int a = 5;      // Initialization expression
x = 5;          // Assignment statement (expression)

Preprocessor and Translation Unit

The process of compiling a C++ program involves several steps:

  1. Preprocessor: Processes directives (e.g., #define, #include).
  2. Translation Unit: The result after preprocessing, ready for compilation.
  3. Compiler: Translates the translation unit into machine code.

Macros

Object-like Macros with Substitution Text

#define MY_NAME "Alex"

std::cout << "My name is: " << MY_NAME << '\n';

Conditional Compilation

#define PRINT_JOE

int main() {
#ifdef PRINT_JOE
    std::cout << "Joe\n";  // Will be compiled since PRINT_JOE is defined
#endif

#ifdef PRINT_BOB
    std::cout << "Bob\n";  // Will be excluded since PRINT_BOB is not defined
#endif
}

Using Macros to Comment Code

#if 0  // Don't compile anything starting here !!!
std::cout << "This will not be compiled";
#endif