Dumb C question.

Dumb C question.

Post by PlanetaryAlchemy on Sun Jan 09, 2011 1:20 am
([msg=51891]see Dumb C question.[/msg])

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;
}
I had a budgie once..
He was blue..
He said it was cause he was lonely.
PlanetaryAlchemy
New User
New User
 
Posts: 30
Joined: Sat Sep 19, 2009 4:03 am
Blog: View Blog (0)


Re: Dumb C question.

Post by nathandelane on Sun Jan 09, 2011 1:53 am
([msg=51893]see Re: Dumb C question.[/msg])

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;
Me, Nathandelane, Highly influential to Hackerdom, Premature Optimization=http://c2.com/cgi/wiki?PrematureOptimization
User avatar
nathandelane
Poster
Poster
 
Posts: 204
Joined: Thu Jun 26, 2008 11:26 am
Location: Utah
Blog: View Blog (0)


Re: Dumb C question.

Post by PlanetaryAlchemy on Sun Jan 09, 2011 2:35 am
([msg=51894]see Re: Dumb C question.[/msg])

You deserve a big slice of chocolate cake. Thanks for that!
I had a budgie once..
He was blue..
He said it was cause he was lonely.
PlanetaryAlchemy
New User
New User
 
Posts: 30
Joined: Sat Sep 19, 2009 4:03 am
Blog: View Blog (0)


Re: Dumb C question.

Post by s0fa on Sun Jan 09, 2011 3:14 am
([msg=51896]see Re: Dumb C question.[/msg])

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! ;)
"The individual is defined only by his relationship to the world and to other individuals; he exists only by transcending himself, and his freedom can be achieved only through the freedom of others." - Simone de Beauvoir
s0fa
New User
New User
 
Posts: 31
Joined: Tue Jan 04, 2011 2:12 pm
Blog: View Blog (0)


Re: Dumb C question.

Post by PlanetaryAlchemy on Sun Jan 09, 2011 3:57 am
([msg=51899]see Re: Dumb C question.[/msg])

Thanks for the advice s0fa.. I'm learning from http://www.learncpp.com. Any advice I can get will help I think.
I had a budgie once..
He was blue..
He said it was cause he was lonely.
PlanetaryAlchemy
New User
New User
 
Posts: 30
Joined: Sat Sep 19, 2009 4:03 am
Blog: View Blog (0)


Re: Dumb C question.

Post by fabianhjr on Sun Jan 09, 2011 12:13 pm
([msg=51919]see Re: Dumb C question.[/msg])

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. :)
Donate bitcoins to me! [1DhRP3hHgmSLQdRTZyT8VPTmzAj7Z2rsGA]
Dunno what bitcoins are? BitcoinMe
fabianhjr
Poster
Poster
 
Posts: 286
Joined: Tue Sep 21, 2010 7:48 pm
Blog: View Blog (0)


Re: Dumb C question.

Post by apples on Tue Jan 25, 2011 4:41 pm
([msg=52869]see Re: Dumb C question.[/msg])

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; }.
User avatar
apples
New User
New User
 
Posts: 37
Joined: Sat Apr 12, 2008 8:30 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests