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

RustyBoltz

macrumors newbie
Original poster
Jan 5, 2009
4
0
Lucas/Arlington, Texas
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.
 
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.
 
perhaps try adding

#import <stdio.h>

to the beginning of the file?
That would be my guess...
 
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>
 
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.
 
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.
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.
 
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.
 
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.

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.
 
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!
 
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
 
C has the unary minus operator, like just any other programming language I know. In other words: "-b" is valid C.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.