"Hello World" Help for extreme beginner in C

Discussion in 'Mac Programming' started by Johnny5alive, Aug 18, 2011.

  1. Johnny5alive, Aug 18, 2011
    Last edited: Aug 18, 2011

    Johnny5alive macrumors newbie

    Joined:
    Aug 1, 2011
    #1
    Hi,

    I have never done anything with any programing. I am trying to create the standard "Hello World" first program in C. Here is what happens:

    1. I create the file in TextEdit and change it to plain text so I can save it as helloworld.c (have also used Text Wrangler and Xcode to create the file)

    2. I open terminal and type:

    gcc helloworld.c -o helloworld


    3. Terminal says:

    i686-apple-darwin11-llvm-gcc-4.2: try.c: No such file or directory
    i686-apple-darwin11-llvm-gcc-4.2: no input files

    I believe it is telling me the file does not exist? But it does. I have tried saving the .c file to multiple different locations all with the same outcome.

    By changing my command to "gcc helloworld.C -o hello world" I managed to get terminal to say:

    -bash: /Users/KevinsMacbookAir/Desktop/samp.c: Permission denied
    (I could not repeat this outcome, which makes me think I probably
    typed it wrong that time?)



    I am sure I am being foolish somehow but have been trying different approaches for almost two hours. If this is as easy as it gets, programming may not be for me.

    Any help is appreciated! Thanks.

    EDIT: try and samp were other files I made following the same guidelines.
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    You're definitely typing something wrong.

    I don't see try or samp in your command lines. Can you post a screenshot?

    There's a thread that describes how to do this including creating the file from the command line, so I'll try to find that for you.

    EDIT: Try to follow the directions in post #4 of http://forums.macrumors.com/showthread.php?p=11747938 exactly and post what happens.

    B
     
  3. chrono1081, Aug 18, 2011
    Last edited by a moderator: Aug 18, 2011

    chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #3
    Something doesn't sound right....

    Assuming your code looks like this:

    Code:
    #include <stdio.h>
    
    main()
    {
    
    printf("Hello World");
    }
    and you saved your file as something like hello.c, try this:


    Open up terminal. Make sure you are on the correct path to wherever you saved your file. If its on the desktop type in something like:

    cd /Users/username/Desktop (user name will be your login name and cd stands for "change directory" in case you didn't know).

    Once you do that, type into terminal the command ls

    ls will let you see the files in your folder that you are currently in. If you see your hello.c or whatever you named it, great.

    Now type:

    gcc -o HelloWorld hello.c and hit return.

    gcc is your compiler, I forget what -o does (maybe someone can chime on on that flag), HelloWorld is the name of your program (you can change this to whatever you wish) and hello.c is the name of the .c file you saved.

    Doing this should generate a little executable that is now on your desktop. If you click it your program should run.
     
  4. Johnny5alive thread starter macrumors newbie

    Joined:
    Aug 1, 2011
    #4
    Thanks for the help! Chrono you were right, I was looking for the file in the wrong place. I couldn't get the cd/users/username/desktop command to work but the basic ls command showed me what directory it was on and my file was not there. I moved the file and it worked fine.

    Balamw, the link you showed me worked as well but seemed more advanced. It cut out the whole word processor file saving step.

    Thanks again!
     
  5. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #5
    You'll definitely want to get familiar with the command line a bit. It'll be enormously beneficial and help you out a lot.

    Once you get comfortable too with the basics I'd say go back to Balamw's links since they will teach you plenty more and get you heading in the right direction.

    Once you do this stuff for a little bit it becomes much much easier.
     
  6. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    Yeah. That's exactly the point. Everything stays together that easy to rule out problems with your editor, current working directory and filenames... The steps in that post make sure you have a working compiler.

    This is something you will learn as you write more and more code. Separate variables. Take the problem apart to find your issues.

    B
     
  7. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #7
    It'd be much shorter to type:

    cd ~/Desktop

    Assuming of course that's where you placed the file.
     
  8. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #8
    Lol I even used that when testing the steps I sent him and never thought to include it.
     

Share This Page