1. Home
  2. /
  3. Emscripten
  4. /
  5. Minimum of Two Numbers (Emscripten)

Minimum of Two Numbers (Emscripten) - Emscripten Typing CST Test

Loading…

Minimum of Two Numbers (Emscripten) — Emscripten Code

Returns the minimum of two integers.

# emscripten/demo/min.c
#include <emscripten/emscripten.h>

EMSCRIPTEN_KEEPALIVE
int min(int a, int b) {
	return a < b ? a : b;
}

Emscripten Language Guide

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.

Primary Use Cases

  • ▸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

Notable Features

  • ▸Compiles C/C++ code to WebAssembly and asm.js
  • ▸Supports modern web APIs like WebGL and WebAudio
  • ▸Provides a virtual file system (MEMFS, IDBFS)
  • ▸Enables multi-threaded code via WebAssembly threads
  • ▸Integrates with JavaScript using embind or WebIDL bindings

Origin & Creator

Emscripten was originally developed by Alon Zakai in 2010 and is now maintained as an open-source project under the LLVM and WebAssembly ecosystems.

Industrial Note

Emscripten is ideal for developers porting legacy C/C++ applications to the web, including games, scientific simulations, multimedia apps, and performance-critical software.

Quick Explain

  • ▸Emscripten converts C/C++ codebases into WebAssembly or asm.js for web execution.
  • ▸Enables running performance-intensive applications such as games, simulations, and multimedia apps in browsers.
  • ▸Provides bindings to web APIs like WebGL, WebAudio, and WebSockets.
  • ▸Supports integration with JavaScript for hybrid applications.
  • ▸Offers a virtual file system and runtime to emulate POSIX-style behavior in the browser.

Core Features

  • ▸LLVM-based compiler toolchain
  • ▸WebAssembly output for near-native performance
  • ▸Asynchronous and synchronous JavaScript glue code generation
  • ▸Filesystem emulation in the browser
  • ▸Support for debugging, optimization, and profiling

Learning Path

  • ▸Learn C/C++ programming basics
  • ▸Understand WebAssembly and browser runtime constraints
  • ▸Install and configure Emscripten SDK
  • ▸Compile and run simple C/C++ projects in browser
  • ▸Integrate with JavaScript and web APIs

Practical Examples

  • ▸Compile SDL-based game to run in browser
  • ▸Run physics simulations directly in WebAssembly
  • ▸Port audio processing C libraries to WebAudio
  • ▸Use WebAssembly for cryptography and compression tasks
  • ▸Integrate legacy native code with modern web apps

Comparisons

  • ▸Emscripten vs WebAssembly: Emscripten is a compiler, Wasm is the runtime format
  • ▸Emscripten vs Fastly Compute@Edge: Emscripten targets browsers, Fastly runs code at edge servers
  • ▸Emscripten vs Google Cloud Functions: Emscripten compiles native code to web, GCF runs serverless cloud functions
  • ▸Emscripten vs asm.js: Wasm is faster, asm.js is older fallback
  • ▸Emscripten vs FunctionX: FX is blockchain smart contracts, Emscripten is for browser-native code compilation

Strengths

  • ▸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

Limitations

  • ▸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

When NOT to Use

  • ▸Applications not using C/C++ code
  • ▸Web apps that don’t require high-performance native code
  • ▸Projects needing server-side execution instead of browser runtime
  • ▸Tasks heavily reliant on database or backend services
  • ▸Code requiring OS-level resources unavailable in browser sandbox

Cheat Sheet

  • ▸emcc source.c -o output.html -> compile C to Wasm + HTML
  • ▸emcc source.c -s WASM=1 -o output.js -> compile to Wasm + JS
  • ▸emrun output.html -> run compiled module in browser
  • ▸emcc -O3 source.c -o output.js -> optimize compilation
  • ▸Use embind to bind C++ classes/functions to JS

FAQ

  • ▸Is Emscripten free?
  • ▸Yes - it is open-source and free to use
  • ▸What languages are supported?
  • ▸Primarily C and C++, with partial support for other LLVM languages
  • ▸Does Emscripten work in all browsers?
  • ▸Works in modern browsers with WebAssembly support; asm.js fallback for older browsers
  • ▸Can I use multithreading?
  • ▸Yes, if the browser supports WebAssembly threads and SharedArrayBuffer
  • ▸How do I debug Emscripten code?
  • ▸Use browser developer tools, source maps, and logging; inspect JS glue code

30-Day Skill Plan

  • ▸Week 1: C/C++ fundamentals and small programs
  • ▸Week 2: Install Emscripten and compile first Wasm module
  • ▸Week 3: Integrate WebGL and WebAudio
  • ▸Week 4: Port a small game or simulation to browser
  • ▸Week 5: Optimize performance and debug advanced scenarios

Final Summary

  • ▸Emscripten is a compiler that converts C/C++ code into WebAssembly for the web.
  • ▸Enables near-native performance in browsers and integration with web APIs.
  • ▸Supports multimedia, graphics, and computationally intensive applications.
  • ▸Provides virtual filesystem and runtime emulation for native code.
  • ▸Ideal for porting legacy applications, games, and high-performance web apps.

Project Structure

  • ▸src/ - C/C++ source files
  • ▸include/ - header files
  • ▸build/ - compiled output (Wasm, JS)
  • ▸emsdk/ - Emscripten SDK (optional local copy)
  • ▸scripts/ - build automation scripts

Monetization

  • ▸Bring desktop games to web platform
  • ▸Deliver computationally intensive apps to browsers
  • ▸Enable SaaS tools with high-performance browser modules
  • ▸Reduce need for native app installation
  • ▸Distribute legacy software to wider audience

Productivity Tips

  • ▸Use precompiled libraries when possible
  • ▸Leverage build automation via CMake/Make
  • ▸Minimize WebAssembly size for faster loading
  • ▸Profile and optimize hot code paths
  • ▸Use embind for easier JS/C++ integration

Basic Concepts

  • ▸emcc - Emscripten compiler for C/C++ to Wasm
  • ▸WebAssembly - binary format for running code in browsers
  • ▸asm.js - JavaScript fallback for older browsers
  • ▸embind - binding C++ classes/functions to JavaScript
  • ▸Filesystem (MEMFS/IDBFS) - virtual file storage in browser

Official Docs

  • ▸https://emscripten.org/docs/
  • ▸https://emscripten.org/docs/getting_started/index.html

More Emscripten Typing Exercises

Simple C Addition Function (Emscripten)Subtract Two Numbers (Emscripten)Multiply Two Numbers (Emscripten)Divide Two Numbers (Emscripten)Factorial Function (Emscripten)Fibonacci Function (Emscripten)Check Even Number (Emscripten)Check Odd Number (Emscripten)Maximum of Two Numbers (Emscripten)

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher