HOME HTML EDITOR C JAVA PHP

C Constants: Handling Fixed Values

In C, **Constants** (also known as literals) are fixed values that do not change during the execution of a program. Constants are used to represent values that are logically "read-only," such as the value of Pi, the number of days in a week, or a fixed tax rate. Using constants makes your code more secure and easier to maintain.

1. What are Constants?

A constant is an identifier whose value is immutable. Unlike a standard variable, if you try to re-assign a new value to a constant after its definition, the C compiler will throw an error. This prevents accidental changes to critical data.

2. Ways to Define Constants in C

There are two primary methods to define a constant in C. Each method has its own advantages depending on the scope and type of data.

A. Using the 'const' Keyword

The const keyword is the modern way to define a constant. It behaves like a variable but with a "read-only" restriction. It has a specific data type, which allows the compiler to perform type-checking.

#include <stdio.h>

int main() {
    const int DAYS_IN_WEEK = 7;
    const float PI = 3.14159;

    // DAYS_IN_WEEK = 10; // Error: assignment of read-only variable

    printf("There are %d days in a week.\n", DAYS_IN_WEEK);
    return 0;
}

B. Using the '#define' Preprocessor

The #define directive creates a "Macro." This is handled by the preprocessor before the actual compilation starts. It simply replaces every occurrence of the constant name with its value throughout the code.

#include <stdio.h>
#define GOLDEN_RATIO 1.618
#define NEWLINE '\n'

int main() {
    printf("Value: %f%c", GOLDEN_RATIO, NEWLINE);
    return 0;
}

3. Types of Literal Constants

Constants can represent different types of data, similar to variables. These are often called "Literals":

Literal Type Example Description
Integer Literal 45, -200 Whole numbers without any decimal point.
Floating-point 3.14, 2.5e2 Real numbers with fractional parts or exponents.
Character Literal 'Z', '7' Single characters enclosed in single quotes.
String Literal "C Program" A sequence of characters in double quotes.

4. Technical Differences: const vs #define

For AdSense-quality documentation, it is important to understand which one to use and why:

5. Naming Conventions for Constants

While not a strict rule of the C language, it is a Standard Industry Practice to write constant names in all **UPPERCASE** letters with underscores for spaces. For example, use BUFFER_SIZE instead of bufferSize. This makes it instantly obvious to anyone reading your code that the value is a constant.

6. Real-World Use Case: Circumference Calculation

In this example, we use a constant for PI to calculate the circumference of a circle based on user-provided radius.

#include <stdio.h>
#define PI 3.14159

int main() {
    float radius = 5.0;
    float circumference;

    circumference = 2 * PI * radius;

    printf("Radius: %.2f\n", radius);
    printf("Circumference: %.2f\n", circumference);

    return 0;
}
Important Note: Once you declare a constant using const, you must initialize it immediately. Writing const int x; x = 10; will result in an error because C considers the first line an attempt to create a "permanent" variable with a junk value.