Skip to content

Constants in C

Constants in C are values that remain unchanged during the execution of a program. They are like variables, but their values cannot be modified once they are assigned. Constants can be of different types, such as integer constants, floating-point constants, character constants, and string constants. Let’s explore them in detail:

Types of Constants in C:

  1. Integer Constants:
    • Integer constants are whole numbers without any fractional part.
    • They can be represented in decimal, octal (base 8), or hexadecimal (base 16) notation.
    • Examples:
      • Decimal: 10, -25, 0.
      • Octal: 037, -012.
      • Hexadecimal: 0xA, 0xFF.
  2. Floating-Point Constants:
    • Floating-point constants represent real numbers with fractional parts.
    • They can be written in standard decimal notation or using scientific notation.
    • Examples:
      • Decimal: 3.14, -0.5, 10.0.
      • Scientific Notation: 6.022e23 (Avogadro’s number), -1.6e-19 (charge of an electron).
  3. Character Constants:
    • Character constants represent single characters enclosed in single quotes (‘ ‘).
    • They can be regular characters, escape sequences, or ASCII values.
    • Examples:
      • ‘A’, ‘x’, ‘9’.
      • Escape Sequences: ‘\n’ (newline), ‘\t’ (tab), ‘\0’ (null character).
      • ASCII Values: ‘\x41’ (ASCII value of ‘A’), ‘\x30’ (ASCII value of ‘0’).
  4. String Constants:
    • String constants represent sequences of characters enclosed in double quotes (” “).
    • They are arrays of characters terminated by a null character (‘\0’).
    • Examples:
      • “Hello”, “C Programming”, “” (empty string).
      • Note: String constants are null-terminated by default.

Examples:

const int MAX_VALUE = 100; // Integer constant

const float PI = 3.14159; // Floating-point constant

const char GRADE = ‘A’; // Character constant

const char* MESSAGE = “Hello, World!”; // String constant

 int main()

{

printf(“Maximum Value: %d\n”, MAX_VALUE);

printf(“Value of PI: %.2f\n”, PI);

printf(“Grade: %c\n”, GRADE);

printf(“Message: %s\n”, MESSAGE);

return 0;

}

Usage of const Keyword:

  • The const keyword is used to declare constants in C.
  • It specifies that the value of a variable cannot be changed after initialization.
  • Constants declared with const are read-only and cannot be modified during program execution.

const int MAX_VALUE = 100;

const float PI = 3.14159;

const char* MESSAGE = “Hello, World!”;

Benefits of Constants:

  1. Readability: Constants improve code readability by providing meaningful names for values used in the program.
  2. Safety: Constants prevent accidental modification of values, reducing the risk of errors in the program.
  3. Optimization: Constants may help the compiler optimize code by replacing constant expressions with their computed values.

Constants are an essential aspect of C programming, aiding in the creation of robust and maintainable code. They provide a way to represent fixed values in a program, enhancing clarity and ensuring correctness.