Learn Emscripten - 10 Code Examples & CST Typing Practice Test
Emscripten is an open-source compiler toolchain that compiles C and C++ code into WebAssembly (Wasm) or asm.js, allowing developers to run native code in web browsers at near-native speed.
View all 10 Emscripten code examples →
Learn EMSCRIPTEN with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Emscripten
What is Emscripten?
Emscripten is an open-source compiler toolchain that compiles C and C++ code into WebAssembly (Wasm) or asm.js, allowing developers to run native code in web browsers at near-native speed.
What are the primary use cases for Emscripten?
Porting desktop games and engines to web browsers. Running scientific simulations and numerical computing in browsers. Enabling multimedia processing (audio/video) on the web. Creating WebAssembly modules for high-performance web apps. Bridging native code libraries to JavaScript/TypeScript projects
What are the strengths of Emscripten?
Run native C/C++ applications in browsers. High-performance WebAssembly execution. Large ecosystem and open-source support. Cross-platform compatibility (Windows, macOS, Linux -> web). Access to modern web APIs from compiled code
What are the limitations of Emscripten?
Limited access to browser-specific features compared to native JavaScript. Debugging can be challenging due to generated code. Threading and SIMD support varies across browsers. Initial compilation setup can be complex. Performance depends on WebAssembly engine and browser optimization
How can I practice Emscripten typing speed?
CodeSpeedTest offers 10+ real Emscripten code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.