C – Language

C Variables

  1. What is a Variable?
  • A variable is a container (memory location) used to store data values.
  • Each variable has:
    • A type → defines the kind of data stored
    • A name → used to identify it
    • A value → the actual data stored in it

👉 Think of a variable as a box that holds a value.

  1. Types of Variables in C

Data Type

Meaning

Example

int

Stores integers (whole numbers) without decimals

int age = 23;

float

Stores floating-point numbers (numbers with decimals)

float price = 19.99;

char

Stores single characters (enclosed in single quotes)

char grade = ‘A’;

 

  1. Declaring (Creating) Variables

To create a variable, we must:

  1. Specify the type
  2. Give it a name
  3. (Optionally) Assign a value

Syntax:

type variableName = value;

Example:

int myNum = 15;

✅ Here:

  • int → data type
  • myNum → variable name
  • 15 → value

 

  1. Declaring Without Assigning

You can declare a variable first and assign a value later.

Example:

int myNum;     // Declaration

myNum = 15;    // Assignment 5. Outputting Variables

Normally, you can print text with:

printf(“Hello World!”);

But to print variable values, C requires format specifiers (like %d, %f, %c).

If you try this:

int myNum = 15;

printf(myNum);  // ❌ Nothing happens

It won’t work because C doesn’t automatically know what type of data you’re printing.

You’ll learn how to print variables in the next chapter using format specifiers.

🧩 Quick Summary

Concept

Description

Variable

A container that stores data

Syntax

type variableName = value;

Example

int age = 25;

int

Whole numbers

float

Decimal numbers

char

Single character

Output

Use printf() + format specifiers