# Learn C - C Constants

## Defining Named Constants

PI is a math constant. We can define Pi as a symbol that's to be replaced in the program by its value during compilation.

``````
#include <stdio.h>
#define PI   3.14159f                       // Definition of the symbol PI
//from  ww w  .  j a va  2 s. co  m
int main(void)
{
float diameter = 0.0f;
float circumference = 0.0f;
float area = 0.0f;

printf("Input the diameter of a table:");
scanf("%f", &diameter);

printf("\nThe circumference is %.2f. ", circumference);
printf("\nThe area is %.2f.\n", area);
return 0;
}
``````

The code above generates the following result.

## Note

The following line in the code above defines a constant value for PI.

`#define PI   3.14159f                       // Definition of the symbol PI `

This defines PI as a symbol that is to be replaced in the code by the string 3.14159f.

It's a common convention in C to write identifiers that appear in a #define directive in capital letters.

Where you reference PI the preprocessor will substitute the string that you have specified in the #define directive.

All the substitutions will be made before compiling the program.

We can also define Pi as a variable, but to tell the compiler that its value is fixed and must not be changed.

You can fix the value of any variable when you declare it by prefixing the type name with the keyword const.

For example:

`const float Pi = 3.14159f;                  // Defines the value of Pi as fixed `

In this way we can define PI as a constant numerical value with a specified type.

The keyword const for Pi causes the compiler to check that the code doesn't attempt to change its value.

## const

You can use a const variable in a variation of the previous example:

``````
#include <stdio.h>
/*from w w  w . jav a 2 s.  co m*/
int main(void)
{
float diameter = 0.0f;                    // The diameter of a table
const float Pi = 3.14159f;                // Defines the value of Pi as fixed

printf("Input the diameter of the table:");
scanf("%f", &diameter);