Master Variables In C

Master the fundamental building blocks of C programming. Learn how variables store data, manage memory addresses, and power your algorithms.

8 PRIMARY TYPES
4 SCOPES
POSSIBILITIES
100% CONTROL

What is a Variable?

A variable is a named location in your computer's memory that stores a value. Think of it like a labeled box where you can put data.

The Name

The label you give the variable (e.g., score, age). Must start with a letter or underscore.

The Type

Defines what kind of data fits in the box (e.g., integer, character) and how much memory it needs.

The Address

The physical location in RAM (e.g., 0x7FF...) where the binary data is actually stored.

Data Types Reference

C provides fundamental types. Choosing the right one is crucial for memory efficiency.

int

Integer

Stores whole numbers without decimals.

  • Size: Usually 4 bytes
  • Range: -2.1B to +2.1B
int count = 42;
float

Floating Point

Stores decimal numbers with single precision.

  • Size: 4 bytes
  • Precision: 6-7 decimal digits
float pi = 3.14f;
double

Double Precision

Higher precision decimal numbers.

  • Size: 8 bytes
  • Precision: 15 decimal digits
double atom = 1.5e-10;
char

Character

Stores a single character or ASCII value.

  • Size: 1 byte
  • Range: -128 to 127
char grade = 'A';
unsigned

Unsigned Modifier

Only positive numbers. Doubles positive range.

  • Ex: unsigned int
  • Range: 0 to 4.2 Billion
unsigned int id = 100;
void

Void Type

Represents "no type" or "nothing".

  • Used for function returns
  • Used for generic pointers
void func();

Declaration & Initialization

How to bring variables into existence. Always initialize to avoid garbage values.

decl.c
// 1. Basic Declaration (Contains Garbage Value) int score; // 2. Declaration with Initialization (Recommended) int age = 25; float price = 19.99; char initial = 'J'; // 3. Multiple Declaration int x = 10, y = 20, z = 30; // 4. Constants (Read-only) const double PI = 3.14159; const int MAX_USERS = 100;
Naming Rules: Variable names must start with a letter or underscore. They cannot contain spaces or special symbols like @, #, $. They are case-sensitive (ageAge).

Memory Architecture

Understanding the physical storage. Every variable has an address in RAM.

Visualizing: int age = 25;

ADDRESS (&age)
0x7FF...A8
NAME
age
VALUE
25
memory.c
#include <stdio.h> int main() { int age = 25; // Use & to get the address printf("Value: %d\n", age); printf("Address: %p\n", &age); // Use sizeof to check memory usage printf("Size: %lu bytes\n", sizeof(int)); return 0; }

Variable Scope

Scope determines where a variable is visible and how long it lives.

Scope Type Declaration Visibility Lifetime
Local Inside function Only that function Function execution
Global Outside all Entire Program Program execution
Static With 'static' kw Only that function Persists between calls
Parameter Func arguments Only that function Function execution

Best Practices

Initialize Always

Uninitialized variables contain random garbage values. Always set a default like int x = 0;.

Descriptive Names

Use studentAge instead of sa or x. Use camelCase for variables and CAPS for constants.

Limit Scope

Avoid global variables. They make debugging hard. Keep variables local to where they are used.

Common Mistakes

Wrong

int x; printf("%d", x); // Garbage Output

Correct

int x = 0; printf("%d", x); // Prints 0

Wrong

const int MAX = 10; MAX = 20; // Error: Read-only

Full Example

Putting it all together: Types, Initialization, Printing, and Logic.

main.c
#include <stdio.h> int main() { // 1. Variable Declarations int id = 101; float gpa = 3.85; char grade = 'A'; unsigned int fees = 5000; // 2. Displaying Values printf("Student ID: %d\n", id); printf("GPA: %.2f\n", gpa); printf("Grade: %c\n", grade); printf("Fees: $%u\n", fees); // 3. Modifying Variables gpa = 4.0; printf("Updated GPA: %.2f\n", gpa); return 0; }