Page 1 of 1

Dumb C question.

PostPosted: Sun Jan 09, 2011 1:20 am
by PlanetaryAlchemy
Hey all,

Apologies for the dumb question off the bat, I'm sure it's an easy answer. Trying to learn C, when I complete the program, save, chmod +x num.c, then try and run it, I get the error below.

./num.c: line 2: syntax error near unexpected token `('
./num.c: line 2: `int main()'

Am I getting this error as I haven't complied it first? Or is it a problem with how I've written the code?

Thanks

Code: Select all
#include <stdio.h>
int main()
{
        int Number1;    // 1
        int Number2;    // 3
        int Number3;    // 5

        Number1=13579   // 2
        Number2=2468    // 4

        sum=Number1+Number2;    //6
        printf("%d",sum);
        return 0;
}

Re: Dumb C question.

PostPosted: Sun Jan 09, 2011 1:53 am
by nathandelane
So first off, C is a compiled language, not a scripting language. This means that in order to run the program you must compile it into an executable. .c files are not inherently executable. Try running this in the same directory as your program file:

Code: Select all
gcc -o num num.c

Then to execute the program just do

Code: Select all
./num

Before this will work completely though you will need to declare your sum variable at the top of your main function like:

Code: Select all
int sum;

Re: Dumb C question.

PostPosted: Sun Jan 09, 2011 2:35 am
by PlanetaryAlchemy
You deserve a big slice of chocolate cake. Thanks for that!

Re: Dumb C question.

PostPosted: Sun Jan 09, 2011 3:14 am
by s0fa
Just some sort of tips you may not know (if you knew them before, just ignore my posting):
Code: Select all
        int Number1;    // 1
        int Number2;    // 3
        int Number3;    // 5


You may declare variables of the same logical datatype in one line separated by commas. That would look like that in your case:
Code: Select all
int Number1, Number2, Numer3;


Just to make it clear what the gcc - command makes: gcc stands for "GNU Compiler Collection" and it has the following syntax:
Code: Select all
gcc parameters


gcc is - of course - the call of the function itself. You may open it with some parameters to influence the act of compiling. -o is such a parameter and it defines the name of the compiled file (without this parameter it would just be a.out). A list of possible parameters is available on the develeoper homepage or on different other pages through the net. The last point is the name of the input-data and the output, the compiled program.

Keep on learning! ;)

Re: Dumb C question.

PostPosted: Sun Jan 09, 2011 3:57 am
by PlanetaryAlchemy
Thanks for the advice s0fa.. I'm learning from http://www.learncpp.com. Any advice I can get will help I think.

Re: Dumb C question.

PostPosted: Sun Jan 09, 2011 12:13 pm
by fabianhjr
Hey in C if any function has no arguments because of the standard it would look something like this:
Code: Select all
int main(void)
{
    //etc
}


Just a tip. :)

Re: Dumb C question.

PostPosted: Tue Jan 25, 2011 4:41 pm
by apples
nathandelane wrote:So first off, C is a compiled language, not a scripting language.


That's not true--C does not have to be compiled at all. The only restriction is that things must be executed according to C's abstract machine.

fabianhjr wrote:Hey in C if any function has no arguments because of the standard it would look something like this:<br>
Code: Select all
int main(void)<br>{<br>    //etc<br>}<br>
<br><br>Just a tip. :)


You forgot to include a return statement. Implicit returns are Not A Good Thing (TM). Also, an important distinction to make is that int main() { return 0; } is not the same thing as int main(void) { return 0; }.