C Constants

C Constants

In the C programming language, constants are fixed values that cannot be changed during the execution of a program. They are used to represent fixed values such as numbers, characters, and strings. Constants are declared using the const keyword and can be of different types, including integer constants, floating-point constants, character constants, and string constants.

Integer Constants

Integer constants are used to represent whole numbers. They can be written in decimal, octal, or hexadecimal format. Here are some examples:

  • Decimal: int x = 10;
  • Octal: int y = 012; (the leading zero indicates an octal value)
  • Hexadecimal: int z = 0x1F; (the leading 0x indicates a hexadecimal value)

Floating-Point Constants

Floating-point constants are used to represent decimal numbers with fractional parts. They can be written in decimal or exponential format. Here are some examples:

  • Decimal: float a = 3.14;
  • Exponential: float b = 1.23e-5; (the e indicates the exponent)

Character Constants

Character constants are used to represent individual characters. They are enclosed in single quotes. Here are some examples:

  • char c = 'A';
  • char d = '5';
  • char e = '$';

String Constants

String constants are used to represent sequences of characters. They are enclosed in double quotes. Here are some examples:

  • char str1[] = "Hello";
  • char str2[] = "World";

Conclusion

In C, constants are fixed values that cannot be changed during program execution. They are declared using the const keyword and can be of different types, including integer constants, floating-point constants, character constants, and string constants.