Variables and constants in C
Variables and Constants in C
📌 Variables
A variable is a container (storage location) used to store data.
-
Each variable has a unique name (identifier)
-
The name represents a memory location
-
The value stored in a variable can change
Example
-
playerScore→ variable name -
int→ data type -
95→ stored value
👉 In C, variables must be declared before use.
📌 Rules for Naming Variables
-
Can contain:
-
Letters (A–Z, a–z)
-
Digits (0–9)
-
Underscore
_
-
-
First character must be:
-
Letter OR underscore
_ -
Starting with
_is discouraged (may conflict with system names)
-
-
Length:
-
No strict limit
-
Most compilers check only the first 31 characters
-
-
C is strongly typed
-
Variable type cannot be changed after declaration
-
Constants / Literals in C
A constant is a value that cannot be changed during program execution.
Examples
Constants can also be defined using identifiers:
Here, PI always represents 3.14.
📌 Types of Constants in C
1️⃣ Integer constants
2️⃣ Floating-point constants
3️⃣ Character constants
4️⃣ String constants
5️⃣ Enumeration constants
1️⃣ Integer Constants
Integer constants are whole numbers without fractional or exponent parts.
Types
-
Decimal (base 10) →
0, -9, 22 -
Octal (base 8) → starts with
0→021, 077 -
Hexadecimal (base 16) → starts with
0xor0X→0x7f, 0x2a
Special Integer Forms
-
Unsigned integer → append
U -
Long integer → append
L -
Unsigned long → append
UL
Rules for Numeric Constants
-
No commas or spaces allowed
-
May be preceded by
- -
Must be within compiler limits
2️⃣ Floating-Point Constants
A floating constant:
-
Contains a decimal OR exponent OR both
-
Always base-10
Examples
Scientific Notation
Example representations of 3 × 10⁵:
Precision Types
-
Default → double precision (8 bytes)
-
Single precision → add
F -
Long floating → add
L
3️⃣ Character Constants
A character constant:
-
Single character
-
Enclosed in single quotes
Examples
Characters use ASCII encoding:
| Character | ASCII |
|---|---|
| 'A' | 65 |
| 'a' | 97 |
| '0' | 48 |
Escape Sequences in C
Escape sequences represent special characters.
| Sequence | Meaning |
|---|---|
\b | Backspace |
\f | Form feed |
\n | New line |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\\ | Backslash |
\' | Single quote |
\" | Double quote |
\? | Question mark |
\0 | Null character |
Special Note: \0
-
Represents null character
-
Marks the end of a string
-
Different from
'0'
Octal Escape
Example:
Hex Escape
Example:
4️⃣ String Constants
A string constant:
-
Sequence of characters in double quotes
Examples
Example Output
Prints:
Important Notes
-
Compiler automatically adds
\0at end of string -
'A'≠"A"
| Type | Meaning |
|---|---|
'A' | single character |
"A" | string with 2 chars → 'A' + \0 |
5️⃣ Enumeration Constants
Defined using keyword enum
Example
Values assigned automatically:
Custom Values
👉 Multiple names can share same value
Mixed Assignment
Output:
Rules
-
Must be integer constants
-
Must be unique within scope
Enum vs Macro
Macros:
Better with enum:
Advantages:
-
Follows scope rules
-
Automatic value assignment
Symbolic Constants (#define)
A symbolic constant is a name representing a fixed value.
Syntax
-
Written at beginning of program
-
No semicolon at end
Example
Usage:
Compiler replaces it with:
Advantages of Symbolic Constants
-
Improves readability
-
Easy to modify values
-
Helps maintain large programs
-
Defined using C preprocessor

Comments
Post a Comment