crazycloudcc@blog:~/playground.cc — zsh — 80×24
--:--:--···
crazycloudcc@blog:~/playground.cc$ pwd
/home/crazycloudcc/blog/playground.cc
> cd ./playground.cc
// playground.cc

C/C++ 在线编译器

C Playground:选择 C11 或 C++17,在浏览器里用 clang 编译运行,无需安装编译器,也无需上传源码。 适合验证小程序、练习算法和查看编译诊断。

使用 scanf / cin 时,请先填好 stdin;运行期间不能追加输入。单次执行限时 5 秒,C++ 不支持异常。 首次打开需要下载 WebAssembly 工具链。

运行示例 · Try C / C++ examples

使用指南: 编译原理与限制 · scanf 与 stdin · 读懂 clang 报错

// playground.cc
> make toolchain
// loading playground...
// quick start

从一个能运行的例子开始 · Run your first program

打开示例会同时载入代码和输入;等待 toolchain ready 后点击 run,再对照预期输出。 示例是只读的,不会覆盖你的代码草稿或 stdin。用浏览器返回后默认打开 C++;选择 C 可继续之前的 C 草稿。

Open an example, wait for toolchain ready, then click run and compare stdout below. Read-only examples keep your saved code and input intact. Go Back to the C++ editor; select C again to resume your C draft.

C · Hello World

用 printf 输出一行文字,不需要输入。Print a line with printf; no input needed.

stdin · 输入
无需输入 · empty
stdout · 预期输出
Hello, World
查看代码 · View hello.c
#include <stdio.h>

int main(void) {
    printf("Hello, World\n");
    return 0;
}
运行 hello.c · read-only

C · scanf 输入求和

用 scanf 读取两个整数,再输出它们的和。Read two integers from stdin and print their sum.

stdin · 输入
3 4
stdout · 预期输出
7
查看代码 · View a+b.c
#include <stdio.h>

int main(void) {
    int a, b;
  if (scanf("%d %d", &a, &b) != 2) {
    return 1;
  }
    printf("%d\n", a + b);
    return 0;
}
运行 a+b.c · read-only

C++ · std::sort 排序

先输入个数,再输入整数序列,按升序输出。Read a count and its integers, then sort them in ascending order.

stdin · 输入
5 4 1 5 2 3
stdout · 预期输出
1 2 3 4 5
查看代码 · View sort.cpp
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

bool read_int(int& value) {
    std::string token;
    if (!(std::cin >> token)) {
        return false;
    }
    std::istringstream parser(token);
    return (parser >> value) && parser.eof();
}

int main() {
    int n;
    if (!read_int(n) || n < 0 || n > 10000) {
        std::cerr << "Expected a count from 0 to 10000.\n";
        return 1;
    }
    std::vector<int> values(n);
    for (int& value : values) {
        if (!read_int(value)) {
            std::cerr << "Expected " << n << " integers after the count.\n";
            return 1;
        }
    }
    std::sort(values.begin(), values.end());
    for (int value : values) {
        std::cout << value << " ";
    }
    std::cout << "\n";
    return 0;
}
运行 sort.cpp · read-only

想修改例子?在上方编辑器选择 C 或 C++,再从 example 菜单载入同名代码。 菜单只替换代码;需要输入时,点击 stdin 旁的「载入示例输入」。 To edit, choose the matching language and example in the editor above, then load its sample input beside stdin.

branch: mainposts: 12encoding: utf-8