Constants in C

 In C programming, constants are fixed values that do not change during the execution of a program. They are used to represent values that remain constant throughout the program's lifecycle. Constants are categorized into several types, including numeric constants, character constants, string constants, and symbolic constants. Here's an overview of these types of constants in C.

  1. Numeric Constants:

    • Numeric constants represent fixed numerical values and can be of various types, such as integers, floating-point numbers, or characters.
    • Integer Constants: Whole numbers without decimal points.
      • Example: 42, -10, 0, 255
    • Floating-Point Constants: Numbers with decimal points.
      • Example: 3.14, -0.001, 1.0e5 (scientific notation)
    • Character Constants: Single characters enclosed in single quotes ('').
      • Example: 'A', '5', '?'
  2. Character Constants:

    • Character constants represent individual characters and are enclosed in single quotes ('').
    • Example: 'A', 'b', '1'
  3. String Constants:

    • String constants represent sequences of characters and are enclosed in double quotes ("").
    • Example: "Hello, World!", "C Programming"
  4. Symbolic Constants (Named Constants):

    • Symbolic constants are user-defined constants with names and values that remain fixed throughout the program.
    • They are typically defined using the #define preprocessor directive.
    • Example:
      c
      #define PI 3.14159265359 #define MAX_LENGTH 100
  5. Enumeration Constants:

    • Enumeration constants (enums) are user-defined data types that consist of a set of named integer constants.
    • They are defined using the enum keyword.
    • Example:
      c
      enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
  6. Hexadecimal Constants:

    • Numeric constants can also be represented in hexadecimal notation by prefixing them with 0x or 0X.
    • Example: 0x1A, 0xFF, 0x10
  7. Octal Constants:

    • Numeric constants can be represented in octal notation by prefixing them with 0.
    • Example: 043, 077, 012
  8. Binary Constants:

    • Binary constants are represented in binary notation by prefixing them with 0b or 0B.
    • This notation is available in C99 and later standards.
    • Example: 0b1010, 0B1101

Constants play a crucial role in C programming, as they provide a way to work with fixed values, make code more readable, and facilitate easy modifications when constants need to be changed globally in a program.