C Programs Tutorials | IT Developer
IT Developer

C Programming - C Constants



Share with a Friend

C Programming - C Constants

C Constants

In C, constants are fixed values that cannot be modified during the execution of a program. Constants are used to make programs more readable, maintainable, and error-proof by replacing hardcoded values with meaningful names.

Types of Constants in C

  1. Integer Constants
  • Represent whole numbers.
  • Can be written in decimal, octal, or hexadecimal format.

Examples:

C

int decimal = 42;       // Decimal constant

int octal = 052;        // Octal constant (starts with 0)

int hexadecimal = 0x2A; // Hexadecimal constant (starts with 0x)

Type Example

Decimal

42

Octal

052

Hexadecimal

0x2A

  1. Floating-Point Constants
  • Represent numbers with fractional parts.
  • Can be written in decimal or scientific/exponential notation.

Examples:

C

float pi = 3.14159;        // Decimal constant

float scientific = 2.5e3;  // Scientific notation (2.5 × 10³)

  1. Character Constants
  • Represent a single character enclosed in single quotes (').
  • Correspond to their ASCII value.

Examples:

C

char letter = 'A';       // Character constant

char newline = '\n';     // Escape sequence constant

Character Constant Description

'A'

The character A

'\n'

Newline character

'\t'

Tab character

  1. String Constants
  • Represent a sequence of characters enclosed in double quotes (").
  • Always terminate with a null character (\0).

Examples:

C

const char *greeting = "Hello, World!";

  1. Symbolic Constants
  • Created using the #define preprocessor directive or the const keyword.

Using #define:

C

#define PI 3.14159

#define MAX 100

Using const :

C

const float PI = 3.14159;

const int MAX = 100;

  1. Boolean Constants
  • Introduced in C99 with <stdbool.h>.
  • Represent true and false.

Examples:

C

#include <stdbool.h>

const bool isAvailable = true;  // Boolean constant

Declaring Constants

Using const Keyword

  • The const keyword creates constants with a specific data type.

Example:

C

const int MAX = 50;

Using #define Preprocessor Directive

  • Used to create symbolic constants without a specific data type.

Example:

C

#define MAX 50

Feature const #define

Type-checking

Yes

No

Scope

Block-level

Global

Debugging Support

Yes

No

Rules for Constants

  1. Integer Constants:
    • Cannot have a decimal point.
    • Must fit within the range of the data type.
  2. Floating-Point Constants:
    • Must include a decimal point or an exponent.
  3. Character Constants:
    • Enclosed in single quotes.
    • Only one character is allowed.
  4. String Constants:
    • Enclosed in double quotes.
    • Can represent multiple characters.

Example Program

C

#include <stdio.h>

#define PI 3.14159 // Symbolic constant

int main() {

    const int MAX = 100; // Constant using const keyword

    const char newline = '\n'; // Character constant

    printf("The value of PI is: %.2f%c", PI, newline);

    printf("The maximum value is: %d%c", MAX, newline);

    return 0;

}

Output:

The value of PI is: 3.14

The maximum value is: 100

Advantages of Using Constants

  1. Readability:
    • Improves program readability by replacing hardcoded values with meaningful names.
  2. Maintainability:
    • Easy to update values in one place without changing the entire code.
  3. Error Prevention:
    • Prevents accidental modification of values.

Key Points

  1. Use const for type-checked, block-scoped constants.
  2. Use #define for preprocessor-based constants.
  3. Always use meaningful names for constants to enhance code readability.
  4. Use escape sequences for special character constants.

Constants are essential for creating robust and maintainable programs in C.