Skip to content

smnmsr/crun

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

crun — Tiny C one‑liner runner for PowerShell

Run little bits of C from your shell and see the result immediately. You type C statements or a single expression (what goes inside main()), crun wraps it into a minimal program, compiles with TinyCC, runs it, and auto‑prints a result when you don’t explicitly print.

  • Auto‑prints the last expression or the last declared/assigned variable
  • Knows basic int vs float printing (uses %lld for ints, %g for floats)
  • math.h ready (e.g., sin, sqrt)
  • Interactive multiline input and editor mode
  • No build artifacts (uses a temp file and cleans up)

Requirements

  • PowerShell 7+ (pwsh). It can also work on Windows PowerShell, but is developed against pwsh.
  • Tiny C Compiler (tcc) on your PATH. Download TinyCC and ensure tcc.exe is available in your PATH.
    • Windows: download from the TinyCC site (or your preferred package manager) and add the install folder to PATH.

Install / Setup

  • Put crun.ps1 somewhere convenient (or clone this repo).
  • Optionally add a shell alias so you can just type crun anywhere:
# In your $PROFILE
Set-Alias crun (Resolve-Path 'C:\\path\\to\\crun.ps1')

Alternatively, call it explicitly when testing:

pwsh -NoProfile -File .\crun.ps1 1+2

Quick start

Use single quotes around your C snippet. This avoids PowerShell interpreting parentheses and quotes.

# Integers and operators
crun 100%27           # 19
crun 1+2              # 3
crun 1<<5             # 32

# Floating point & math.h
crun 'sqrt(2)'        # 1.41421
crun 'sin(0.5)'       # 0.479426

# Explicit printing
crun 'printf("hi!\n")'    # hi!

# Variables — last variable auto‑prints
crun 'int a=7,b=27; int k=b%a'   # 6
crun 'double x=2.5; x'           # 2.5

If you insist on double quotes, you must escape/double the inner quotes because PowerShell parses them:

# Either escape with the PowerShell double-double-quote escape
crun "printf(""hi!\n"")"

# Or single outside and double inside
crun 'printf("hi!\n")'

Examples

See EXAMPLES.md for a grab‑bag of ready‑to‑run snippets.

Multiline and editor modes

  • No arguments: interactive multiline input. Type lines; finish with EOF on its own line.
  • -UseEditor: opens your editor to write multiple lines; save & close to run. The script checks $env:VISUAL, $env:EDITOR, then tries vim, nvim, nano, code -w, notepad.

Interactive example:

crun
Enter C statements for inside main(); finish with EOF on its own line.
Examples:
  printf(\ hi\\n\);
  int a=7,b=27; int k=b%a;
  sin(0.5)  // (no printf) last expr auto-prints
c>: int s=0;
c>: for(int i=1;i<=5;i++) s+=i;
c>: s
c>: EOF
15

Editor mode:

crun -UseEditor
# Write statements inside main(), for example:
#   int s=0; for(int i=0;i<10;i++) s+=i; s
# Save & exit to run

Auto‑print rules (what gets printed)

  • If you don’t call printf/puts and don’t return, crun auto‑prints:
    • The last expression, or
    • The last declared/assigned variable’s value (tries to pick int vs float formatting)
  • If you do print or explicitly return, auto‑print is skipped. You’re in full control.
  • A trailing semicolon is added for you if you forget it.

Headers included by default: stdio.h, stdlib.h, stdint.h, stdbool.h, string.h, math.h, time.h, errno.h, assert.h, plus a convenience macro #define BIT(x) (1ULL<<(x)).

Common pitfalls (PowerShell quoting!)

PowerShell is not a dumb pass‑through; it parses parentheses, quotes, and % (alias for ForEach-Object).

  • Prefer single quotes around the entire snippet: crun 'printf("hi!\n")'
  • Unquoted snippets that contain parentheses may be split by PowerShell. Example: crun sqrt(2) is parsed into two tokens (sqrt and 2), producing a compile error. Quote it: crun 'sqrt(2)'.
  • If you use double quotes, escape inner quotes with double doublequotes: crun "printf("\""hi!\n"")".
  • Escaping using a backslash (e.g., crun "printf(\"hi!\n\")") does not work.

License

See LICENSE.

About

Run C expressions and statements from pwsh via TinyCC. Auto-prints the last value, handles int/float formatting, supports math.h, and offers interactive/editor modes.

Topics

Resources

License

Stars

Watchers

Forks

Contributors