Constants in C

Constants in C: With Writing Rules, Examples, and Syntax

In C programming, constants are values that remain unchanged during the execution of the program. They are essential for maintaining fixed values throughout the code, enhancing readability and preventing unintended modifications.

1. Integer Constants

An integer constant is a numeric constant without any fractional or decimal part. These constants can be written in three different forms: decimal, octal, and hexadecimal.

Writing Rules:

  1. Decimal Constants:
    • Must contain digits from 0-9.
    • Must not start with a 0 (leading zero indicates an octal constant).
    • Example: 123, -345.
  2. Octal Constants:
    • Must start with a 0.
    • Must contain digits from 0-7.
    • Example: 0123 (Octal 0123 is equal to Decimal 83).
  3. Hexadecimal Constants:
    • Must start with 0x or 0X.
    • Can contain digits from 0-9 and letters A-F (or a-f).
    • Example: 0x7B (Hexadecimal 0x7B is equal to Decimal 123).

Example:

#include<stdio.h>

int main() {
    int dec = 123;         // Decimal constant
    int oct = 0123;        // Octal constant (83 in decimal)
    int hex = 0x7B;        // Hexadecimal constant (123 in decimal)
    
    printf("Decimal: %d\n", dec);
    printf("Octal: %d\n", oct);
    printf("Hexadecimal: %d\n", hex);
    
    return 0;
}
    
Output: Decimal: 123
Octal: 83
Hexadecimal: 123

2. Floating-Point Constants

A floating-point constant represents real numbers with decimal points or numbers in exponential notation.

Writing Rules:

  1. Standard Notation:
    • Must contain at least one digit before or after the decimal point.
    • Example: 3.14, -0.001.
  2. Exponential Notation:
    • Represents numbers as a base followed by an exponent (e or E).
    • Example: 3.14e2 (3.14 x 102 = 314), 1.23e-3 (1.23 x 10-3 = 0.00123).

Example:

#include<stdio.h>

int main() {
    float pi = 3.14159;         // Floating-point constant
    double exp = 1.23e3;        // Exponential notation (1.23 x 10^3 = 1230)
    
    printf("Value of pi: %.5f\n", pi);
    printf("Value of exp: %.2f\n", exp);
    
    return 0;
}
    
Output: Value of pi: 3.14159
Value of exp: 1230.00

3. Character Constants

A character constant is a single character enclosed in single quotes. It is stored as an integer corresponding to its ASCII value.

Writing Rules:

  • A character constant must be enclosed in single quotes (' ').
  • It must contain a single character or escape sequence (e.g., '\n', '\t').
  • Character constants are of type int and are stored as their ASCII values.

Example:

#include<stdio.h>

int main() {
    char letter = 'A';    // Character constant
    char newline = '\n';  // Newline escape sequence
    
    printf("Character: %c", letter);
    printf("%c", newline);  // Prints a new line
    printf("ASCII value of %c: %d\n", letter, letter);
    
    return 0;
}
    
Output: Character: A
ASCII value of A: 65

4. String Constants

A string constant is a sequence of characters enclosed in double quotes. Strings in C are stored as arrays of characters, ending with a null character (\0).

Writing Rules:

  • A string constant must be enclosed in double quotes (" ").
  • It can contain multiple characters, including escape sequences like \n, \t, etc.
  • Strings are automatically terminated with a null character (\0).

Example:

#include<stdio.h>

int main() {
    char greeting[] = "Hello, World!";  // String constant
    
    printf("%s\n", greeting);  // %s is used to print a string
    
    return 0;
}
    
Output: Hello, World!

5. Symbolic Constants

A symbolic constant is a constant value assigned to a name using the #define directive or the const keyword. These constants are defined at the beginning of the program and cannot be modified.

Writing Rules:

  • Using #define:
    • The syntax is: #define CONSTANT_NAME value.
    • Constants defined using #define do not consume memory.
    • Example: #define PI 3.14159.
  • Using const:
    • The syntax is: const data_type identifier = value;.
    • Constants declared using const are of the type specified and are stored in memory.
    • Example: const float PI = 3.14159;.

Example (Using #define):

#include<stdio.h>

#define PI 3.14159  // Defining a symbolic constant

int main() {
    float radius = 5.0;
    float area = PI * radius * radius;  // Using symbolic constant
    
    printf("Area of the circle: %.2f\n", area);
    
    return 0;
}
    

Example (Using const):

#include<stdio.h>

int main() {
    const float PI = 3.14159;  // Defining a constant using 'const'
    float radius = 5.0;
    float area = PI * radius * radius;
    
    printf("Area of the circle: %.2f\n", area);
    
    return 0;
}
    

Output (Both Examples):

Area of the circle: 78.54

6. Enumeration Constants

Enumeration constants are user-defined constants that assign names to integral constants. They are defined using the enum keyword.

Writing Rules:

  • The syntax is: enum {CONST1, CONST2, CONST3, ...};.
  • The first constant is assigned the value 0, and subsequent constants are assigned incrementally.
  • You can also explicitly assign values to enumeration constants.

Example:

#include<stdio.h>

enum Days {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};

int main() {
    enum Days today;
    today = WEDNESDAY;
    
    printf("Day %d of the week\n", today);  // Output: 3 (WEDNESDAY is the 4th day, starting from 0)
    
    return 0;
}
    

Output:

Day 3 of the week

Summary of Constants in C

  • Integer Constants: Whole numbers without decimal points.
    Example: int x = 10;, int y = 0xA;.
  • Floating-Point Constants: Numbers with decimals or in exponential notation.
    Example: float pi = 3.14;, double exp = 1.23e4;.
  • Character Constants: A single character enclosed in single quotes.
    Example: char ch = 'A';.
  • String Constants: A sequence of characters enclosed in double quotes.
    Example: char str[] = "Hello";.
  • Symbolic Constants: Defined using #define or const for immutability.
    Example: #define MAX 100, const float PI = 3.14;.
  • Enumeration Constants: User-defined integral constants using the enum keyword.
    Example: enum Days {SUNDAY, MONDAY, TUESDAY};.

Constants in C allow for better control, clarity, and prevention of unintended changes to values. These immutable values help maintain consistency and ensure that data remains fixed throughout the program execution.