Hello World! issues with learning C :(

Discussion in 'Mac Programming' started by brent0saurus, Aug 6, 2008.

  1. brent0saurus macrumors regular

    brent0saurus

    Joined:
    Feb 16, 2006
    #1
    i'm trying to get into programming as i love os x and have a lot of great ideas for apps. i've been working all day on learning the very beginning and am trying to run the "Hello World" program but can't seem to do it.
    here's what i've done so far:
    went to this site : http://www.learn-programming.za.net/programming_c_learn01.html

    followed the directions exactly, typed the code into aquamacs emacs which i guess is just a nicer looking version of emacs? that's my understanding at least. then i "save buffer as" hello.c like it tells you to and then type the "$cc hello.c -ohello" command into Terminal and it says command not found.

    these are my conditions:
    i have hello.c saved on my desktop
    i have installed xcode from the adc site. i also instealled gcc from the custom install options and when i type cc into terminal to make sure i have gcc i get: powerpc-apple-darwin8-gcc-4.0.1: no input files

    please help! i've been working literally from 10 am to 5 pm (i'm pathetically slow) and i'm getting very frustrated.
    thanks for any info you can give me!

    p.s. i'm also looking into buying "Cocoa Programming for Mac OS X" and "The C Programming Language" by kernighan and ritchie but before i spend 50 dollars per book (poor college student) i want to see how far i can get with the plethora of info on the internet.
    thanks in advance

    brent
     
  2. TEG macrumors 604

    TEG

    Joined:
    Jan 21, 2002
    Location:
    Langley, Washington
    #2
    Use 'gcc' as the C compiler. CC is the official C compiler, 'gcc' is the GNU C Compilier and is free. Also there should be a space between the -o and the output file name. Then run it with ./hello

    TEG
     
  3. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    Type gcc -o hello hello.c

    That will create an executable file called hello from the source code file hello.c. Change it to your needs.

    Run the hello program by typing "./hello" (exactly as shown without the quotes).
     
  4. brent0saurus thread starter macrumors regular

    brent0saurus

    Joined:
    Feb 16, 2006
    #4
    when i type that i get: powerpc-apple-darwin8-gcc-4.0.1: hello.c: No such file or directory.
    and how exactly do i install gcc? i've gone to the site and i don't understand it at all. i just found a bunch of mirrors that are hosting a million different folders.

    am i supposed to have the file saved in a certain place? i have a very very minimal understanding of this whole process but how does terminal know where to find hello.c unless i tell it where it is?
     
  5. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    Just install the latest version of Xcode (3.1 if you are running Leopard, 2.5 if you are running Tiger). No need for anything else.

    Well you need to make sure you are in the same directory as the file when you use the terminal. Type this to change directory:

    cd /Users/myusername/Documents/blah
     
  6. brent0saurus thread starter macrumors regular

    brent0saurus

    Joined:
    Feb 16, 2006
    #6
    okay so i just typed "cd /users/brent/desktop and terminal came back with "new-host:/users/brent/desktop brent$" and then i type "$cc hello.c -ohello" and again i get "-bash: hello.c: command not found

    :(

    i must be doing something wrong
     
  7. TEG macrumors 604

    TEG

    Joined:
    Jan 21, 2002
    Location:
    Langley, Washington
    #7
    You do have to run CC or GCC within the same folder as your hello.c file. Within the folder containing hello.c type the following

    gcc -o hello hello.c

    That should work, otherwise

    gcc hello.c -o hello should work

    (I can never remember where attributes are supposed to be in command lines. So all the applications I write will accept them in any order.)

    TEG
     
  8. brent0saurus thread starter macrumors regular

    brent0saurus

    Joined:
    Feb 16, 2006
    #8
    IT WORKED! i'm an idiot and though i still had the file on my desktop but i moved it into my user folder so i changed to the wrong directory...

    i guess i'm getting somewhere now.

    still not sure if i need a different compiler or how to install gcc. i guess what i have is working so far? but could you guys recommend something that is better? and what text editors do you use? aquamacs seems to be fine for me so far, but... all i've done is gotten terminal to say "Hello World" haha
     
  9. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    You already have gcc. It is the compiler that Xcode uses.
     
  10. daflake macrumors 6502a

    Joined:
    Apr 8, 2008
    #10
    Sounds to me like you have not installed the compiler. I did not find one on my default machine (other than Java) so you probably need to install it. From what I read you can get it from the XCode Tools folder on install DVD.

    I found this link on Apple that might help you a bit more.

    http://discussions.apple.com/thread.jspa?messageID=5076895


    LOL, guess I was a bit slow at posting. :) Glad you got it working. Congrats on your first program.
     
  11. weg macrumors 6502a

    weg

    Joined:
    Mar 29, 2004
    Location:
    nj
    #11
    Before you start writing applications, there are a few other things that you have to understand.
    1.) Get used to the command line (other people might disagree, suggesting that you should use XCode and avoid the shell -- however, I believe that the "bottom-up approach" is much better when you learn programming). If you type in $cc, the shell assumes that you're refering to an environment variable. If there is no such environment variable (i.e., one named cc), then $cc will evaluate to the empty string, and your command turns out to be "hello.c -ohello". Since hello.c is not an executable, the shell fails to execute it and tells you that there's no such program.
    2.) To find out whether gcc is installed, type in "which gcc" in your shell. That command will tell you where and whether the gcc compiler is installed.

    Yes, but that won't change. No matter how experienced you are as a programmer, you'll always spend a lot of time trying to find out what you are currently doing wrong :D
     
  12. TEG macrumors 604

    TEG

    Joined:
    Jan 21, 2002
    Location:
    Langley, Washington
    #12
    As my former boss said, "You spend 80% of your time doing the final 20% of the work."

    I also echo the "use the shell/terminal" sentiment. You will understand what you are doing much more if you start there.

    TEG
     
  13. daflake macrumors 6502a

    Joined:
    Apr 8, 2008
    #13
    I agree with the above and still have no clue why some of the projects that I have written don't work right. :eek::p:eek:
     
  14. brent0saurus thread starter macrumors regular

    brent0saurus

    Joined:
    Feb 16, 2006
    #14


    i understand everything you said except "If you type in $cc, the shell assumes that you're refering to an environment variable. If there is no such environment variable (i.e., one named cc), then $cc will evaluate to the empty string, and your command turns out to be "hello.c -ohello". Since hello.c is not an executable, the shell fails to execute it and tells you that there's no such program. "

    environment variable? i guess the issue is i'm not really sure what i'm doing when i type cc. i'm doing it because the tutorial told me to...
    could you further explain?

    thanks SO much for all of your help. everyone.
     
  15. TEG macrumors 604

    TEG

    Joined:
    Jan 21, 2002
    Location:
    Langley, Washington
    #15
    Because cc doesn't exist in your folder, the system checks the program name against its list of paths and sees if it exists there. If is is not there, but you are using $cc, then the system ignores the $cc, and instead tries to run the program "hello.c". Hello.c is not a valid application, so it fails.

    TEG
     
  16. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #16
    cc is actually a symlink in Mac OS X to gcc so it will work correctly.
     
  17. brent0saurus thread starter macrumors regular

    brent0saurus

    Joined:
    Feb 16, 2006
    #17
    symlink?
    i'm so lost.
     
  18. aross99 macrumors 68000

    aross99

    Joined:
    Dec 17, 2006
    Location:
    East Lansing, MI
    #18
    A symlink (symbolic link) is like a synonym for another file - like a copy, only it does not take up any disk space.

    What they are trying to say is that when you type "cc", the system sees that it is a symlink to gcc, and really runs gcc, even though you typed cc. It's like two different ways to run the same thing.

    When you work in terminal, you are really work in something called a "shell". A Shell is like a programming language with variables, and commands. You can combine these variables and commends into program text files called "shell scripts" or "shells". You can also enter shell commands at the shell prompt, which is how you usually work with terminal.

    In your previous posts, it looks like you have been typing "$cc" as your terminal command. When you enter a command with a $ in it, the system (usually) thinks that this is a variable named "cc" and it is going to replace the value if that variable before it processes your command. Since you didn't define "cc", it replaces the "$cc" in your command with the blank value, and continues processing your command with the next parameter.

    If you type:

    $cc hello.c -ohello

    The system actually processes this as:

    hello.c -ohello

    since it has replaced the "$cc" with a blank string. hello.c is not an executable program, so you get the error message.

    Your problems here seem to have been:

    1. Your source file was not in the directory you were working in when you issued the cc command.
    2. You were typing "$cc" when you should have been typing "cc". The $ in the examples you see was probably supposed to be the terminal prompt, and you were not supposed to enter that info.
    In a nutshell, if your hello.c file was on your desktop, you should have used these commands in terminal:

    cd /users/brent/desktop
    cc hello.c -o hello

    and to run the program:

    ./hello
     
  19. WebMongol macrumors member

    Joined:
    Sep 19, 2004
    Location:
    Bay Area, CA
    #19
    UNIX programming environment

    Reading book about UNIX programming environment would really help you.

    - Unix Programming Environment by Brian W. Kernighan, Rob Pike (classic written 25-years ago but may be a bit difficult for starters)

    - UNIX for Programmers and Users (3rd Edition) by Graham Glass, King Ables (good text covering UNIX concepts, basics, UNIX for programmers and sys admins)

    - Learning Unix for Mac OS X Tiger by Dave Taylor (short Mac OS X specific introduction)

    A few useful UNIX commands:

    pwd - print current directory

    cd <dir> - change current directory
    cd ~/Desktop

    ls -- list directory contents
    ls -l
    ls -lt ~/Desktop

    find <dir> -name <filename> - find file <filename> starting at directory <dir>
    find ~ -name hello.c

    which <cmd> - locate a program file
    which gcc

    emacs <file-name> -
    emacs hello.c

    man <cmd> - show help for command <cmd>
    man ls
    man which

    ./cmd - invoke command <cmd> located in current directory
    ./hello

    Instead of paying $50 for UNIX book you can start with online tutorial:

    Mac OS X Unix Tutorial: http://www.osxfaq.com/tutorials/LearningCenter/

    Prof Matloff's UNIX tutorial:
    http://heather.cs.ucdavis.edu/~matloff/unix.html
    Matloff has a series of tutorials about UNIX, editors (vim, emacs), C, etc

    Gentle introduction to UNIX:
    http://unix.t-a-y-l-o-r.com/

    Good luck.
     
  20. trule macrumors 6502

    Joined:
    Mar 16, 2007
    #20
    Whilst I understand why some people are recommending the bottom up approach might I suggest you start at the top with XCode. Even if you don't understand every small detail at first you will at least be able to build some cool apps fast. Then you get a better idea of what is involved and can find out if you really want to be an app developer.

    Working from the bottom up you only really learn about C/C++ and compliers, from the top down you get to learn about GUI design, databases, internet stuff and interfacing with useful library code that gives to access to all the cool mac user technology. The high level stuff is, initially, also easier to learn than the low level stuff.

    There are plenty of walk-throughs and tutorials, and a few good books on getting started with XCode.
     
  21. newb16 macrumors regular

    Joined:
    Feb 27, 2008
    #21
    This tutorial sucks at least. Just went to 'strings' section:
    Code:
    s1 = "abc";
    s2 = "xyz";
    strcat(s1,s2); // s1 = "abcxyz" 
    
    This is an strcat example. If you copy it verbatim (adding char*s1, *s2 declarations ) and try to run, it most likely will bork as s1 points to pre-allocated string of 4 ( including trailing 0 ) bytes and strcat will overwrite this zero and memory after it. It crashed in strcat for me ( vs2005). It is not surprising as they have a bunch of other tutorials 'learn in 24 hours' on the same yellow sidebar with no information about author whatsoever. Buy k&r book instead.

    ETA-
    Or in 'decision' section , about 'switch' statement:
    ...If you use more than one command then you need to remember to group them between curly brackets. ( which is not true for switch)
    and right after that they write code with multiple statements and no curly brackets.
    ( and forget to mention 'break' keyword totally - one have to discover why it is required himself, after some debugging )

    ETA2 - 'loops' section
    ...solution to the problem that we had with the 24 printf commands:
    Code:
    for (i = 1;i == 24;i++)
          printf("H\n");
    
    ...You can also say ++i or --i and the difference is that with ++i the 1 is added before the for loop tests if i == 24 and with i++ 1 is added to i after the loop has tested if i == 24

    o.m.f.g. what else can I say
     
  22. brent0saurus thread starter macrumors regular

    brent0saurus

    Joined:
    Feb 16, 2006
    #22
    thank you SO much. this makes a lot of sense and will probably make even more sense the more i keep practicing. thanks again!
     
  23. brent0saurus thread starter macrumors regular

    brent0saurus

    Joined:
    Feb 16, 2006
    #23

    THANK YOU! these look like they'll help.
    i've been told by another user that the tutorial i'm using is bad. seems like there are a lot of errors.
    so thanks for pointing that out newb16

    again, thank you everyone for all of your input!!!
    god i love this forum.
     
  24. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #24
    My goodness. Steer clear from this stuff indeed.
     
  25. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #25
    This has to be the longest thread about "Hello, World!" i've ever seen. =)

    At least we've established that the source material was horribly misleading at this point.

    -Lee
     

Share This Page