Login | Register

Nerd Paradise

Now with 15% less sodium!
Open
Original issue
I am attempting to learn C, and so I created this program:

#include <stdio.h>
main()
{
  printf("hello world\n");
}

but when I execute it in bash, all I get is
bash: 2.c: line 4: syntax error near unexpected token `"hello world\n"'
bash: 2.c: line 4: `  printf("hello world\n");'


Does anyone know what the problem is? I've consulted my copy of K&R, and it agrees with my code...
[Quote] [Link]
Closed
Solution
You've written a C program but are attempting to run it in bash. You need to compile it first, because bash does not understand C syntax. You might have better luck with csh, but the real right way is to compile it.
Save it to a file like 'myprogram.c' and then say
$ gcc -o myprogram myprogram.c
. Then you'll get an executable you can run:
$ ./myprogram
Bash is not executing your code, it's only telling the OS to load the program.

Edit: Some background -
C code is not executed directly by any program or hardware. Instead, it is compiled (translated) into machine instructions for whatever OS and hardware (for example, Windows 98 on a Pentium IV) that you intend to run it on. Then that executable code can be loaded into memory and processed by your target system without further ado. Contrast this with an interpreted language like bash, where each instruction is read by a program which takes the appropriate actions, step by step. So instead of generating the machine-specific code in advance, the interpreter is deciding what to do on the fly.
As for your initial difficulty with the C program, the underlying problem can be generalized even further to say that you're speaking the wrong language. Bash understands bash code (yes you can program in it(!)), the C compiler understands C code. A similar situation would be pasting a few snippets of Python code into your PHP application; a program written to understand one language is not going to know what to do with code written in a different language.
[Quote] [Link]
gws said:
You've written a C program but are attempting to run it in bash. You need to compile it first, because bash does not understand C syntax. You might have better luck with csh, but the real right way is to compile it.
Save it to a file like 'myprogram.c' and then say
$ gcc -o myprogram myprogram.c
. Then you'll get an executable you can run:
$ ./myprogram
Bash is not executing your code, it's only telling the OS to load the program.

Oops. Heh. I guess
$ . myprogram.c
won't work for this. Thanks! I feel like a newbie.

Edit: Don't worry, I know what compilation and interpretation is.
[Quote] [Link]
It doesn't help that bash and C have some lexical elements in common; it makes the error messages misleading. # exists in both languages, but means different things. Same for { }, and I'm pretty sure printf comes predefined in bash, too.
[Quote] [Link]
Current Date: 13 Ineo 9:3Current Time: 6.8.33Join us in IRC...
Server: irc.esper.net
Channel: #nerdparadise
Your IP: 50.16.17.90Browser: UnknownBrowser Version: 0