11

By "code snippet execution", I mean the ability to write a few lines of code, run and test it without having to fire up an IDE and create a dummy project.

It's incredibly useful for helping people with a small code sample without creating a project, compiling everything cleanly, sending them the code snippet and deleting the project.

I'm not asking about the best code snippets or a snippet editor or where to store snippets!

For C#, I use Snippet Compiler.

For Java, I use Eclipse Scrapbook.

For LINQ, I use LINQPad.

Any suggestions for other (better?) tools? e.g. is there one for Java that doesn't involve firing up Eclipse?

What about C?

1

6 Answers 6

4

Try ideone. It is able to run your code on server side in more than 40 programming languages including C and Java (it is free for individual usage).

Sign up to request clarification or add additional context in comments.

Comments

3

For C, the in-browser http://codepad.org/ is truly excellent. Executes code and everything.

Comments

1

For executing JavaScript snippets I use most Firebug and Google Chrome JavaScript console.

For F# I use the Interactive Console.

Comments

1

I sometimes want to try something very short just to confirm semantics. Since creating a temporary file and putting in the boilerplate takes more than 30 seconds, I have this script:

#!/bin/sh

body="$1"
out=$(mktemp /tmp/ccrun-XXXXXX)
src=${out}.c
cat > ${src} <<EOF
#include <limits.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define UNUSED __attribute__((unused))

int main(int UNUSED argc,char UNUSED *argv[])
{
EOF
echo "$body" >> ${src}
echo -e "return 0;\n}" >> ${src}
cc -std=c99 -Wall -Wextra ${CCRUN_FLAGS} -o ${out} ${src} -lm
shift
echo ${out} "$@"
${out} "$@"
#rm ${out} ${src}

A sample invocation (this code statically initializes an array of function pointers) looks like:

$ ccrun 'int f(int a){return a+1;} int g(int a){return a+2;} int (*farr[2])(int) = {f,g}; for (int i=0; i<2; i++) printf("%d %d\n",i,farr[i](i));'
/tmp/ccrun-6nT4Wo
0 1
1 3

If I want to make little changes, I just edit the command line. If it becomes unwieldy on the command line, I'll edit the temporary file, in this case /tmp/ccrun-6nT4Wo.c. Command line arguments to the executable can be given after the program (first argument). The executable is left in place so it can be run without recompiling. You can do something similar for any language.

Comments

0

In Ruby you can use the Interactive Ruby Shell.

It also looks like the guru's at the mono project have gone and made a C# interactive. YAY

Comments

0

For Python and Sage, try out http://live.codenode.org. It is also an open source project, licensed under the BSD, so it can be downloaded and ran from your own computer, more info is here: http://codenode.org

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.