Constants, Variables & Statements in C
The Essential Building Blocks of C Programming
In C programming, everything revolves around data and how we use it. To work with data, we use constants, variables, and statements. These are the building blocks of every program.
Let's explore these concepts in a simple and clear way.
What is a Constant in C?
A constant is a value that never changes during program execution. Think of it like your date of birth—it remains fixed forever!
Why Use Constants?
• Keep values fixed
• Improve code readability
• Avoid accidental changes
Types of Constants
1. Integer Constants
10, -25, 1000
2. Floating-point
3.14, -0.01
3. Character
'A', '%'
4. String
"Hello", "C Language"
Declaring Constants in C
1. Using #define
#define PI 3.14
Compiler replaces PI with 3.14 everywhere
2. Using const
const int speedLimit = 60;
Cannot be changed after declaration
What is a Variable?
A variable is a named memory location where you can store and change data during program execution.
Why Use Variables?
• Store user input
• Perform calculations
• Make programs dynamic
Naming Rules
• Start with letter or _
• No spaces/special chars
• Avoid keywords
• Examples: age, total_marks
Declaring Variables in C
Declaration Only
int age;
float price;
char grade;
float price;
char grade;
With Initialization
int age = 20;
float price = 99.5;
float price = 99.5;
What are Statements in C?
A statement is an instruction that tells the program what to do. Think of them as sentences in a story.
Types of Statements
1. Declaration
int x;
2. Assignment
x = 10;
3. I/O
scanf("%d", &x);
printf("%d", x);
4. Control
if, else, while, for
Constant vs Variable
Feature | Constant | Variable |
---|---|---|
Value | Never changes | Can change |
Declaration | #define or const | With data type |
When fixed | Compile time | Can change at runtime |
Code Example
#include <stdio.h>
#define PI 3.14
int main() {
const int daysInWeek = 7;
int radius = 5;
float area;
area = PI * radius * radius;
printf("Area: %.2f\n", area);
return 0;
}
#define PI 3.14
int main() {
const int daysInWeek = 7;
int radius = 5;
float area;
area = PI * radius * radius;
printf("Area: %.2f\n", area);
return 0;
}
Elements used:
• Constants: PI, daysInWeek
• Variables: radius, area
• Statements: declaration, assignment, printf
Importance of Proper Declarations
• Saves memory
• Makes code readable
• Avoids errors
• Creates efficient programs
Common Mistakes to Avoid
• Changing constant values
• Using undeclared variables
• Misspelling variable names
• Forgetting semicolons
Summary
• Constants: Fixed values that never change
• Variables: Storage that holds changing values
• Statements: Commands that make programs work
Master these elements to build powerful C programs!
FAQs
Q1. Can I use same name for constant and variable?
No, it can confuse the compiler and readers.
Q2. What happens if I change a constant?
The compiler will give an error.
Q3. Why do statements end with semicolons?
It tells the compiler the instruction is complete.
Q4. Can I declare multiple variables at once?
Yes: int a, b, c;
Q5. Must variables be declared before use?
Yes, in C every variable must be declared first.
0 Comments
If you have any doubts, Please let me know