Variables & Types
Static types and primitive sizes
Introduction
In this lesson, you'll learn about variables & types in C. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In JavaScript, you're familiar with static types and primitive sizes.
C has its own approach to static types and primitive sizes, which we'll explore step by step.
The C Way
Let's see how C handles this concept. Here's a typical example:
#include <stdio.h>
#include <stdbool.h> /* for bool (C99) */
char name[] = "Alice"; /* string = char array */
int age = 30;
float score = 9.5f; /* 32-bit float */
double pi = 3.14159; /* 64-bit float */
bool active = true;
const int MAX = 100;
/* Sized integers */
#include <stdint.h>
int64_t big = 9007199254740993LL;
uint8_t byte = 255;Comparing to JavaScript
Here's how you might have written similar code in JavaScript:
let name = "Alice";
let age = 30;
let score = 9.5;
let active = true;
let big = 9007199254740993n; // BigInt
const MAX = 100;You may be used to different syntax or behavior.
C has separate float (32-bit) and double (64-bit); JS has only number
You may be used to different syntax or behavior.
C strings are null-terminated char arrays, not objects
You may be used to different syntax or behavior.
C requires #include for bool; JS has built-in boolean
You may be used to different syntax or behavior.
C has sized integer types (int8_t, int64_t); JS has BigInt for large integers
Step-by-Step Breakdown
1. Numeric Types
C has many numeric types with explicit sizes. Choosing the right type matters for performance and correctness.
let count = 5; let ratio = 0.5;int count = 5; // 32-bit integer
double ratio = 0.5; // 64-bit float2. Strings as Char Arrays
C has no string type. Strings are arrays of chars ending with a null byte '\0'. This is why string operations need buffer sizes.
let s = "Hello";char s[] = "Hello"; /* {'H','e','l','l','o','\0'} */3. No Type Inference
C requires explicit type declarations for every variable. There is no let or var — just the type name.
Common Mistakes
When coming from JavaScript, developers often make these mistakes:
- C has separate float (32-bit) and double (64-bit); JS has only number
- C strings are null-terminated char arrays, not objects
- C requires #include for bool; JS has built-in boolean
Key Takeaways
- C has float, double, int, char — JS has only number + string
- C strings are char arrays with null terminator
- All variables need explicit type declarations
- stdint.h provides sized types (int32_t, uint8_t)