Overview of Constants in C Language

1. Overview of Constants

A constant is a value that cannot be changed during the execution of a program. Unlike variables, which can change their values at runtime, once a constant is defined, its value cannot be altered. Constants play an important role in programs as they enhance code readability and maintainability, and they also assist the compiler in optimization.

2. Classification and Examples of Constants

(1) Integer Constants

Integer constants can be divided into three forms: decimal, octal, and hexadecimal.

  1. Decimal: This is the most commonly used representation, such as 123 and -456. They represent positive and negative integers, respectively.

  • Example Program:

#include<stdio.h>

int main() {
    printf("Decimal integer constant example:\n");
    printf("%d\n", 123); // Outputs 123
    printf("%d\n", -456); // Outputs -456
    return 0;
}
  1. Octal: Starts with 0, for example, 0123. Its value is 1 × 8² + 2 × 8¹ + 3 × 8⁰ = 83 (decimal).

  • Example Program:

#include<stdio.h>

int main() {
    printf("Octal integer constant example:\n");
    printf("%d\n", 0123); // Outputs 83
    return 0;
}
  1. Hexadecimal: Starts with 0x or 0X, followed by hexadecimal digits (0-9, a-f, or A-F). For example, 0x123, its value is 1 × 16² + 2 × 16¹ + 3 × 16⁰ = 291 (decimal). Additionally, 0Xabc is also a valid hexadecimal constant.

  • Example Program:

#include<stdio.h>

int main() {
    printf("Hexadecimal integer constant example:\n");
    printf("%d\n", 0x123); // Outputs 291
    printf("%d\n", 0Xabc); // Outputs 2748
    return 0;
}

(2) Floating-point Constants

Floating-point constants are used to represent numbers with decimal parts. They can be expressed in two forms: decimal form and exponential form.

  1. Decimal Form: For example, 12.34 and -0.567. Here, 12.34 represents twelve point three four, and -0.567 represents negative zero point five six seven.

  • Example Program:

#include<stdio.h>

int main() {
    printf("Floating-point constant (decimal form) example:\n");
    printf("%f\n", 12.34); // Outputs 12.340000
    printf("%f\n", -0.567); // Outputs -0.567000
    return 0;
}
  1. Exponential Form: The representation is [±] mantissa E [±] exponent. For example, 1.23E4 represents 1.23 × 10⁴ = 12300, and -5.67e-2 represents -5.67 × 10⁻² = -0.0567. Here, E or e indicates the base 10 exponent.

  • Example Program:

#include<stdio.h>

int main() {
    printf("Floating-point constant (exponential form) example:\n");
    printf("%f\n", 1.23E4); // Outputs 12300.000000
    printf("%f\n", -5.67e-2); // Outputs -0.056700
    return 0;
}

(3) Character Constants

Character constants are single characters enclosed in single quotes, such as ‘A’, ‘B’, ‘$’, etc. In memory, character constants are actually stored using their corresponding ASCII values. For example, the ASCII value of ‘A’ is 65.

  1. Regular Character Constants: For example, ‘A’, ‘B’, ‘$’, etc.

  • Example Program:

#include<stdio.h>

int main() {
    printf("Character constant example:\n");
    printf("%c\n", 'A'); // Outputs A
    printf("%d\n", 'B'); // Outputs 66 (ASCII value)
    return 0;
}
  1. Escape Characters: Start with a backslash (\) and are used to represent non-printable characters or characters with special functions. Common escape characters include:

  • \n: Newline character, used to move the cursor to the beginning of the next line. For example, printf(“Hello\nWorld!”); outputs Hello on one line and World on the next line.

  • \t: Horizontal tab character, used to create a tab space in output, typically used for text alignment. For example, printf(“Name:\tJohn\nAge:\t20”); aligns “John” and “20” in the output.

  • \: Represents the backslash itself. For example, printf(“This is a backslash: \”); outputs This is a backslash: \.

  • ‘ : Represents a single quote. For example, when outputting a character constant itself, printf(“The character is ‘;” outputs The character is ‘.

  • ” : Represents a double quote. For example, printf(“She said \”Hello\””); outputs She said “Hello”.

  • Example Program:

#include<stdio.h>

int main() {
    printf("Escape character example:\n");
    printf("Hello\nWorld!\n"); // Newline character example
    printf("Name:\tJohn\nAge:\t20\n"); // Horizontal tab character example
    printf("This is a backslash: \\n"); // Backslash example
    printf("The character is '\n"); // Single quote example
    printf("She said \"Hello\"\n"); // Double quote example
    return 0;
}

(4) String Constants

String constants are sequences of characters enclosed in double quotes, such as “Hello, world!”, “12345”, etc. When stored in memory, the system automatically adds a termination character ‘\0’ (null character) at the end of the string, which has an ASCII value of 0. For example, the string “abc” is actually stored in memory as ‘a’, ‘b’, ‘c’, ‘\0’.

  • Example Program:

#include<stdio.h>

int main() {
    printf("String constant example:\n");
    printf("%s\n", "Hello, world!"); // Outputs Hello, world!
    printf("%s\n", "12345"); // Outputs 12345
    return 0;
}

(5) Symbolic Constants

Symbolic constants are defined using the #define preprocessor directive. Its format is #define symbol_name constant_value. For example: #define PI 3.14159 allows using PI in the program to replace 3.14159. The advantage of symbolic constants is that if the value needs to be modified throughout the program, it only needs to be changed in one place, making maintenance easier. Symbolic constant names are usually written in uppercase letters to distinguish them from variable names.

  • Example Program:

#include<stdio.h>

#define PI 3.14159
#define MAX 100

int main() {
    printf("Symbolic constant example:\n");
    printf("PI = %f\n", PI); // Outputs PI = 3.141590
    printf("MAX = %d\n", MAX); // Outputs MAX = 100
    return 0;
}

(6) Reference Type Constants (C99 Standard and Later)

In the C99 standard and later, the const keyword can be used to define reference type constants. For example: const int MAX = 100; Here, MAX is an integer constant with a value of 100 that cannot be modified. This type of constant differs from variables in terms of memory storage; it has a memory address and can be accessed via pointers. For example: const int *p = &MAX printf(“%d”, *p); this can output the value of MAX. Using const to define constants can better control data modification, improving code readability and safety.

  • Example Program:

#include<stdio.h>

int main() {
    const int MAX = 100;
    const int *p = &MAX
    printf("Reference type constant example:\n");
    printf("MAX = %d\n", *p); // Outputs MAX = 100
    return 0;
}

3. Conclusion

Constants play an important role in C programming, enhancing code readability and maintainability. By using different types of constants, we can conveniently represent various data. In actual programming, we should choose the appropriate type of constant based on our needs.

4. Next Lecture Preview

In the next session, we will delve into operators in C language. Operators are core tools in programming, and C language has a rich variety of operators covering arithmetic, relational, logical, bitwise, and more. They play a key role in expressions, executing various calculations and comparisons, and are an essential foundation for building program logic.

Leave a Comment