View Full Version : Trying to learn C
RustyBoltz
Jan 5, 2009, 07:06 PM
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.
Revelation78
Jan 5, 2009, 07:16 PM
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.
Me1000
Jan 5, 2009, 07:22 PM
perhaps try adding
#import <stdio.h>
to the beginning of the file?
That would be my guess...
HiRez
Jan 5, 2009, 07:25 PM
Yeah, you type
gcc ~/Desktop/welcome.c
or
cd ~/Desktop
gcc welcome.c
When it successfully compiles, type
./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:
#include <stdio.h>
RustyBoltz
Jan 5, 2009, 08:08 PM
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.
HiRez
Jan 5, 2009, 08:30 PM
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:
gcc welcome.c -o welcome
Now your executable (application) is named welcome and you can run it with:
./welcome
(or name it whatever you want). from the same directory, or if you're not with:
~/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.
Nicolaius
Jan 5, 2009, 08:52 PM
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.
North Bronson
Jan 5, 2009, 08:58 PM
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.
RustyBoltz
Jan 5, 2009, 10:05 PM
Wow, thanks for all the help. I was wondering how Xcode works in all this so thanks. I'll be sure to come back if I run into anymore problems.
Sander
Jan 7, 2009, 09:45 AM
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!
RustyBoltz
Feb 2, 2009, 01:31 PM
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
Sander
Feb 2, 2009, 01:49 PM
C has the unary minus operator, like just any other programming language I know. In other words: "-b" is valid C.
tom.
Feb 2, 2009, 02:45 PM
You may find this useful:
http://www.cprogramming.com/tutorial.html
Enjoy!
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.