Variables and Data Types
# CHAPTER 4
Variables and Data Types
1. Introduction
A computer's memory is like a massive warehouse of empty boxes. To store information (like a player's score or a user's name), you need to claim a box, give it a label, and specify what kind of item goes inside. In C, the label is the variable, and the kind of item is the data type.2. Learning Objectives
By the end of this chapter, you will be able to:- Declare and initialize variables.
-
Understand primitive data types (
int,float,char,double).
-
Use format specifiers in
printf().
-
Create constants using
constand#define.
- Understand type conversion (casting).
3. What is a Variable?
A variable is a name given to a memory location where data is stored. Its value can change (vary) during program execution.Syntax: dataType variableName = value;
4. Data Types in C
C is a statically typed language, meaning you must declare the data type before using a variable.| Data Type | Description | Size (typical) | Format Specifier | Example |
|---|---|---|---|---|
int | Integer (whole number) | 4 bytes | %d or %i | 10, -5 |
float | Single-precision decimal | 4 bytes | %f | 3.14f |
double | Double-precision decimal | 8 bytes | %lf | 19.99999 |
char | Single character | 1 byte | %c | 'A' |
_Bool | Boolean (C99) | 1 byte | %d (1 or 0) | 1 (true) |
5. Format Specifiers and Printing Variables
To print variables usingprintf(), you must use format specifiers as placeholders in the text.
6. Variable Naming Rules
-
Must begin with a letter or underscore (
).
- Can contain letters, numbers, and underscores.
-
CANNOT contain spaces or special characters (
@,#, etc.).
-
CANNOT be a C keyword (like
int,return,if).
-
Best Practice: Use descriptive names (
playerscoreinstead ofps).
7. Constants
Constants are variables whose values cannot be changed after initialization.Method 1: Using const keyword
Method 2: Using #define (Macro)
8. Type Conversion (Type Casting)
Sometimes you need to convert data from one type to another.Implicit Conversion (Automatic): Smaller types are safely promoted to larger types.
Explicit Conversion (Manual): You force the conversion by placing the target type in parentheses.
9. Memory-Level Explanation
When you declareint age = 25;:
-
1.
The compiler sees
intand allocates 4 contiguous bytes of memory on the Stack.
-
2.
It notes that the memory address (e.g.,
0x7ffee123) is labeledage.
-
3.
It converts
25to binary (00011001) and stores it in those 4 bytes.
10. Common Mistakes
-
Using single quotes for strings:
'Hello'is wrong. Single quotes are ONLY for single characters ('H'). Use double quotes for strings.
-
Mismatched format specifiers: Printing a float using
%dwill result in garbage values or0.
-
Forgetting
fon floats:float pi = 3.14;technically creates a double. Usefloat pi = 3.14f;.
11. Exercises
- 1. Declare variables to store your age, height in meters (float), and blood type letter. Print them out in a single sentence.
- 2. Create a constant for the speed of light (299792458) and print it.
- 3. Write a program that divides 5 by 2 using integers, then fix it using type casting to get the correct 2.5 answer.
12. MCQ Quiz with Answers
Which data type is used for storing single characters?
What is the correct format specifier for a float?
Which of the following is a valid variable name?
What is the size of an int on most modern 64-bit systems?
How do you define a constant using macros?
What happens in int x = (int) 4.99;?
Which format specifier prints a double?
Single quotes are used for:
Which keyword prevents a variable from being modified?
What prints: printf("%d", 10.5);?
13. Interview Questions
-
Q: What is the difference between
#defineandconst?
- Q: Explain implicit vs. explicit type casting.
- Q: What happens if you assign a larger value to an integer than it can hold? (Integer overflow).
14. Summary
Variables store data in memory, requiring a specific data type likeint, float, or char. Format specifiers act as placeholders for printf(). Constants protect data from changing, and type casting safely converts data between types.