Type of C Contants- part 6

Type of C Contants- part 6

play-rounded-fill play-rounded-outline play-sharp-fill play-sharp-outline
pause-sharp-outline pause-sharp-fill pause-rounded-outline pause-rounded-fill
00:00

Types of Constants in C (Part 6)

In the C programming language, constants are fixed values that do not change during program execution. Constants can be classified into different types based on their data type and usage.



1. Integer Constants

Integer constants are whole numbers (without decimal points). They can be:

  • Decimal (Base 10): 10, 25, -100
  • Octal (Base 8, starts with 0): 012 (equivalent to 10 in decimal)
  • Hexadecimal (Base 16, starts with 0x): 0xA (equivalent to 10 in decimal)

2. Floating-Point (Real) Constants

Floating-point constants represent decimal numbers or numbers in exponential form.

  • Example: 3.14, -0.001, 1.2E3 (equivalent to 1.2 × 10³ = 1200)

3. Character Constants

A single character enclosed in single quotes (‘ ‘).

  • Example: 'A', '5', '@'
  • Each character constant has an ASCII value (e.g., 'A' = 65).

4. String Constants

A sequence of characters enclosed in double quotes (” “).

  • Example: "Hello", "C Programming"
  • Stored as an array of characters with an implicit \0 (null terminator).

5. Symbolic Constants

Defined using the #define preprocessor directive.

  • Example:
    c
    #define PI 3.14159
    #define MAX_SIZE 100
  • These are replaced by their values during compilation.

6. Enumeration Constants

Created using enum for defining a set of named integer constants.

  • Example:
    c
    enum Color {RED, GREEN, BLUE};

Would you like a detailed explanation of any specific

CHAPTER -4 CONSTANTS ,VARIABLES AND DATA TYPES

Type of C Contants- part 6

Unit-I Chapter 2 C Tokens: Character set, Identifiers, …

Here’s a structured and easy-to-understand explanation of:

💻 C Programming – Types of Constants (Part 6)

Suitable for BCA, B.Tech, Diploma, or beginner programmers.


📘 What are Constants in C?

A constant is a fixed value that does not change during the execution of a program.

Constants are used in programs to make them more readable and maintainable.


🔹 Types of Constants in C

C supports several types of constants, broadly categorized as:

1️⃣ Integer Constants

  • Whole numbers without decimal.
  • Example: 10, -5, 0, 2000

Types:

  • Decimal (Base 10): 25
  • Octal (Base 8): starts with 0, e.g. 037
  • Hexadecimal (Base 16): starts with 0x, e.g. 0x1F

2️⃣ Floating-point Constants

  • Numbers with decimal point or in exponential (scientific) notation.
  • Example: 3.14, -0.001, 2.5e3

3️⃣ Character Constants

  • A single character enclosed in single quotes ' '.
  • Example: 'A', 'z', '5'

Each character constant has an ASCII value internally.

char ch = 'A'; // ASCII value = 65

4️⃣ String Constants

  • A sequence of characters enclosed in double quotes " ".
  • Example: "Hello", "1234", "C Programming"
char str[] = "Hello";

5️⃣ Enumeration Constants (enum)

  • Used to define named integer constants.
  • Improves readability of code.
enum Days { MON, TUE, WED, THU, FRI };

Here, MON is 0, TUE is 1, etc.


6️⃣ Defined Constants (#define)

  • Use the preprocessor directive #define to create constants.
#define PI 3.14
#define MAX 100

Once defined, PI will always be replaced with 3.14.


🧠 Summary Table

Constant Type Example
Integer 100, -23
Floating-point 3.14, 2.0e3
Character 'A', '7'
String "Hello"
Enumeration enum Colors {RED, BLUE}
Defined (#define) #define PI 3.14

✅ Tips

  • Constants make code easier to read and maintain.
  • Use const keyword to declare variables as constants:
const int age = 18; // age cannot be changed

Would you like:

  • 🧾 Practice questions on constants?
  • 📄 A PDF revision sheet?
  • 🧪 A quiz to test your understanding?

Let me know, and I’ll provide it!

Type of C Contants- part 6

Programming in C with Numerical Methods Unit I Chapter II …



Leave a Reply

Your email address will not be published. Required fields are marked *

error: