JS
C

JavaScript to C

10 lessons

Progress0%
1Variables & Types2Functions3Arrays & Pointers4Objects → Structs5Memory Management6Preprocessor & Headers7String Handling in Depth8Enums and Bitwise Operations9Bit-Fields and Unions10Multi-File Projects and Linking
All Mirror Courses
JS
C
Variables & Types
MirrorLesson 1 of 10
Lesson 1

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.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with static types and primitive sizes.

C
In C:

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:

C
C 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:

JS
JavaScript (What you know)
let name = "Alice";
let age = 30;
let score = 9.5;
let active = true;
let big = 9007199254740993n; // BigInt

const MAX = 100;
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C has separate float (32-bit) and double (64-bit); JS has only number

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C strings are null-terminated char arrays, not objects

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C requires #include for bool; JS has built-in boolean

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

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.

JS
JavaScript
let count = 5; let ratio = 0.5;
C
C
int count = 5;    // 32-bit integer
double ratio = 0.5; // 64-bit float

2. 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.

JS
JavaScript
let s = "Hello";
C
C
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.

Rule of Thumb
When in doubt, use int for integers and double for floating point.

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
Common Pitfall
Don't assume C works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

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)
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in C to practice these concepts.
OverviewNext