Online Test Banks
Score higher
See Online Test Banks
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Use Constants in C Programming

Even with C programming, computers and their electronic brethren enjoy doing repetitive tasks. In fact, anything you do on a computer that requires you to do something over and over demands that a faster, simpler solution be at hand. Often, it’s your mission to simply find the right tool to accomplish that goal.

How to use the same value over and over

It may be too early in your C programming career to truly ponder a repetitive program. But that doesn’t mean you can’t code programs that use values over and over.

Exercise 1: Create a new project, ex0511, and type in the source code, as shown in It’s a Magic Number. Save it, build it, run it.


#include <stdio.h>
int main()
  printf("The value is %d\n",3);
  printf("And %d is the value\n",3);
  printf("It's not %d\n",3+1);
  printf("And it's not %d\n",3-1);
  printf("No, the value is %d\n",3);

The code uses the value 3 on every line. Here’s the output:

The value is 3
And 3 is the value
It's not 4
And it's not 2
No, the value is 3

Exercise 2: Edit the code to replace the value 3 with 5. Compile and run.

You might think that Exercise 2 is cruel and requires a lot of work, but such things happen frequently in programming.

There has to be a better way.

Basics of constants in C programming

A constant is a shortcut — specifically, something used in your code to substitute for something else. A constant operates at the compiler level. It’s created by using the #define directive, in this format:

#define SHORTCUT constant

SHORTCUT is a keyword, usually written in all caps. It’s created by the compiler to represent the text specified as constant. The line doesn’t end with a semicolon because it’s a compiler directive, not a C language statement. But the constant you create can be used elsewhere in the code, especially in the statements.

The following line creates the constant OCTO, equal to the value 8:

#define OCTO 8

After defining the constant, you can use the shortcut OCTO anywhere in your code to represent the value 8 — or whatever other constant you define; for example:

printf("Mr. Octopus has %d legs.",OCTO);

The preceding statement displays this text:

Mr. Octopus has 8 legs.

The OCTO shortcut is replaced by the constant 8 when the source code is compiled.

  • The #define directive is traditionally placed at the top of the source code, right after any #include directives.

  • You can define strings as well as values:

    #define AUTHOR "Dan Gookin"

    The string that's defined includes the double quotes.

  • You can even define math calculations:

    #define CELLS 24*80
  • The definitions can be used anywhere in the source code.

How to put constants to use in C programming

Anytime your code uses a single value over and over (something significant, like the number of rows in a table or the maximum number of items you can stick in a shopping cart), define the value as a constant. Use the #define directive.

Preparing for Constant Updates shows an update to the source code in Exercise 1. The VALUE constant is created, defined as being equal to 3. Then that constant is used in the text. The constant is traditionally written in all caps, and you can see in the source code how doing so makes it easy to find, and identify as, a constant.


#include <stdio.h>
#define VALUE 3
int main()
  printf("The value is %d\n",VALUE);
  printf("And %d is the value\n",VALUE);
  printf("It's not %d\n",VALUE+1);
  printf("And it's not %d\n",VALUE-1);
  printf("No, the value is %d\n",VALUE);

Exercise 3: Create a new project named ex0513 using the source code from Preparing for Constant Updates. If you like, you can use the source code from Exercise 1 as a starting point. Build and run.

The output is the same as for the first version of the code. But now, whenever some bigwig wants to change the value from 3 to 5, you need to make only one edit, not several.

Exercise 4: Modify the source code from The Computer Does the Math so that the two values 8 and 2 are represented by constants.


#include <stdio.h>
int main()
  puts("Values 8 and 2:");
  printf("Addition is %d\n",8+2);
  printf("Subtraction is %d\n",8-2);
  printf("Multiplication is %d\n",8*2);
  printf("Division is %d\n",8/2);
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus

Inside Sweepstakes

Win an iPad Mini. Enter to win now!