Whats Wrong with?

Discussion in 'Mac Programming' started by smogsy, Oct 6, 2008.

  1. smogsy macrumors 6502a

    Joined:
    Jan 8, 2008
    #1
    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’


    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!
     
  2. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #2
    need a semicolon after the variable assignment...

    my = 4 * 6;
     
  3. NT1440 macrumors G4

    NT1440

    Joined:
    May 18, 2008
    Location:
    Hartford, CT
    #3
    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 =)
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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.
     
  5. brn2ski00 macrumors 68020

    brn2ski00

    Joined:
    Aug 16, 2007
    #5
    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.
     
  6. NT1440 macrumors G4

    NT1440

    Joined:
    May 18, 2008
    Location:
    Hartford, CT
    #6
    OP, id brush up on your indentation guidelines, itll make everything much more structured.
     
  7. smogsy thread starter macrumors 6502a

    Joined:
    Jan 8, 2008
    #7
    dont worry its indented but copy/paste jobbie got rid of them lol i knew it was somthing silly!
     
  8. NT1440 macrumors G4

    NT1440

    Joined:
    May 18, 2008
    Location:
    Hartford, CT
    #8
    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:(
     
  9. smogsy thread starter macrumors 6502a

    Joined:
    Jan 8, 2008
    #9
    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
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    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
     
  11. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  12. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #12
    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.
     

Share This Page