I wanted to try using a simple printf on terminal to see if i can directly program on it , but didn't work; I wrote on it these 2 lines:

~$ #include<stdio.h>
~$ printf("E");

and it says

bash: syntax error near unexpected token '"E"'

I don't see anything wrong...?


Your shell isn't a C interpreter - it has its own syntax, and its own printf function, which aside from inheriting many of the format specifiers from the C function is quite separate

Valid forms are

printf 'E\n'


printf '%c\n' E

There is no equivalent of the #include directive (which would just be an - ignored - comment as far as the shell is concerned)

If you want to actually write a small C program from the command line, you can do that with cat using a here document:

$ cat > main.c
#include <stdio.h>

int main(void) {
    printf("Hello world!\n");
    return 0;

Terminate your input py pressing Ctrl+D. Then you can compile the program using gcc:

gcc -o my_first_prog main.c

and finally run it from the shell

$ ./my_first_prog 
Hello world!
| improve this answer | |
  • this is quite silly then, my professor was teaching about the "main" function arguments and wrote on the blackboard a C program with main taking 2 ints from the terminal...saying it could all be written on the shell but i guess it's a lie then – wattbatt Mar 9 '19 at 11:11
  • 1
    @wattbatt perhaps a misunderstanding: you can certainly write a C program from the command line - however you can't expect the Bash shell to interpret and run it. – steeldriver Mar 9 '19 at 11:16
  • @wattbatt I have added what I imagine your professor had in mind – steeldriver Mar 9 '19 at 11:26
  • 1
    @wattbatt "but i guess it's a lie then" – apparently you're a newbie with shell and C programming, that's fine, everyone starts as a newbie. But please stop for a moment and think about the probability of your professor knowing something fundamental incorrectly or even telling straight lies, versus you misunderstanding something. – egmont Mar 9 '19 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.