Learn EMSCRIPTEN with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Simple C Addition Function (Emscripten)
# emscripten/demo/add.c
#include <emscripten/emscripten.h>
EMSCRIPTEN_KEEPALIVE
int add(int a, int b) {
return a + b;
}
A basic C function that adds two numbers and returns the result, compiled with Emscripten.
2
Subtract Two Numbers (Emscripten)
# emscripten/demo/subtract.c
#include <emscripten/emscripten.h>
EMSCRIPTEN_KEEPALIVE
int subtract(int a, int b) {
return a - b;
}
Subtracts one integer from another.
3
Multiply Two Numbers (Emscripten)
# emscripten/demo/multiply.c
#include <emscripten/emscripten.h>
EMSCRIPTEN_KEEPALIVE
int multiply(int a, int b) {
return a * b;
}
Multiplies two integers and returns the result.
4
Divide Two Numbers (Emscripten)
# emscripten/demo/divide.c
#include <emscripten/emscripten.h>
EMSCRIPTEN_KEEPALIVE
int divide(int a, int b) {
return a / b;
}
Divides one integer by another, integer division.
5
Factorial Function (Emscripten)
# emscripten/demo/factorial.c
#include <emscripten/emscripten.h>
EMSCRIPTEN_KEEPALIVE
int factorial(int n) {
if(n <= 1) return 1;
return n * factorial(n - 1);
}
Calculates factorial of a number recursively.
6
Fibonacci Function (Emscripten)
# emscripten/demo/fibonacci.c
#include <emscripten/emscripten.h>
EMSCRIPTEN_KEEPALIVE
int fibonacci(int n) {
if(n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
Calculates Fibonacci number recursively.
7
Check Even Number (Emscripten)
# emscripten/demo/isEven.c
#include <emscripten/emscripten.h>
EMSCRIPTEN_KEEPALIVE
int isEven(int n) {
return (n % 2) == 0;
}
Returns 1 if a number is even, 0 otherwise.
8
Check Odd Number (Emscripten)
# emscripten/demo/isOdd.c
#include <emscripten/emscripten.h>
EMSCRIPTEN_KEEPALIVE
int isOdd(int n) {
return (n % 2) != 0;
}
Returns 1 if a number is odd, 0 otherwise.
9
Maximum of Two Numbers (Emscripten)
# emscripten/demo/max.c
#include <emscripten/emscripten.h>
EMSCRIPTEN_KEEPALIVE
int max(int a, int b) {
return a > b ? a : b;
}
Returns the maximum of two integers.
10
Minimum of Two Numbers (Emscripten)
# emscripten/demo/min.c
#include <emscripten/emscripten.h>
EMSCRIPTEN_KEEPALIVE
int min(int a, int b) {
return a < b ? a : b;
}
Returns the minimum of two integers.