Constants in C

In the C programming language, constants are values that do not change during the execution of a program. They are used to represent fixed values that remain the same throughout the program’s execution. Constants can be of various types, such as integers, floating-point numbers, characters, or strings.

There are two types of constants in C:

  1. Numeric Constants:
    • Integer Constants: These are whole numbers without any fractional or decimal part. For example, 10, -5, 0.
    • Floating-point Constants: These are numbers with a fractional or decimal part. They can be expressed in decimal form or scientific notation. For example, 3.14, -0.5, 2e-3.
    • Character Constants: These are single characters enclosed in single quotes. For example, ‘A’, ‘5’, ‘@’.
    • Enumeration Constants: These are user-defined constants that represent a set of named integer values. They are defined using the enum keyword.
  2. String Constants:
    • String constants are sequences of characters enclosed in double quotes. For example, “Hello, World!”, “OpenAI”, “42”.

In C, constants are typically used in situations where you want to assign a fixed value to a variable or as operands in expressions.

Here’s an example that demonstrates the usage of constants in C:

#include <stdio.h>

int main() {
    const int MAX_VALUE = 100; // Integer constant
    const float PI = 3.14; // Floating-point constant
    const char NEW_LINE = '\n'; // Character constant
    const char GREETING[] = "Hello, World!"; // String constant

    printf("Maximum value: %d\n", MAX_VALUE);
    printf("PI: %f\n", PI);
    printf("New line: %c\n", NEW_LINE);
    printf("Greeting: %s\n", GREETING);

    return 0;
}

In the above example, MAX_VALUE, PI, NEW_LINE, and GREETING are constants of different types. They are assigned values that cannot be changed during the program’s execution.

List of Constants in C:

In C, there are several predefined constants available for use. Here is a list of some commonly used constants in C:

  1. Mathematical Constants:
    • M_PI: Represents the value of pi (π) approximately 3.14159265358979323846.
    • M_E: Represents the value of Euler’s number (e) approximately 2.71828182845904523536.
  2. Character Constants:
    • EOF: Represents the end-of-file marker.
  3. Integer Limits:
    • INT_MAX: Represents the maximum value that can be stored in an int.
    • INT_MIN: Represents the minimum value that can be stored in an int.
    • UINT_MAX: Represents the maximum value that can be stored in an unsigned int.
    • LONG_MAX: Represents the maximum value that can be stored in a long.
    • LONG_MIN: Represents the minimum value that can be stored in a long.
    • ULONG_MAX: Represents the maximum value that can be stored in an unsigned long.
    • LLONG_MAX: Represents the maximum value that can be stored in a long long.
    • LLONG_MIN: Represents the minimum value that can be stored in a long long.
    • ULLONG_MAX: Represents the maximum value that can be stored in an unsigned long long.
  4. Other Constants:
    • NULL: Represents a null pointer.
    • EXIT_SUCCESS: Represents a successful program termination.
    • EXIT_FAILURE: Represents a failed program termination.

These constants are defined in various header files, such as <math.h>, <limits.h>, <stdio.h>, and <stdlib.h>. To use these constants, you need to include the corresponding header file in your C program.

Here’s an example that demonstrates the usage of some of these constants:

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

int main() {
    printf("Value of pi: %.15f\n", M_PI);
    printf("Value of e: %.15f\n", M_E);

    printf("Maximum value of int: %d\n", INT_MAX);
    printf("Minimum value of int: %d\n", INT_MIN);
    printf("Maximum value of unsigned int: %u\n", UINT_MAX);
    
    printf("NULL pointer: %p\n", NULL);

    return EXIT_SUCCESS;
}

In this example, we use the %f format specifier to print the floating-point constants M_PI and M_E. The integer limits and NULL pointer constant are printed using the appropriate format specifiers.

2 ways to define constant in C:

In C, there are two common ways to define constants: using the #define preprocessor directive and using the const keyword.

  1. Using the #define preprocessor directive: The #define directive is a preprocessor directive that allows you to define constants by specifying a name and its corresponding value. It is a simple text substitution performed by the preprocessor before the compilation process. Here’s the syntax for defining a constant using #define:
#define CONSTANT_NAME value

The constant name is not enclosed in quotes, and there is no equal sign between the name and value. Here’s an example:

#include <stdio.h>

#define MAX_VALUE 100
#define PI 3.14

int main() {
    printf("Maximum value: %d\n", MAX_VALUE);
    printf("PI: %f\n", PI);

    return 0;
}

In this example, the MAX_VALUE constant is defined as 100, and the PI constant is defined as 3.14. Whenever the constant names MAX_VALUE and PI appear in the code, they will be replaced with their respective values during the preprocessing step.

2. Using the const keyword: The const keyword is used to define constants in C. It allows you to create variables that cannot be modified once initialized. Here’s the syntax for defining a constant using the const keyword:

const data_type CONSTANT_NAME = value;

The constant name is typically written in uppercase to indicate that it is a constant value. Here’s an example:

#include <stdio.h>

int main() {
    const int MAX_VALUE = 100;
    const float PI = 3.14;

    printf("Maximum value: %d\n", MAX_VALUE);
    printf("PI: %f\n", PI);

    return 0;
}
  1. In this example, the MAX_VALUE constant is defined as 100, and the PI constant is defined as 3.14. The const keyword ensures that these variables cannot be modified later in the program.

Both methods have their advantages and usage scenarios. The #define method is a simple text substitution, whereas the const method creates a typed constant that respects scope and type checking. The const method is generally considered more preferable because it provides better type safety and is easier to debug.