Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

smogsy

macrumors 6502a
Original poster
Jan 8, 2008
592
2
i just started learning C today and i created this but
get this error on gcc compiling
test4.c:10: error: syntax error before ‘printf’


#include <stdio.h>

int main ()
{
int my;

my = 4 * 6

printf("declare my next" );

return 0;
}
i was just playing around with C i wanted it run as tell me the answer i know i kinda got it right and just missed something silly , but it does not work :eek:

thank please explain why i got it wrong to so i can learn from my mistakes

SEE BOTTOM OF THREAD!
 

NT1440

macrumors Pentium
May 18, 2008
15,093
22,159
i only know very basic java as im learning in school, but it looks like you missed a ";" after "my = 4 * 6"

I could be wrong tho. Good luck

Edit: beat to it, but i got it right =)
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
statements must end with ;

Code:
my = 4 * 6

should be

Code:
my = 4 * 6;

I personally would prefer:
Code:
int main() {
was:
Code:
int main(int argc, char *argv[]) {
or
Code:
int main(void) {
but others might disagree.

-Lee

Edit: Damn! We know our semicolons around here.
 

brn2ski00

macrumors 68020
Aug 16, 2007
2,242
14
MA
Lol, yes we sure do!

Also, as far as the main function declaration, its not a bad idea to add arguments to the call -- just like stated above. This way, you can use the argument vectors later down the road if need be. Its just a nice standard practice and will not effect your code in any way.
 

NT1440

macrumors Pentium
May 18, 2008
15,093
22,159
OP, id brush up on your indentation guidelines, itll make everything much more structured.
 

smogsy

macrumors 6502a
Original poster
Jan 8, 2008
592
2
dont worry its indented but copy/paste jobbie got rid of them lol i knew it was somthing silly!
 

NT1440

macrumors Pentium
May 18, 2008
15,093
22,159
dont worry its indented but copy/paste jobbie got rid of them lol i knew it was somthing silly!

you'll come to learn that its the little things that take most of your time. I had an EASY assignment for my class once. THREE hours later I gave up and had to come to my teacher for help the next day. The problem? I had placed a semi colon at the beginning of a while statement:(
 

smogsy

macrumors 6502a
Original poster
Jan 8, 2008
592
2
hey

hey guys i hope you can help me im on lesson to now :D variables..

i learnt how to declare a variable and the uses of variables but i would like to know to use it in a code for my own personal knowledge and satisfaction.

how do i get the Result of my = 4*6 to be displayed after Declare my Next Number To Be

my code is
#include <stdio.h>

int main () {
int my;

my = 4 * 6;

printf("declare my next Number To Be:");

HERE i want it to display the answer of "my"

return 0;
}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I'm sure your book has them as well, but here are the printf and scanf format specifiers:
http://irc.essex.ac.uk/www.iota-six.co.uk/c/c2_printf_and_scanf.asp

You're printing a 4-byte signed integer, so use %i or %d. The print line would look like this:
Code:
printf("declare my next Number To Be: %i\n",my);

printf is a variable-argument function. You need to pass a variable for each format specifier that appears in the format string. You also have to make sure the types are correct, as this is an easy way to end up with a crash.

-Lee
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
I personally would prefer:
Code:
int main() {
was:
Code:
int main(int argc, char *argv[]) {
I think that's the Xcode default when you create a "Standard Tool" project. Since it's meant to be run from the command-line, it makes sense to accept arguments.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.