Trying to learn C

Discussion in 'Mac Programming' started by RustyBoltz, Jan 5, 2009.

  1. RustyBoltz macrumors newbie

    Joined:
    Jan 5, 2009
    Location:
    Lucas/Arlington, Texas
    #1
    I hesitate writing this but I've spend the last two weeks stuck in the same spot.
    I am trying to teach myself C to prepare myself for my numerical analysis and programming course. I used Text Wrangler to write a simple program

    /* A first program in C */

    main()
    {
    printf("Welcome to C!\n");
    }

    and saved it as welcome.c on my desktop. I am lost from there. I have installed the xcode package off of apple's website to get the gcc but can't get anywhere with it. I get this

    Last login: Mon Jan 5 17:43:14 on ttys002
    Nathans-Computer:~ Nathan$ gcc
    i686-apple-darwin9-gcc-4.0.1: no input files
    Nathans-Computer:~ Nathan$

    and then this

    Nathans-Computer:~ Nathan$ gcc /Users/nathan/Desktop/welcome.c
    /Users/nathan/Desktop/welcome.c: In function ‘main’:
    /Users/nathan/Desktop/welcome.c:5: warning: incompatible implicit declaration of built-in function ‘printf’
    Nathans-Computer:~ Nathan$

    Maybe i'm just confused on what I'm supposed to do. I have no problem with the code, I just get lost at the compile step.
    Thank you for any help and your patience.
     
  2. Revelation78 macrumors 68000

    Revelation78

    Joined:
    Dec 18, 2008
    Location:
    North Carolina
    #2
    Do you have a compiler? If you don't, I'd recommend hitting MSDN and getting Visual Express products, they're free and include compilers, debuggers and everything else you could possibly need.
     
  3. Me1000 macrumors 68000

    Me1000

    Joined:
    Jul 15, 2006
    #3
    perhaps try adding

    #import <stdio.h>

    to the beginning of the file?
    That would be my guess...
     
  4. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #4
    Yeah, you type

    Code:
    gcc ~/Desktop/welcome.c
    or

    Code:
    cd ~/Desktop
    gcc welcome.c
    When it successfully compiles, type

    Code:
    ./a.out
    from the Desktop folder to run it.

    The compiler error is because you need to include the standard C I/O library at the top of your source code to use printf:

    Code:
    #include <stdio.h>
     
  5. RustyBoltz thread starter macrumors newbie

    Joined:
    Jan 5, 2009
    Location:
    Lucas/Arlington, Texas
    #5
    Awesome! Wow, what a relief, guess it figures something be wrong since i'm working out of a 1992 book of my father's.
    So I get
    Last login: Mon Jan 5 19:56:49 on ttys000
    Nathan-Rusterholtzs-Computer:~ Nathan$ gcc ~/Desktop/welcome.c
    Nathan-Rusterholtzs-Computer:~ Nathan$ ./a.out
    Welcome to C!
    Nathan-Rusterholtzs-Computer:~ Nathan$

    Is that it? It just compiles and runs the program in Terminal? No new files on the Desktop? This is all new to me b/c I'm used to working in HTML where you write some code and you get a file that you can view.
    Thanks for all the help. It's surprising that it was that simple.
     
  6. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #6
    It does create a new file, by default it's named a.out (If you view the contents of your Desktop in Finder you should see it). With a little extra typing you can name it something more readable (and less confusing if you're working on multiple projects) by using the -o flag:

    Code:
    gcc welcome.c -o welcome
    Now your executable (application) is named welcome and you can run it with:

    Code:
    ./welcome
    (or name it whatever you want). from the same directory, or if you're not with:

    Code:
    ~/Desktop/welcome
    One thing on OS X though. You will find a lot of Mac applications have a .app extension (eg. Photoshop.app). DO NOT add that extension to your simple ANSI C projects. The .app extension implies that the project is contained in a special folder (bundle), so this could very well cause problems.

    Anyway, glad it's working for you.
     
  7. Nicolaius macrumors newbie

    Joined:
    Jan 4, 2009
    #7
    do you know how to use xcode? Just open it up and go to file...create new project, cocoa application. Type in a name, then go to file, add file...then click c file. Then copy the code you have and you should be able to run it from xcode.
     
  8. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #8
    Well, I don't really think you should load all the Cocoa frameworks if you're just programming in C.

    Just try choosing Command Line Utility --> Standard Tool from the Template Chooser.
     
  9. RustyBoltz thread starter macrumors newbie

    Joined:
    Jan 5, 2009
    Location:
    Lucas/Arlington, Texas
    #9
    Wow, thanks for all the help. I was wondering how Xcode works in all this so thanks. I'll be sure to come back if I run into anymore problems.
     
  10. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #10
    A few notes just so you don't get off on the wrong foot.

    #import is Objective-C; plain C uses "#include". Also, main() should return an int.

    Have fun programming!
     
  11. RustyBoltz thread starter macrumors newbie

    Joined:
    Jan 5, 2009
    Location:
    Lucas/Arlington, Texas
    #11
    I'm back, my programming class has started but my teacher speaks very poor english so I will mostly be learning from a book and what I can find on the internet.
    One problem I ran into is while writing a program the solves a quadratic equation for 'x', I cannot find the function to negate a number such as -b. any suggestions?
    Thanks
     
  12. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #12
    C has the unary minus operator, like just any other programming language I know. In other words: "-b" is valid C.
     
  13. tom. macrumors 6502

    tom.

    Joined:
    Nov 9, 2007
    Location:
    UK

Share This Page