Build Your First WebAssembly Project with C in WebAssembly Studio

This tutorial walks you through creating a simple C project in WebAssembly Studio, converting the code to WebAssembly, editing the accompanying HTML and JavaScript, and running the result in the browser, providing a hands‑on introduction to WebAssembly development.

21CTO
21CTO
21CTO
Build Your First WebAssembly Project with C in WebAssembly Studio

As web applications grow, developers face the limitation that browsers natively execute only JavaScript, prompting the creation of WebAssembly to deliver near‑native performance for languages like C, C++ and Rust.

What is WebAssembly

WebAssembly (Wasm) is a low‑level binary format that runs alongside HTML, CSS and JavaScript, offering a fourth language for the web and enabling high‑performance code compiled from C/C++ or Rust.

Project Practice

Open WebAssembly Studio and create a new Empty C Project . The interface shows several panels:

Panel 1 – Build and run the project.

Panel 2 – Save your files.

Panel 3 – Contains main.c, main.html and main.js.

In main.c replace the default content with exported functions:

#define WASM_EXPORT __attribute__((visibility("default")))
WASM_EXPORT
int add(int a, int b) {
  return a + b;
}

WASM_EXPORT
int sub(int a, int b) {
  return a - b;
}

Mark the functions with WASM_EXPORT so they become visible to WebAssembly.

Next, edit main.html to provide placeholders for the results:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <style>
    body { background-color: #fff; }
  </style>
</head>
<body>
  <h1 id="add"></h1>
  <h1 id="sub"></h1>
</body>
</html>

Save the file, then modify main.js to call the exported functions and display their results:

document.getElementById("add").textContent = "1 + 2 = " + instance.exports.add(1,2);
document.getElementById("sub").textContent = "1 - 2 = " + instance.exports.sub(1,2);

After saving, click the Build & Run button in Panel 1. The output appears in the preview area, showing the computed values.

You can also create a new file in Panel 3 to view the generated WebAssembly module, which lists the exported symbols add and sub. Download the .wasm file for use in other projects if desired.

Summary

This guide demonstrates how WebAssembly Studio lets developers quickly prototype and run C code as WebAssembly, illustrating the workflow of writing, exporting, and invoking functions from the browser, and highlighting WebAssembly as a promising future web technology.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

WebAssemblyCWebAssembly Studio
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.