C Variables and Data Types — int, float, char, double
Learning Objectives
- Declare and initialize variables in C
- Choose the right fundamental data type for a value
- Understand how much memory common types usually occupy
- Follow safe naming and initialization habits
What Is a Variable?
A variable is a named storage location in memory that holds a value. You declare a variable by specifying its type and name, then you can assign values and use them in calculations. In C, every variable has a fixed type, and that type determines what kind of data it can store and how it behaves.
int age;
float price;
char grade;
double pi;
These four lines declare four variables. Nothing is stored yet until you assign a value. Declaration tells the compiler how much space to reserve and how to interpret the bits.
Core Data Types
C provides several fundamental types that cover most everyday needs. Integers store whole numbers, floating-point types store decimal numbers, and char stores a single character represented by a small integer code.
| Type | Typical Use | Example Values |
|---|---|---|
| int | Whole numbers | 0, 42, minus 7 |
| float | Decimal numbers | 3.14, 0.5 |
| double | High precision decimals | 3.141592653589793 |
| char | Single character | A, 9, question mark |
The keyword unsigned can modify integer types to remove negative values and expand the positive range. Most beginners start with plain int for whole numbers.
Declaring and Initializing
You can assign a value at declaration time. This is called initialization, and it is a best practice because using an uninitialized variable leads to unpredictable results.
int score = 100;
float temperature = 98.6;
char initial = 'J';
double balance = 1500.75;
score = score + 5;
printf("%d\n", score);
Notice that character values use single quotes, while strings use double quotes. Integer literals need no special decoration. Floating literals include a decimal point so the compiler treats them as real numbers.
You may declare multiple variables of the same type on one line by separating them with commas, but many style guides prefer one declaration per line for clarity as programs grow.
Format Specifiers for Printing
When you print variables with printf, you match each value with a format specifier. Getting the specifier wrong causes strange output or undefined behavior.
| Specifier | Type |
|---|---|
| %d | int |
| %f | float and double |
| %c | char |
| %ld | long integer |
| %s | String, covered in a later lesson |
int year = 2026;
float gpa = 3.75;
char level = 'A';
printf("Year: %d\n", year);
printf("GPA: %f\n", gpa);
printf("Level: %c\n", level);
Choosing the Right Size
Exact byte sizes can vary by compiler and platform, but common modern systems use the following pattern. You can check the size of a type on your machine with the sizeof operator, which returns the number of bytes.
printf("int size: %zu bytes\n", sizeof(int));
printf("double size: %zu bytes\n", sizeof(double));
Use int for typical counters and indexes, float when memory is tight and precision is moderate, double for general scientific math, and char for characters or small flags. When you need guaranteed widths, the standard headers provide fixed-size types such as int32_t.
⚠️Watch for Overflow
An int has a largest value. If a calculation exceeds that limit, the number wraps around and silently corrupts your data. Choose a larger type when values can grow beyond normal ranges.
Naming Rules
Variable names must start with a letter or underscore, and they may contain letters, digits, and underscores. Names are case-sensitive, so total and Total are different variables. Keywords such as int and return cannot be used as names.
Good names describe purpose. Prefer count over x, and studentName over tmp. Consistent naming makes debugging dramatically easier for you and anyone who reads your code later.
Key Takeaways
💡
- Variables are named memory locations with a fixed type
- int stores whole numbers, float and double store decimals, char stores characters
- Always initialize variables before reading them
- Match printf format specifiers to each variable type
- Use descriptive names and check sizes with sizeof when portability matters
Practice Questions
- What is the difference between declaring a variable and initializing it?
- Which format specifier would you use to print an int and a char?
- Why should variable names describe their purpose instead of using single letters?
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type — Always start by categorizing the problem
- Incorrect formula application — Verify the formula applies to your specific case
- Unit errors — Always check units are consistent throughout
- Sign errors — Pay careful attention to positive and negative values
- Rounding too early — Keep full precision until the final answer
- Forgetting constraints — Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic — Build on the foundation laid here
- Next Topic — See how this concept extends to more advanced material
- Practice Problems — Additional exercises with solutions
- Video Explanation — Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions