tags:

views:

40

answers:

2

On a linux box

is it compulsory to write a program into a file and compile it

cant this be done from command line

localhost$gcc "include<stdio.h> int main(){printf('hello world'); return o; }"
+5  A: 

sure you can, but i doubt that that makes sense....

$ echo '#include <stdio.h>
int main() { printf("hello world\n"); return 0; }' | gcc -x c -
$ ./a.out 
hello world
$

gcc options:

  -o <file>                Place the output into <file>
  -x <language>            Specify the language of the following input files
                           Permissible languages include: c c++ assembler none
                           'none' means revert to the default behavior of
                           guessing the language based on the file's extension
Markus Winand
@ Markus:Can a.out be changed to a user choice instead,say test1.out for the above code..
Rajeev
@Rajeev Yes, specify the -o option, which tells gcc where it should write to.e.g. gcc file.c -o example; ./example
brennie
Thanks....................
Rajeev
A: 

I always liked Barry Browns answer to FizzBuzz:

In C:

F

Compile with:

gcc -DF='main(){int i;for(i=0;i<101;puts(i++%5?"":"Buzz"))printf(i%3?i%5?"%d":"":"Fizz",i);}' fizzbuzz.c
Konerak