1# c 2 3<!-- 4[![Build Status](https://travis-ci.org/ryanmjacobs/c.svg)](https://travis-ci.org/ryanmjacobs/c) 5--> 6 7> "There isn't much that's special about C. That's one of the reasons why it's 8fast." 9 10I love C for its raw speed (although it does have its drawbacks). We should 11all write more C. 12 13With this shell script, you can compile and execute C "scripts" in one go! 14 15(Oh yeah, and it works for C++ too.) 16 17![GIF Demo](http://i.imgur.com/aWnG03r.gif) 18 19Here's a simple example: 20```c 21#include <stdio.h> 22 23int main(void) { 24 printf("Hello World!\n"); 25 return 0; 26} 27``` 28 29Run it by typing: 30```bash 31$ c hello.c 32Hello World! 33``` 34 35Or, call it from the shebang! 36```c 37#!/usr/bin/c 38#include <stdio.h> 39 40int main(void) { 41 printf("Hello World!\n"); 42 return 0; 43} 44``` 45```bash 46$ chmod +x hello.c 47$ ./hello.c 48Hello World! 49``` 50 51## Hooked? Here's how to install it: 52Use a package manager? [Check here](https://github.com/ryanmjacobs/c#packages). 53 54For the entire system: 55```bash 56$ wget https://raw.githubusercontent.com/ryanmjacobs/c/master/c 57$ sudo install -m 755 c /usr/bin/c 58 59# Or... for systems that prefer /usr/local/bin (e.g. macOS) 60$ sudo install -m 755 c /usr/local/bin/c 61``` 62 63For your local user only: 64```bash 65$ wget https://raw.githubusercontent.com/ryanmjacobs/c/master/c 66$ sudo install -Dm 755 c ~/.bin/c 67$ echo 'PATH=$PATH:$HOME/.bin' >> ~/.bashrc 68``` 69 70Note: if you install it somewhere other than `/usr/bin/c`, then your shebang 71will be different. For example it may be something more similar to 72`#!/home/ryan/.bin/c`. 73 74## Okay, how do I use it? 75c will use whatever `$CC` is set to. You can change this with: 76```bash 77$ export CC=clang 78$ export CC=tcc 79$ # etc... 80``` 81## CLI 82### Multiple Files - CLI 83Anything you want passed to the compiler, put in quotes as the first argument. 84Whether they're flags (`-Wall`, `-O2`, etc.) or file names (`file.c`, 85`main.c`, etc.). 86 87```bash 88$ c "main.c other.c" arg1 arg2 89$ c "main.c other.c -O3 -Wall -lncurses" arg1 arg2 90``` 91### Single File - CLI 92With only one file, omit the quotes: 93```bash 94$ c hello.c 95$ c main.c arg1 arg2 96``` 97 98## Shebang! 99After adding a shebang, just set the file to executable and it's ready to run. 100```bash 101$ chmod +x file.c 102$ ./file.c 103``` 104 105### Single File - Shebang 106Add this to the top of your C file: 107```c 108#!/usr/bin/c 109``` 110 111### Multiple Files - Shebang 112Just tack on any extra flags, options, or files you want passed to the compiler. 113Then be sure to add the terminating `--` characters. 114```c 115#!/usr/bin/c file1.c file2.c -lncurses -lm -- 116``` 117 118## Caching 119The default cache size is set to 5 MB. You can change this with: 120```bash 121$ export C_CACHE_SIZE=$((10*1024)) # 10 MB 122``` 123The default cache path is set to `$TMPDIR/c.cache`. You can change this with: 124```bash 125$ export C_CACHE_PATH="/tmp/the_cache" 126``` 127 128# Contributing 129Feel free to submit any ideas, questions, or problems by reporting an issue. 130Or, if you're feeling a bit brave, submit a pull request. :grimacing: 131 132Just hack away and make sure that all the tests pass. 133```bash 134$ cd tests 135$ ./test.sh 136``` 137 138## Why? 139First of all, I want to clarify why this is **not** the same as `tcc -run`. 140TCC is a compiler. We all know that. TCC will perform its own set of 141optimizations, just as GCC will perform its own and Clang will perform its 142own. The purpose of this script is to give a simple front-end to your favorite 143compiler. 144 145Whether it's GCC, Clang, or something else entirely, **you** get to choose 146your compiler. 147 148Second reason: it's simply satisfying to type `c hello.c` and see it run instantly. 149 150Third reason: I'm a fan of speed, and C definitely offers it. Being able to 151write a small, fast, and portable C "script" is great. You can pass around a 152C "script" just like you would a BASH script. 153 154## zsh 155If you're using `zsh`, then you can take advantage of `zsh`'s suffix aliases: 156```bash 157$ alias -s c='c' 158$ alias -s cc='c' 159$ alias -s cpp='c' 160``` 161Then you can run files with `./file.c` without `chmod +x`. 162 163## Packages 164Use a package manager? You've come to the right place. 165 166AUR: https://aur.archlinux.org/packages/c/<br> 167bpkg: `bpkg install ryanmjacobs/c`<br> 168brew: `brew install https://raw.githubusercontent.com/ryanmjacobs/c/master/c.rb` 169(shebang will be `#!/usr/local/bin/c`)<br> 170 171## Todo 172~~Maybe later we can implement caching.~~ Done! 173 174## License 175[MIT License](https://raw.githubusercontent.com/ryanmjacobs/c/master/LICENSE). 176 177Basically, you can do whatever you want provided that you include 178the LICENSE notice in any copy of the source. Also, I am not liable 179if the script breaks anything. 180