Cover Image for Constants in C
106 views

Constants in C

The constants are fixed values that do not change during the execution of a program. Constants are used to represent data that remains the same throughout the program’s execution, and they are often used for defining parameters, configuration values, and mathematical constants. C supports different types of constants, including:

  1. Integer Constants:
  • Integer constants represent whole numbers, and they can be written in decimal, octal (base 8), or hexadecimal (base 16) notation.
  • Examples:
    • Decimal: 42, -123
    • Octal: 075 (prefixed with 0)
    • Hexadecimal: 0x1A or 0X1A (prefixed with 0x or 0X)
  1. Floating-Point Constants:
  • Floating-point constants represent real numbers with decimal points. They can have a fractional part and/or an exponent part.
  • Examples: 3.14159, -0.01, 1.0, 2.5e3 (scientific notation)
  1. Character Constants:
  • Character constants represent individual characters and are enclosed in single quotes.
  • Examples: 'A', '7', '%'
  1. String Constants:
  • String constants represent sequences of characters enclosed in double quotes.
  • Example: "Hello, World!"
  1. Enumeration Constants:
  • Enumeration constants are user-defined constants created using the enum keyword. They represent a set of named integer values.
  • Example:
    c enum Days { MON, TUE, WED, THU, FRI, SAT, SUN };
  1. Symbolic Constants (Macros):
  • Symbolic constants are defined using the #define preprocessor directive. They provide a way to create named constants in your code.
  • Example:
    c #define PI 3.14159
  1. Boolean Constants:
  • While C does not have dedicated boolean constants, 0 is often used to represent “false,” and non-zero values (usually 1) represent “true.”
  1. NULL Pointer Constant:
  • The special constant NULL represents a null pointer and is often used for initializing pointers.
  • Example: int *ptr = NULL;

Constants are used in C to make programs more readable, maintainable, and flexible. They allow you to define values once and use them throughout your code, making it easier to update and modify those values in the future.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS