Coding/Learn/c/c variables and data types
C Foundational Course
01.Introduction, Compilation Pipeline & Syntax
02.Variables, Primitives & Explicit Type Casting
03.Operators, Precedence & Booleans (<stdbool.h>)
04.Control Flow: If-Else, Ternary & Switch
05.Loops (For, While, Do-While) & Jump Statements
06.Contiguous Arrays & Multi-Dimensional Matrices
07.Null-Terminated Strings & <string.h>
08.User Input, scanf & fgets Buffer Safety
09.Pointers, Memory Addresses & Dereferencing
10.Functions, Prototypes & Pass-by-Pointer
11.11. Recursion & High-Speed Function Pointers
12.File Streams: Create, Write & Read (fopen/fclose)
13.Structures, Unions & typedef Aliasing
14.Memory Word Alignment & Struct Padding
15.Enums & Bitwise Manipulation (Flags & Masks)
16.Dynamic Memory: malloc, calloc, realloc & free
17.Defensive Error Handling, errno & Assertions
18.Preprocessor Macros & Storage Classes
19.Fixed-Width Types (stdint.h), Math & Time (<time.h>)
20.Capstone: High-Performance In-Memory Key-Value Engine
Course Certificate0%

Complete all lessons to earn

LearnVariables, Primitives & Explicit Type Casting
2 / 20 0%
Lesson 02 of 20
6 mins read

Variables, Primitives & Explicit Type Casting

PreviousNext Lesson
In Plain English

In C, variables occupy a fixed number of contiguous bytes in memory (char: 1 byte, int/float: 4 bytes, double: 8 bytes). Use the sizeof operator to measure memory footprints and explicit type casts (double)expr to prevent integer division truncation.

The Mental Model

"Declaring a variable in C is reserving an exact physical drawer in a metal filing cabinet sized precisely for your document type."

Deep Dive: How It Works

Primitive Sizes: char (1B, -128..127), int (4B, ~2 billion), float (4B, 6 digits), double (8B, 15 digits).

The sizeof Operator: Evaluates byte size at compile time without runtime execution overhead.

Explicit Casts: (double)total / count converts total to double before division, preserving decimal precision.

Syntax Blueprint

const int MAX_USERS = 1000;
double ratio = (double)points / games;

Use const for immutable read-only storage and prefix (type) for explicit conversions.

PointCore Rules to Remember

Point star
Contiguous Bytes: Variables occupy exact physical memory bytes without hidden object wrapper overhead.
Point star
sizeof Operator: Returns size_t byte count of any type or expression at compile time.
Point star
Integer Division Trap: 19 / 4 evaluates to 4 in C. Casting (double)19 / 4 evaluates accurately to 4.75.

Common Beginner Traps & How to Fix Them

Mistake:Dividing two integers and assigning the result to a double expecting decimal precision.

Why it happens: In int a = 5, b = 2; double c = a / b;, the division truncates to integer 2 before assignment.

How to fix: Cast at least one operand: double c = (double)a / b;.

Pro Tip: Use const on any variable that should remain immutable to allow the compiler to store it in read-only memory.

Live Interactive Example

Hit Run Code to see it live
Throughput Calculation with Casts
C11 / GCC 9.2
1
2
3
4
5
6
7
8
9
10
Output Console
Click "Run Code" to view the rendered output.
How it works: totalRequests is cast to double, resulting in double division 450.0 / 8 = 56.25.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Define Max Capacity Constant

  • PointDeclare a constant integer "MAX_CAPACITY" initialized to 1000 using the const keyword.
  • PointPrint "Max Capacity: %d\n" with MAX_CAPACITY.
1
2
3
4
5
6
7
8
9
10
11
12
13
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates