Constants:
In ‘C‘ Language, a Number or Character or String of Characters is called as constant. And it can be any Data Type. Constants are also called as Literals.
Two Types of Constants:
1.Primary Constants:
Integer, Float, and Character are called as Primary Constants.
-
Integer Constants:
Integer constants are whole numbers without any fractional part.
Examples:
const int age = 25;
const int days_in_week = 7;
-
Floating-Point Constants:
Floating-point constants are numbers that have a fractional part or are written in exponential notation.
Examples:
const float pi = 3.14159;
const double gravity = 9.8;
-
Character Constants:
Character constants are single characters enclosed in single quotes.
Example:
const char grade = ‘A’;
Example of Primary Constants:
#include <stdio.h>int main(){const int height = 20;const int base = 40;float area;area = 0.5 * height * base;printf(“The Area of Traingle :%f”, area);return 0;}
2.Secondary Constants:
Array, Structures, Pointers, Enum etc., called as Secondary Constants.
-
String Constants:
String constants are sequences of characters enclosed in double quotes.
Examples:
const char *message = “Hello, world!”;
-
Enumeration Constants:
Enumeration constants are symbolic names for integer values.
Examples:
enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
const enum Days today = Wednesday;
-
Macro Constants (Preprocessor Constants):
Macro constants are defined using the #define preprocessor directive.
Example:
#define MAX_SIZE 100
-
Hexadecimal Constants:
Hexadecimal constants are expressed in base-16 notation and start with 0x.
Example:
const int hex_value = 0xA3;
-
Octal Constants:
Octal constants are expressed in base-8 notation and start with 0.
Example:
const int octal_value = 075;
Example of Secondary Constants:
#include <stdio.h>int main(){int a;int *p;a = 10;p = &a;printf(“a=%dn”, a);printf(“a=%dn”, *p); // Corrected line*p = 12;printf(“a=%dn”, a);printf(“a=%dn”, *p); // Corrected linereturn 0;}