Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

amnost

macrumors regular
Original poster
Feb 6, 2010
142
0
United States
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:
 
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.

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 :).
 
Cross-posting from his other topic:

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.
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).
 
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 :).

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 ;)
 
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;
}
 
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:
 
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.
 
Errow

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.

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:
 
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.
 
Here's the helloworld code as a txt file attachment. Download and rename to main.c.

B
 

Attachments

  • helloworld.txt
    80 bytes · Views: 89
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");
}
 
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.
 
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
 
I got a textbook on C. The introduction involves me doing almost the same instructions I was given to make a hello, world program. ...
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.
 
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.

...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
 
...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

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.
 
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.

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.
 
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.

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.
 
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.
 
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.

From another recent thread:

I hate to say it, but I recommend all beginner coders start at C/C++. It's much easier going from C/C++ to Java than the other way around. Java is also much easier in the sense some conceptual stuff is abstracted out which could be bad in let's say a low level whiteboard interview

pohtaytoe, pohtahtoe.

B
 
On the contrary, I use Xcode right now and I understand makefiles.

So do I, but you don't get that from anything you learn from Xcode.

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.

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.

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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.