Saturday, January 4, 2025
HomeQ&AConstants in C

Constants in C

Constants in C are fixed values that do not change during program execution. They can be of various types like integer, floating-point, or character.

Declaring Constants:

  1. Using #define (Preprocessor Directive):
    c
    #define PI 3.14
  2. Using const Keyword:
    c
    const int MAX = 100;

Types of Constants:

  1. Integer Constants: 10, 0xA (Hexadecimal).
  2. Floating-point Constants: 3.14, 2.718.
  3. Character Constants: 'A', '@'.
  4. String Constants: "Hello".

Example:

c
#include <stdio.h>
#define PI 3.14

int main() {
const int MAX = 100;
printf("PI: %f, MAX: %d\n", PI, MAX);
return 0;
}

Constants ensure data integrity by preventing unintended modifications.

RELATED ARTICLES

Leave a Reply

- Advertisment -

Most Popular

Recent Comments