code-playpad

C/C++ (experimental)

language="c" and language="cpp" compile with the pinned Clang 20.1.2 build in a Web Worker, link with LLD and run the result as WebAssembly. The first Run downloads about 30 MB compressed (95 MB once unpacked); later runs use Cache Storage.

C: hello

#include <stdio.h> int main(void) { for (int i = 1; i <= 3; i++) printf("%d. hello from C17\n", i); return 0; }

C++: reading stdin

Input is prefilled: edit the stdin box before pressing Run. The program sees EOF after the last line.

#include <iostream> #include <string> int main() { std::string name; int n; std::cin >> name >> n; for (int i = 0; i < n; i++) std::cout << "hello, " << name << '\n'; std::cerr << "(stderr works too)\n"; }

C++20: STL and a LeetCode-style function

#include <bits/stdc++.h> using namespace std; vector<int> twoSum(const vector<int>& nums, int target) { unordered_map<int, int> seen; for (int i = 0; i < (int)nums.size(); i++) { if (auto it = seen.find(target - nums[i]); it != seen.end()) return {it->second, i}; seen[nums[i]] = i; } return {}; } int main() { auto r = twoSum({2, 7, 11, 15}, 9); cout << "twoSum: [" << r[0] << ", " << r[1] << "]\n"; vector<string> words{"pear", "fig", "apple", "kiwi"}; ranges::sort(words, {}, &string::size); for (const auto& w : words) cout << w << ' '; cout << '\n'; map<char, int> counts; for (char c : string("mississippi")) counts[c]++; for (auto [c, k] : counts) cout << c << '=' << k << ' '; cout << '\n'; }

Multi-file C++

#include <iostream> #include "shapes.h" int main() { Rect r{3, 4}; std::cout << "area " << area(r) << ", perimeter " << perimeter(r) << '\n'; } #pragma once struct Rect { int w, h; }; int area(Rect r); int perimeter(Rect r); #include "shapes.h" int area(Rect r) { return r.w * r.h; } int perimeter(Rect r) { return 2 * (r.w + r.h); }

Errors

A compile error, then a program that calls abort().

#include <iostream> int main() { int x = "not a number"; std::cout << x << '\n' } #include <stdio.h> #include <stdlib.h> int main(void) { puts("about to abort"); abort(); }

Stop and the time limit

This loops forever: press Stop. The next Run reinitializes the compiler.

#include <stdio.h> int main(void) { for (unsigned long i = 0;; i++) if (i % 100000000 == 0) printf("still running: %lu\n", i); }