C Variables and Constants 📦
🎯 Overview
Variables and constants are fundamental building blocks of C programs. Variables are used to store data that can change, while constants represent data that remains unchanged during program execution. Understanding how to use them is an essential foundation for learning C.
📝 What are Variables?
Variable Concept
Variables are named memory locations for storing data. You can think of a variable as a labeled box used to hold different types of data.
Three Elements of Variables
- Variable Name: Identifier for referencing the variable
- Data Type: Determines what kind of data the variable can store
- Variable Value: The actual data stored in the variable
Variable Declaration
Variable Initialization
Variable Naming Rules
✅ Allowed Naming Rules:
- Composed of letters, digits, and underscores
- Must start with a letter or underscore
- Case-sensitive (
ageandAgeare different variables)
❌ Not Allowed Names:
Variable Naming Suggestions
🔢 Variable Scope
Local Variables
Variables declared inside a function or code block are only valid within that scope.
Global Variables
Variables declared outside all functions can be accessed throughout the entire program.
Static Variables
Declared with the static keyword, static variables retain their values between function calls.
💎 What are Constants?
Constants are values that cannot be changed during program execution. Using constants improves code readability and maintainability.
1️⃣ Literal Constants
Literals are fixed values written directly in code.
Integer Literals
Floating-point Literals
Character Literals
String Literals
Common Escape Characters
2️⃣ Symbolic Constants (Macro Definitions)
Constants defined using the #define preprocessor directive.
Basic Usage
Macro Characteristics
✅ Advantages:
- Replaced before compilation, doesn't use memory
- Can define any type of constant
- Can be used for conditional compilation
❌ Disadvantages:
- No type checking
- Difficult to debug (replaced during preprocessing)
- May produce unexpected side effects
Macro Functions
Macro Function Pitfalls
3️⃣ const Constants
Constants declared with the const keyword have type safety.
Basic Usage
const Pointers
const vs #define Differences
4️⃣ Enumerated Constants
Enumerations (enum) are used to define a set of related integer constants.
Basic Usage
Specifying Enum Values
Practical Application of Enums
🎯 Practical Examples
Example 1: Calculate Circle Area and Circumference
Example 2: Temperature Conversion Program
Example 3: Student Grade Management
📚 Best Practices
1. Variable Naming Suggestions
2. Constant Usage Suggestions
3. Choose Appropriate Constant Types
4. Initialize Variables
💡 Common Errors
Error 1: Modifying Constants
Error 2: Missing Parentheses in Macro Definitions
Error 3: Confusing Variable Scope
📖 Summary
Variable Points
- Variables store data that can change
- Must be declared before use
- Follow scope rules
- Use meaningful names
Constant Points
- Literals: Values written directly in code
- #define: Preprocessor macro, no type checking
- const: Type-safe constants, recommended
- enum: Define sets of related integer constants
Selection Recommendations
Next: Learn C Operators to understand how to perform various operations on variables and constants.