Symbolic Constants in C

The value of Pi is a constant, it never changes. The only question we really have with Pi is, “How precise do we want to be?” We need to insure a uniform amount of precision throughout the program, so that the value will not be changed by mistake. This means that we do not want the value of Pi to vary from function to function.

After all, the whole point of variables is that they are mutable. If we store Pi in a variable, there is nothing to insure that the value won’t be modified by accident.

A symbolic constant is a constant that is represented by a symbol in our program. Whenever we need the symbolic constant’s value in our program, we can its name just as we would use a variable’s name.

There are two ways to implement symbolic constants in C. Our first option here is to define Pi as a symbol that will be replaced in the program by a specified value during compilation. In this case, Pi wouldn’t be a variable at all, it would more like an alias or stand-in for the value it represents. We do this using what is know as a #define directive.

#include <stdio.h>

#define PI 3.14159

int main(void){
 
  double diameter = 5.5;
  double radius = diameter / 2.0;
 
  double circumference = 2.0*radius*PI;
 
  double area = PI*radius*radius;
 
  printf("The diameter of the circle is %.2f\n", diameter);
  printf("The radius of the circle is %.2f\n", radius);
  printf("The circumference of the circle is %.3f\n", circumference);
  printf("The area of the circle is %.3f\n", area);
 
  return 0;
 
 
}

Note that by convention identifiers that appear in a #define statement are written in capital letters.

All #define directives should be grouped together near the beginning of the file and before the main() function.

#include <stdio.h>

#define GRAMS_PER_POUND 454
#define METERS_PER_FOOT .3048

int main(void){
 
    int pounds1 = 115;
    int pounds2 = 175;
    
    int feet1 = 6;
    int feet2 = 300;
    
    printf("%d pounds = %d grams\n", pounds1, pounds1 * GRAMS_PER_POUND);
    printf("%d pounds = %d grams\n", pounds2, pounds2 * GRAMS_PER_POUND);
    
    printf("%d feet = %f meters\n", feet1, METERS_PER_FOOT * (double)feet1);
    printf("%d feet = %f meters\n", feet2, METERS_PER_FOOT * (double)feet2);
    
    return 0;
    
}

The second way to define a symbolic constant is with the const keyword. We can make the value of any variable immutable by prefixing the type name with the keyword const when we declare the variable. A value is initialized at the time of declaration and is then prohibited from being changed. Any code that attempts to change its value will be flagged as an error and the compilation will fail.

#include <stdio.h>


int main(void){
 
  const double Pi = 3.14159;
 
  double radius = 11.11;
  double circumference = radius * 2.0 * Pi;
  double area = radius * radius * Pi;
 
 
  printf("The radius of the circle is %f\n", radius);
  printf("The circumference of the circle is %f\n", circumference);
  printf("The area of the circle is %f\n", area);
 
  return 0;
 
}

Note that many functions in the standard library use const in their parameter declarations.

The header file <limits.h> defines symbolic constants that represent values for the limits of each integer data type.

#include <stdio.h>
#include <limits.h>


int main(void){
 
  printf("The char type stores values from %d to %d\n", CHAR_MIN, CHAR_MAX);
  printf("The int type stores values from %d to %d\n", INT_MIN, INT_MAX);
  printf("The unsigned int type stores values from 0 to %u\n", UINT_MAX);
  printf("The long long int type stores values form %lld to %lld\n", LLONG_MIN, LLONG_MAX);
 
 
  return 0;
 
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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