C++ #define

This post was written and published to explain the topic "#define," a C++ preprocessor command or directive, with examples. So, without further ado, let's get started.

Preprocessor commands that begin with a pound or hash symbol (#) are known as directives. There should be no white space before the #, and a semicolon is not required at the end of the statement beginning with "#."

Many things that can be done during the preprocessing phase include:

Through some preprocessor directives, you can also conditionally compile or execute some preprocessor directives.

Note: The preprocessing phase of a C++ program occurs before a program is compiled. The C++ preprocessor is a program that is executed before the source code is compiled.

You have already learned to work with the #include preprocessor directive, which lets you include desired header files in your program. This post is dedicated to the #define preprocessor directive.

The #define preprocessor allows us to define symbolic names and constants. For example,

#define PI 3.14159

This statement will translate every occurrence of PI in the program to 3.14159. Let me write another statement using "#define."

#define MAX 70

Before compilation, if the C++ preprocessor finds MAX as one word (so words like MAXIMUM will not be affected), in the source code, it replaces it with the number 70. If MAX was part of a string (for example, between quote marks in cout), the preprocessor will leave it alone.

You can call the macro definition anything you want, as long as it doesn't contain special characters or spaces, and it cannot start with a number. I tend to use upper-case and underscore characters. You can define strings as well.

#define NAME "Information Technology"

Every time the preprocessor sees NAME, it will replace it with "Information Technology"

This feature is particularly useful for defining "magic" numbers. An advanced use of #define is in the creation of macros.

C++ #define Example

Now consider the complete example wherein every occurrence of PI will be replaced with the value 3.14159, the value defined with the #define directive.

#include<iostream>
using namespace std;

#define PI 3.14159

int main()
{
   float radius, area;
   cout<<"Enter the radius of a circle: ";
   cin>>radius;

   area = PI * (radius*radius);

   cout<<"\nArea = "<<area;
   cout<<endl;

   return 0;
}

The following snapshot shows the initial output produced by the above C++ program:

c++ #define

Now type the radius of the circle, say "23.4", and hit the ENTER key to produce the following output:

c++ #define example

That is, since I defined the identifier "PI" with "3.14159," therefore, if I write "PI" anywhere in the program, then it will be replaced with "3.14159." This is not limited to using "#define" to give a name to a constant; you can also give a name to a loop or other C++ code, for example:

#define fori10 for(int i=0; i<10; i++)

The above statement states that the identifier "fori10" refers to the following code:

for(int i=0; i<10; i++)

As an example:

#include<iostream>
using namespace std;

#define fori10 for(int i=0; i<10; i++)

int main()
{
   int num = 1;
   fori10
   {
      cout<<num<<" ";
      num++;
   }
   cout<<endl;
   return 0;
}

The output should be:

1 2 3 4 5 6 7 8 9 10

In the above program, "fori10" refers to the following code:

for(int i=0; i<10; i++)

You can use #define to define the most frequently used code fragment in the program with a short identifier, not only making the program shorter but also saving time. However, I recommend using capital letters when naming a constant or a code fragment with the "#define" preprocessor command, so that the capital-letter words are clearly identified as macros or "#define" identifiers.

C++ Quiz


« Previous Tutorial Next Tutorial »