What to do next for C programming. HELP!

Discussion in 'Mac Programming' started by amnost, Apr 25, 2010.

  1. amnost macrumors regular

    Joined:
    Feb 6, 2010
    Location:
    United States
    #1
    I have finished studying a little basic computer science architecture. I am now ready to start learning my first programing language. I have chosen C because it is one of the most important and most useful to know. I also heard that it is one of the best languages to learn as your first code language. I have found the worlds most popular book on how to program in C. Apparently it will work very well, and is fantastic for any skill level. Everything is good there. But there is one thing that confuses me greatly. Once I write up some code using the stuff I learn in this book, apparently in order for the computer to read it, it needs to be ran through a C compiler to make it executable. This is where everything gets fuzzy. I have the code, but I need to make it so the computer can read it and I can put it in a web page or Mac OS X app, or whatever it may be. I have learned that X Code can do these things. But, I am left unsure on how to do this. I have the entire Apple developer kit. I just don't know what to do upon opening the developer folder. I am unsure what is next. I have the C code, I just want to be able to make it so the computer can read it! I want to test my first basic C Code by just putting some test code onto my websites blog posts, and then removing it after I realize it works. So, basically I just want to take the C code I have, and make it so I can just use an HTML editor to put it on a website. But, this can't be done until the computer can read it, I am told.

    In summary, What exactly am I doing wrong and how do I do the things above? :confused: :confused: :confused: :confused: :confused: :confused:
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    You've got a long way to go I am afraid.

    Here is a very basic intro:

    1) Install Xcode. Download it from http://developer.apple.com/mac/ (needs registration).

    2) Open up any text editor (Text Wrangler, Smultron, Vim, Emacs are all commonly suggested and free).

    3) Insert the following code:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       printf("Hello, World\n");
       return 0;
    }
    and save it as main.c

    4) Open the Terminal application (/Applications/Utilities iirc)

    5) Navigate to the place you saved the file (if you saved it directly in your home folder you won't need to do anything here).

    6) Type
    Code:
    gcc main.c -o helloworld
    7) If any errors appear, read them and try and figure out what is wrong yourself.

    8) If you have fixed the errors and rerun the command in step 6 with no warnings being shown type
    Code:
    ./helloworld
    9) Marvel at your first ever computer program :).
     
  3. Objectivist-C macrumors 6502

    Joined:
    Jul 1, 2006
    #3
    Cross-posting from his other topic:

    You seem to have some fundamental misconceptions here that I'm not really sure how to address. Basically, you can certainly post your C source code to your blog, but you can't just plonk an actual application executable onto a webpage and have it run. Most interactive web page elements are done in Javascript, which is an interpreted language (but this is wandering off from the original topic of C).

    For getting started on programming with Xcode, I'd recommend "Learn C on the Mac" (available in print or pdf here). It'll handhold you through getting your programs running, and there are followup books to introduce you to Objective-C (what OS X development mostly uses) and Cocoa/Cocoa Touch (the frameworks provided by OS X and iPhoneOS).
     
  4. amnost thread starter macrumors regular

    Joined:
    Feb 6, 2010
    Location:
    United States
    #4
    That is awesome. However, I'll be honest, I can't figure out how to fix the problem! :( Will you please give me a hint? You or anyone can hint me ;)
     
  5. Objectivist-C macrumors 6502

    Joined:
    Jul 1, 2006
    #5
    He just showed you how to test your programs. You can't do it though a webpage, you need to take the C code you've written and "compile" it into something readable by a computer to run it, using something like Xcode.

    Try substituting your code at this point in the process:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       printf("Hello, World\n");
       return 0;
    }
     
  6. amnost thread starter macrumors regular

    Joined:
    Feb 6, 2010
    Location:
    United States
    #6
    Confused

    Huh? I thought all I needed to do was fix the errors in the code he gave me :confused:

    What do you mean "Try substituting your code at this point in the process"?
    :confused::confused::confused::confused::confused::confused::confused::confused::confused::confused::confused::confused::confused::confused:
     
  7. Objectivist-C macrumors 6502

    Joined:
    Jul 1, 2006
    #7
    The part I quoted is a simple C program (the traditional "Hello, World!"). He gave you instructions on how to take it and compile it (using a more direct route than Xcode). You can substitute your own programs there to see if they will compile and run. I really recommend you pick up the book I suggested, as it will explain things much more coherently than a gaggle of unorganized strangers on an Internet forum.
     
  8. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #8
    There are no errors. Just follow the instructions that I posted. The reason I mentioned errors was if you mis-typed what I had posted.
     
  9. amnost thread starter macrumors regular

    Joined:
    Feb 6, 2010
    Location:
    United States
    #9
    Errow

    I just copy & pasted it. But, when I paste this in: gcc main.c -o helloworld
    and click enter, I get the following error:
    main.c:1: error: expected identifier or ‘(’ before ‘{’ token
    main.c:1: error: stray ‘\’ in program
    main.c:1: error: stray ‘\’ in program
    main.c:1: error: stray ‘\’ in program
    main.c:1: error: stray ‘\’ in program
    main.c:1: error: stray ‘\’ in program
    main.c:2: error: stray ‘\’ in program
    main.c:2: error: stray ‘\’ in program
    main.c:2: error: stray ‘\’ in program
    main.c:2: error: stray ‘\’ in program
    main.c:3: error: stray ‘\’ in program
    main.c:3: error: stray ‘\’ in program
    main.c:3: error: stray ‘\’ in program
    main.c:3: error: stray ‘\’ in program
    main.c:3: error: stray ‘\’ in program
    main.c:3: error: stray ‘\’ in program
    main.c:3: error: stray ‘\’ in program
    main.c:4: error: stray ‘\’ in program
    main.c:4: error: stray ‘\’ in program
    main.c:4: error: stray ‘\’ in program
    main.c:4: error: stray ‘\’ in program
    main.c:4: error: stray ‘\’ in program
    main.c:5: error: stray ‘\’ in program
    main.c:6: error: stray ‘\’ in program
    main.c:6: error: stray ‘\’ in program
    main.c:6: error: stray ‘\’ in program
    main.c:6: error: stray ‘\’ in program
    main.c:8: error: stray ‘\’ in program
    main.c:8: error: stray ‘\’ in program
    main.c:8: error: stray ‘\’ in program
    main.c:8: error: stray ‘\’ in program
    main.c:8: error: stray ‘#’ in program
    main.c:8:38: warning: backslash-newline at end of file
    main.c:8: error: stray ‘\’ in program
    main.c:8: error: stray ‘\’ in program

    What happened? I followed you instructions 100%, down to every part. :confused:
     
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #10
    It looks like you didn't save the file as plain text. Judging by the proliferation of backslashes, it's probably been saved as RTF.

    Open the file again. Choose Make Plain Text from the Format menu, if you're using TextEdit.app. Then Save as plain text.

    If you're not using TextEdit.app, then tell us exactly what editor you're using.
     
  11. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #11
    Here's the helloworld code as a txt file attachment. Download and rename to main.c.

    B
     

    Attached Files:

  12. amnost thread starter macrumors regular

    Joined:
    Feb 6, 2010
    Location:
    United States
    #12
    a.out

    I ended up getting the hello world program working by changing it to plain text as you guys said. thanks.

    --

    I got a textbook on C. The introduction involves me doing almost the same instructions I was given to make a hello, world program. Except, this one has me make a file called hello.c
    It says to compile it I just needed to enter cc hello.c
    like I did before
    But it says, by doing that, I have created an executable file called a.out
    It said if I enter a.out it will then print "hello, world"

    It did not. :confused:

    Instead it just said:
    -bash: a.out: command not found


    What do I do instead?

    By the way, the hello.c file has the following code in it:

    #include <stdio.h>

    main ( )
    {
    printf ("hello, world\n");
    }
     
  13. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #13
    Did you list the files in the directory to confirm that the file a.out exists?

    Does "cc" work as a compiler command? (Do you need to use gcc instead?)

    Did you prefix the a.out command with a "." to force the shell to run from the current directory? Try that. Type "./a.out" (without the quotes) and see if that makes a difference.
     
  14. amnost thread starter macrumors regular

    Joined:
    Feb 6, 2010
    Location:
    United States
  15. sollyfitz macrumors newbie

    Joined:
    Apr 30, 2010
    #15
    registration as Developer $90?

    xCode is supposed to be free, but we have to register as a developer for $90? I just want to make sure I am getting this right before I plonk down my cash.

    Thanks
     
  16. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #16
    It is free. So is registration. You only need to pay if you want the extra features of a paid account.
     
  17. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #17
  18. Thomas Harte macrumors 6502

    Joined:
    Nov 30, 2005
    #18
    Would that be Kernighan & Ritchie by any chance?

    I'm not sure why nobody has suggested you just use the IDE provided with the Xcode tools, itself also called Xcode. If you installed the latest version to the normal place, launch /Developer/Applications/Xcode and when creating a new project choose Mac OS X, Application, Command Line Tool. You'll get a new project with a "Hello World" program already in it. You can do all your editing right there, click a single button to build and run and, when you're ready for it, have a neat way to organise multiple files and graphically debug.
     
  19. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #19
    ...and have no idea what's actually going on, then find yourself crippled whenever you're on a machine that lacks XCode. I think it's best to start simple, and only move to an IDE when you know how to build big projects with makefiles, etc. and really need the small stuff handled for you.

    -Lee
     
  20. Thomas Harte macrumors 6502

    Joined:
    Nov 30, 2005
    #20
    Oh, well I think the other way around, that there's no point learning makefiles and stuff until you understand the various phases of the build system. I can see both sides of the argument, naturally, but I'm definitely from the top down school of learning rather than bottom up.

    I also don't think anybody who never strayed beyond Xcode would seriously expect to know what they're doing without Xcode.
     
  21. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #21
    Which you'll never do if you use Xcode or any other IDE. If you rely on the IDE then the act of linking appears to be black magic, in other words "it just works". Whilst this maybe good for consumer apps, development wise it removes your understanding of the process.
     
  22. Thomas Harte macrumors 6502

    Joined:
    Nov 30, 2005
    #22
    On the contrary, I use Xcode right now and I understand makefiles.

    From the original poster's hello world program and the problem they faced, I'm going to go beyond asking and sate that I'm completely certain they're working from Kernighan & Ritchie — he's used a declaration of main that leaves the return type and arguments implicit (which you don't see often now for style reasons), named the file hello.c and failed to appreciate that just 'a.out' won't run the program, which having checked my copy of K&R Second Edition is exactly what you'd get from page 6.

    The layout of that book is Chapter 1 where a whole load of complete programs are specified with very brief exposition on how they work. Subsequent chapters actually explain the language details in depth and from the ground up. The book actually never discusses makefiles because they aren't part of the language.

    I therefore think it's acceptable to say "use the IDE first" because:
    1. it matches the teaching order of Kernighan & Ritchie — get things working first, learn how they work second;
    2. the resource he's using actually never covers makefiles or the command line.

    Will the original poster never learn how to use printf if someone tells him that the way to print "Hello, world" is printf("Hello, world"); rather than starting with the way strings are stored, how to declare a string literal and the printf escape sequences?

    By all means disagree but don't pretend that I'm unavoidably wrong. It's just a different learning style, and I happen to think that mine better matches the definitive text on the language.
     
  23. Rhalliwell1 macrumors 6502a

    Joined:
    May 22, 2008
    #23
    not to put a downer on what your doing but starting your programming career with C is going to be difficult. Most people start with Java.
     
  24. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #24
    From another recent thread:

    pohtaytoe, pohtahtoe.

    B
     
  25. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #25
    So do I, but you don't get that from anything you learn from Xcode.

    K&R is a poor introductory book if you ask me. Pointers on C was the book that I learnt C with and it was pretty good.

    I didn't. I think you are reading more into what I typed than was actually there. Starting out in Xcode has many disadvantages compared to the command line. For one, it is an immensely complex program. When you are learning a language you want to dedicate all your brain power to the language, not figuring out Xcode's sometimes odd way of doing things. I still have problems statically linking libraries in Xcode! It takes about 10 seconds using a makefile.

    Having said that, you'll still need to learn the command line whatever you do. The advantage though of doing it at the beginning is that it is a transferable skill. You can do the same on Linux, FreeBSD, AIX, Solaris, HP UX etc etc.

    You can't with Xcode.
     

Share This Page