One way in which constants can be declared in C, as previously stated is using “const”.

 const int PI = 3.14159;

An alternative way to do this, is using something called a preprocessor directive. This is a piece of code that you will write at the very top.

 #define PI 3.14159

The lines beginning with the hash symbol (#) are not C statements. Notice that the #define statement doesn’t end in a semicolon, also there is no “=” sign between the identifier, PI, and the value, 3.14159.
These statements are rather referred to as preprocessor directives. The preprocessor goes through the code before compilation begins, and replaces every instance of, in this case, MYCONST with 23.
Hence, if you’re writing a program that uses pi, you write the line:

 #define PI 3.14159

At the top, and then everywhere where the value 3.14 is required, you write PI.
Once you run the command to compile your source code, the preprocessor goes through your code and replaces every instance of PI with 3.14159.
This is different from using:

 const int PI = 3.14159;

When using the const int, it works like any regular integer, the only exception being that if you try to assign PI a different value in your code, the compiler will throw an error.
With the preprocessor directive PI, it is essentially a placeholder for the number 3.14159.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s