jypi
  • Explore
ChatWays to LearnMind mapAbout

jypi

  • About Us
  • Our Mission
  • Team
  • Careers

Resources

  • Ways to Learn
  • Mind map
  • Blog
  • Help Center
  • Community Guidelines
  • Contributor Guide

Legal

  • Terms of Service
  • Privacy Policy
  • Cookie Policy
  • Content Policy

Connect

  • Twitter
  • Discord
  • Instagram
  • Contact Us
jypi

© 2026 jypi. All rights reserved.

CS50 - Introduction to Computer Science
Chapters

1Computational Thinking and Foundations

2C Language Basics

Compilers, Linkers, and MakeProgram Structure and mainPrimitive Types and sizeofVariables and ScopeOperators and ExpressionsConditionals and switchLoops: for, while, do-whileFunctions and PrototypesHeaders and the PreprocessorStandard I/O with stdio.hFormatting with printfUser Input and scanfCode Style and NamingUsing gdbMakefiles and Build Targets

3Arrays, Strings, and Algorithmic Basics

4Algorithm Efficiency and Recursion

5Memory, Pointers, and File I/O

6Core Data Structures in C

7Python Fundamentals

8Object-Oriented and Advanced Python

9Relational Databases and SQL

10Web Foundations: HTML, CSS, and JavaScript

11Servers and Flask Web Applications

12Cybersecurity and Privacy Essentials

13Software Engineering Practices

14Version Control and Collaboration

15Capstone: Designing, Building, and Presenting

Courses/CS50 - Introduction to Computer Science/C Language Basics

C Language Basics

9264 views

Learn the C toolchain and core syntax to express algorithms precisely and efficiently.

Content

3 of 15

Primitive Types and sizeof

Primitive Types and sizeof in C: Sizes, Ranges, and Rules
3192 views
beginner
computer science
C
educational
humorous
gpt-5-mini
3192 views

Versions:

Primitive Types and sizeof in C: Sizes, Ranges, and Rules

Watch & Learn

AI-discovered learning video

Sign in to watch the learning video for this topic.

Sign inSign up free

Start learning for free

Sign up to save progress, unlock study materials, and track your learning.

  • Bookmark content and pick up later
  • AI-generated study materials
  • Flashcards, timelines, and more
  • Progress tracking and certificates

Free to join · No credit card required

Primitive Types and sizeof in C — The Small Things That Matter

"If data is the new oil, then primitive types are the refinery — small machinery, huge consequences."

You already know how a C program is stitched together (remember main, compilation, and linking?), and you’ve started building mental models about how data is represented. Now we go deeper: primitive types (the atoms of your program) and the trusty operator sizeof (the ruler that tells you how many bytes those atoms take up). This is the bridge between computational thinking about representation and practical, bug-avoiding C coding.


Why this matters

  • Choosing the right type affects memory, performance, and correctness.
  • sizeof helps you allocate memory correctly, avoid buffer overflows, and reason about how arrays and pointers behave.
  • Many bugs (and portability headaches) come from assuming type sizes, or misusing sizeof on pointers vs arrays.

The primitive family tree (quick tour)

  • Integer types: char, short, int, long, long long — each can be signed or unsigned.
  • Floating types: float, double, long double.
  • Other: _Bool (in <stdbool.h> as bool), and pointer types (e.g., int*).

Important: C does not require exact widths. The standard guarantees minimum ranges. The concrete sizes are platform-dependent (ABI, compiler, architecture): that's why sizeof matters.

A few truths to memorize

  • sizeof(char) is always 1. That defines the C "byte".
  • A byte (char) might be 8 bits on most machines, but consult CHAR_BIT in <limits.h> to be sure.
  • sizeof returns a value of type size_t (an unsigned integer type). Print it with %zu.

sizeof: use it like a pro

Basic usage

#include <stdio.h>
#include <limits.h>

int main(void) {
    printf("sizeof(char) = %zu\n", sizeof(char));
    printf("CHAR_BIT = %d bits per char\n", CHAR_BIT);
    printf("sizeof(int) = %zu\n", sizeof(int));
    printf("sizeof(double) = %zu\n", sizeof(double));
    return 0;
}

Parentheses: when you need them

  • sizeof(type) — parentheses required when you pass a type.
  • sizeof expression — parentheses optional for an expression: sizeof x works.

Example: sizeof x + 1 is parsed as (sizeof x) + 1 (sizeof is a unary operator).

size_t and printf

sizeof yields a size_t. Use %zu in printf; using %d or %ld is wrong on some platforms and will give you grief.


Arrays, pointers, and the sizeof gotcha

This is where students get bitten. Consider:

int a[10];
printf("%zu\n", sizeof a);          // 10 * sizeof(int)
printf("%zu\n", sizeof(a) / sizeof a[0]); // yields 10 -> neat!

int *p = a;
printf("%zu\n", sizeof p);          // size of pointer (e.g., 8), NOT 10 * sizeof(int)

Why? Because arrays usually "decay" into pointers when passed to functions, or when used in many expressions. If you use sizeof inside the same scope where the array is declared, you get the total byte size. But once the array decays (e.g., in a function parameter int a[] or int *a), sizeof gives you the pointer-size — not the array length.

Tip: To compute number of elements in a static array in the same scope: sizeof a / sizeof a[0].


Structs, alignment, and padding — why sizeof can surprise you

CPU architectures have alignment requirements. Compilers insert padding to align fields, so

struct S { char c; int x; };

sizeof(struct S) may be 8 on a 64-bit system (1 byte for char + 3 bytes padding + 4 bytes for int) — not 5. Always trust sizeof, not your intuition.


Integer ranges, overflow, and signed vs unsigned

  • The range of a type depends on its size and whether it is signed.
  • Unsigned arithmetic wraps modulo 2^N (well-defined). Signed overflow is undefined behavior — avoid it.

Use limits.h to inspect constants like INT_MAX, LONG_MIN, etc.


Practical patterns you should use immediately

  • Safe malloc idiom:
int *arr = malloc(n * sizeof *arr); // nicer than sizeof(int) and robust to type changes
  • Use sizeof for portability — never hardcode 4 or 8 unless you really mean a fixed width type (use stdint.h for those: int32_t, int64_t).

  • When writing APIs: prefer passing both pointer and length (or use a struct) — don't rely on sizeof inside a function to find the caller's array length.


Special cases & advanced notes

  • sizeof on VLA (variable-length arrays, C99) is evaluated at run-time (not compile-time).
  • sizeof on a function or on void is invalid.
  • Pointers on 64-bit systems usually have sizeof 8; on 32-bit systems, sizeof 4. This affects data layout and ABI (recall the compiler/linker/ABI conversation from earlier).

Quick reference table

Expression What sizeof returns
sizeof(char) 1 (by definition)
sizeof(int) platform-dependent (common: 4)
sizeof(long) platform-dependent (4 or 8)
sizeof(double) usually 8
sizeof pointer size of pointer (4 or 8)
sizeof array total bytes (length * element size) if in scope
sizeof struct bytes including padding (>= sum of fields)

Takeaways (the TL;DR that wins you exams and fixes bugs)

  • sizeof tells you bytes. The "byte" is char and has CHAR_BIT bits.
  • Don't assume sizes. Use sizeof or <stdint.h> fixed-width types for portability.
  • Arrays vs pointers: sizeof(array) = total bytes, sizeof(pointer) = pointer size. Watch out for decay when passing arrays to functions.
  • Use malloc(n * sizeof *p) to avoid duplicating type names and reduce copy/paste errors.
  • Signed overflow is UB; unsigned wraps. Use the right type for your needs.

"This is the moment where the concept finally clicks: types are not just names — they are contracts with the machine about storage, alignment, and behavior. sizeof is your measuring tape."


Final homework (tiny but effective)

  1. Print sizeof of all common types on your machine: char, short, int, long, long long, float, double, long double, pointers.
  2. Create a struct with mixed types and predict its size before checking with sizeof.
  3. Write a function size_t arrlen(int a[]) and observe what sizeof tells you inside it — then fix the API to accept length.

Do that, and your next debugging session will feel a lot less like spelunking in a dark cave and more like turning on the lights.

Flashcards
Mind Map
Speed Challenge

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!

Ready to practice?

Sign up now to study with flashcards, practice questions, and more — and track your progress on this topic.

Study with flashcards, timelines, and more
Earn certificates for completed courses
Bookmark content for later reference
Track your progress across all topics