C++ preprocessors : macros

The Preprocessor directives are the lines reside in the header section, and just like #include directives, the preprocessor directives has # sign to begin with. The Preprocessors are also known as "macros"

These preprocessors directives can be used as :

  • constants -- key word #define
  • macros - key word #define
  • conditional compilation- key word #define
  • pragma-

#include <iostream>
#define team_roster 18
#define nothing "hello"
using namespace std;
//preprocessor_1.cpp
int main()
{
int n1[team_roster];
int n2 = team_roster;
char str[12]= nothing;
cout << "Welcome to C++!\n" << n1 <<"\n" << n2<<endl;
cout << str <<endl;
return 0; // indicate that program ended successfully
}

The preprocessor is not a part of C++, adding semicolon is optional. and it could be anything. n

#include <iostream> //provides std::cout
#define team_roster 18
#define nothing "hello" ;
using namespace std;// cout alone
//preprocessor_2.cpp
int main()
{
int n1[team_roster];
int n2 = team_roster;
char str[12]= nothing;
cout << "Welcome to C++!\n" << n1 <<"\n" << n2<<endl;
cout << str <<endl;
return 0; // indicate that program ended successfully
}

  • C programming
    • logical-OR-expression ? expression : conditional-expression
    • preprocessor in C e = ((a ? b : c) = d)
  • C++
    • however the above was wrong in C++
    • e = (a ? b : (c = d)) is valid operation.