Skip to main content
C Programming Basics
CHAPTER 04 Beginner

Variables and Data Types

Updated: May 17, 2026
5 min read

# 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 const and #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;

c
123
int age = 25;       // 'age' is a variable holding an integer
float price = 19.99; // 'price' is a variable holding a decimal
char grade = 'A';    // 'grade' is a variable holding a character

4. Data Types in C

C is a statically typed language, meaning you must declare the data type before using a variable.
Data TypeDescriptionSize (typical)Format SpecifierExample
intInteger (whole number)4 bytes%d or %i10, -5
floatSingle-precision decimal4 bytes%f3.14f
doubleDouble-precision decimal8 bytes%lf19.99999
charSingle character1 byte%c'A'
_BoolBoolean (C99)1 byte%d (1 or 0)1 (true)

5. Format Specifiers and Printing Variables

To print variables using printf(), you must use format specifiers as placeholders in the text.
c
12345678910111213
#include <stdio.h>

int main() {
    int score = 100;
    float temp = 98.6;
    char initial = &#039;J&#039;;

    printf("Score: %d\n", score);
    printf("Temperature: %.2f\n", temp); // %.2f limits to 2 decimal places
    printf("Initial: %c\n", initial);

    return 0;
}

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 (playerscore instead of ps).

7. Constants

Constants are variables whose values cannot be changed after initialization.

Method 1: Using const keyword

c
12
const float PI = 3.14159;
// PI = 3.14; // ERROR: assignment of read-only variable

Method 2: Using #define (Macro)

c
12
#define MAX_USERS 100
// Replaces every instance of MAX_USERS with 100 before compiling.

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.

c
12
int num = 10;
float decimal = num; // Automatically becomes 10.000000

Explicit Conversion (Manual): You force the conversion by placing the target type in parentheses.

c
12
float price = 9.99;
int rounded = (int) price; // Forces conversion. 'rounded' becomes 9 (truncates decimal)

9. Memory-Level Explanation

When you declare int age = 25;:
  1. 1. The compiler sees int and allocates 4 contiguous bytes of memory on the Stack.
  1. 2. It notes that the memory address (e.g., 0x7ffee123) is labeled age.
  1. 3. It converts 25 to binary (00011001) and stores it in those 4 bytes.
12345
Memory Stack:
Address      Value      Variable
0x1000       00000000   
0x1004       00011001   <- age (4 bytes)
0x1008       'A'        <- initial (1 byte)

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 %d will result in garbage values or 0.
  • Forgetting f on floats: float pi = 3.14; technically creates a double. Use float pi = 3.14f;.

11. Exercises

  1. 1. Declare variables to store your age, height in meters (float), and blood type letter. Print them out in a single sentence.
  1. 2. Create a constant for the speed of light (299792458) and print it.
  1. 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

Question 1

Which data type is used for storing single characters?

Question 2

What is the correct format specifier for a float?

Question 3

Which of the following is a valid variable name?

Question 4

What is the size of an int on most modern 64-bit systems?

Question 5

How do you define a constant using macros?

Question 6

What happens in int x = (int) 4.99;?

Question 7

Which format specifier prints a double?

Question 8

Single quotes are used for:

Question 9

Which keyword prevents a variable from being modified?

Question 10

What prints: printf("%d", 10.5);?

13. Interview Questions

  • Q: What is the difference between #define and const?
  • 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 like int, float, or char. Format specifiers act as placeholders for printf(). Constants protect data from changing, and type casting safely converts data between types.

15. Next Chapter Recommendation

Now that you can store data, you need to manipulate it. In Chapter 5: Operators in C, we'll cover arithmetic, relational, logical, and bitwise operators.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·