Making a Single File C/C++ Sandbox

Need to try something out quickly in C? Then it’s helpful to know how to compile a one file C program as a way to make yourself a C sandbox.

You will need to be comfortable with the command prompt and have a C++ compiler installed:

Create a text file with a text editor with something like:

#include <stdio.h> int main(){ printf("Hello World!\n"); return 0; }

Save the file into something like main.c. Then compile it at the command prompt using:

c++ -o test main.c ./test

This will compile the input file called main.c into a binary file called test. Then ./test is what is used to run the program.

On my machine this is what I see:

Eliots-MacBook-Pro-2:example eliotmuir$ vi main.c Eliots-MacBook-Pro-2:example eliotmuir$ c++ -o test main.c Eliots-MacBook-Pro-2:example eliotmuir$ ./test Hello World! Eliots-MacBook-Pro-2:example eliotmuir$

 

Related pages