Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 17, 2008, 09:39 PM   #1
j03
macrumors newbie
 
Join Date: Mar 2008
Best way to start learning C

Hey you all,

This is my first post on these forums. I need to start learning C , what do you think the best way to program in C on the macbook (not pro) is? I have xcode, ,but is there a better way to go about learning other than using a IDE or all in one program?

Thanks all!

j03
j03 is offline   0 Reply With Quote
Old Mar 17, 2008, 10:11 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Get a vim cheatsheet, use that to edit your source files.

Code:
#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("Hello World!\n");
  return 0;
}
save it as helloworld.c, then compile:
gcc -o helloworld helloworld.c
./helloworld

XCode can handle C fine, but you will find vi(m) on most any unix-like system, and likely have cc or gcc available for compilation.

Good luck! Have fun!

-Lee
lee1210 is offline   0 Reply With Quote
Old Mar 18, 2008, 08:46 AM   #3
j03
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
Quote:
Originally Posted by lee1210 View Post
Get a vim cheatsheet, use that to edit your source files.

save it as helloworld.c, then compile:
gcc -o helloworld helloworld.c
./helloworld

-Lee
What is a VIM cheatsheet and how do i do :
Quote:
gcc -o helloworld helloworld.c
./helloworld
j03 is offline   0 Reply With Quote
Old Mar 18, 2008, 09:04 AM   #4
yeroen
macrumors 6502a
 
yeroen's Avatar
 
Join Date: Mar 2007
Location: Cambridge, MA
open Terminal.app and start typing

(Alongside C, you should also learn the OS X unix environment. The terminal provides a shell to the OS. Once you learn your way around the shell, you'll find it to be the most powerful tool at your disposal)

vim is a "modal" text editor found on all unix systems: http://en.wikipedia.org/wiki/Vim_%28text_editor%29

A vim cheatsheet is a list of all the commonly used keystrokes. It's a bit unintuitive at first, but the effort spent in learning vi (or emacs) pays off handsomely in the long term.

Alternatively you could just skip vim for now, and edit your C-source in any text editor of your choice (like TextEdit.app).
__________________
all Jarvis, all the time

Last edited by yeroen; Mar 20, 2008 at 06:11 PM.
yeroen is offline   0 Reply With Quote
Old Mar 18, 2008, 09:52 PM   #5
Muncher
macrumors 65816
 
Muncher's Avatar
 
Join Date: Apr 2007
Location: California
Quote:
Originally Posted by j03 View Post
Hey you all,

This is my first post on these forums. I need to start learning C , what do you think the best way to program in C on the macbook (not pro) is? I have xcode, ,but is there a better way to go about learning other than using a IDE or all in one program?

Thanks all!

j03
I strongly recommend Xcode. It takes care of just about everything for a standard command-line program for you.

Just go to File > New Project > Command Line Utility > Standard Tool

Then just do whatever you wanted to do.
__________________
2.4Ghz 20" Rev. A iMac, Wii, crimson DS, intuos3 4x6, PS3
iPod touch 2G 16GB > Fiio E1 > Hifiman RE0
Muncher is offline   0 Reply With Quote
Old Mar 18, 2008, 10:16 PM   #6
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
I'm going to have to disagree with starting with Xcode for your first program. It's like using a high-powered laser cannon to open a can. Someday when you need to open a 20-story can, you might start considering the laser, but for your first program I would stick to a can opener.

A vi(m) cheatsheet can be be found here:
http://www2.cs.uidaho.edu/~rinker/ed03.pdf

To run vim you'll need to open terminal in OS X. from there, you can start with:
vi helloworld.c

This will open vi with a new document called helloworld.c. Open a second terminal window with ctrl+n.

In the vim window, enter (i)nsert mode with the i key. Then type:
Code:
#include <stdio.h>
int main(int argc,char *argv[]) {
  printf("Hello World!\n");
}
Once you've entered this text, press esc to exit insert mode. Then type :w<enter>. This will (w)rite the changes you've made.

In your second terminal window, enter:
gcc -o helloworld helloworld.c
./helloworld

The commands in vim will be confusing at first. However, knowing how to use this editor, if you will ever work on another unix-like system, will be invaluable. For starters what's in this post, plus :q to quit are all you'll need. You can then move on to deleting characters, words, etc. Eventually working up to replacement, undo and redo, etc.
lee1210 is offline   0 Reply With Quote
Old Mar 18, 2008, 11:11 PM   #7
Muncher
macrumors 65816
 
Muncher's Avatar
 
Join Date: Apr 2007
Location: California
Quote:
Originally Posted by lee1210 View Post
I'm going to have to disagree with starting with Xcode for your first program. It's like using a high-powered laser cannon to open a can. Someday when you need to open a 20-story can, you might start considering the laser, but for your first program I would stick to a can opener.

A vi(m) cheatsheet can be be found here:
http://www2.cs.uidaho.edu/~rinker/ed03.pdf

To run vim you'll need to open terminal in OS X. from there, you can start with:
vi helloworld.c

This will open vi with a new document called helloworld.c. Open a second terminal window with ctrl+n.

In the vim window, enter (i)nsert mode with the i key. Then type:
Code:
#include <stdio.h>
int main(int argc,char *argv[]) {
  printf("Hello World!\n");
}
Once you've entered this text, press esc to exit insert mode. Then type :w<enter>. This will (w)rite the changes you've made.

In your second terminal window, enter:
gcc -o helloworld helloworld.c
./helloworld

The commands in vim will be confusing at first. However, knowing how to use this editor, if you will ever work on another unix-like system, will be invaluable. For starters what's in this post, plus :q to quit are all you'll need. You can then move on to deleting characters, words, etc. Eventually working up to replacement, undo and redo, etc.
I agree, sort of. I just remember how much frustration I got because of not using an IDE as a beginner programmer. Any shell/terminal can be hell to figure out on your own.
__________________
2.4Ghz 20" Rev. A iMac, Wii, crimson DS, intuos3 4x6, PS3
iPod touch 2G 16GB > Fiio E1 > Hifiman RE0
Muncher is offline   0 Reply With Quote
Old Mar 18, 2008, 11:26 PM   #8
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
I guess I feel this way about IDEs:
you should be able to use an IDE, you shouldn't need to use one.

I doubt the original poster cares much about any of this. either path should reach the same goal, I was trying to suggest what I believe leads to the most versatility.

-Lee
lee1210 is offline   0 Reply With Quote
Old Mar 19, 2008, 07:54 AM   #9
j03
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
Well, this is a good way of putting it. Is using xCODE the same as trying to make a website in dreamweaver:design view and terminal more like hand coding a website (the right way a website is created) ?

I just don't want to jump down the xcode path and then always have to use that in order to program c. I mean I'm sure if i start with just the xcode path to get down some basics then try to figure out terminal and then work then together ? Does that sound better? Thanks for all the replies too btw.
j03 is offline   0 Reply With Quote
Old Mar 19, 2008, 09:03 AM   #10
yeroen
macrumors 6502a
 
yeroen's Avatar
 
Join Date: Mar 2007
Location: Cambridge, MA
Quote:
Originally Posted by Muncher View Post
I agree, sort of. I just remember how much frustration I got because of not using an IDE as a beginner programmer. Any shell/terminal can be hell to figure out on your own.
Perhaps it's a generational thing, but I find that to be much more the case with IDE's. Once you've learned how to use the shell, it's practically universal across all systems. IDE's have a lot in common, but you find yourself having to learn each one anew.
__________________
all Jarvis, all the time
yeroen is offline   0 Reply With Quote
Old Mar 19, 2008, 09:12 AM   #11
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by j03 View Post
Well, this is a good way of putting it. Is using xCODE the same as trying to make a website in dreamweaver:design view and terminal more like hand coding a website (the right way a website is created) ?

I just don't want to jump down the xcode path and then always have to use that in order to program c. I mean I'm sure if i start with just the xcode path to get down some basics then try to figure out terminal and then work then together ? Does that sound better? Thanks for all the replies too btw.
The dreamweaver comparison is somewhat accurate. You still have to write most of the code in XCode, but it hooks in to the compiler, so you don't have to worry about that yourself, it hooks in to the debugger so you don't have to worry about that yourself, etc. It's a great tool, but I think you end up more well-rounded if you know how to use a unix editor (i prefer vi, so i was pushing that, but emacs seems to be equally popular), you know how to use a compiler and figure out some switches (i.e. -g sets up your binary with debugging symbols), you know how to run things from the command line, you know how to use a debugger, and so on.

My real reasoning here is that someday you might want to write something somewhere other than your mac. If that's the case, XCode is likely unavailable. I think the same way about Visual Studio, etc. There are times when, in languages like Java or C#, I feel like working without an IDE is much trickier. Mostly because of the breadth of their class libraries and "code completion" features that IDEs have.

I think the important thing is you just start programming in either environment, and if you want to switch later, you should be fine. If you want to use XCode as your editor, but still compile and run your program in the command line so you don't have to use vi, that's great. The only pitfall I would try to avoid is taking the power of XCode for granted. You enter your source, and then you press the "build and go" button, and that's it. You don't know what it did, but your program ran. I think it's important to know the mechanics.

Good luck, if you have any issues getting started browse the forums and if you can't come up with anything post. People here are glad to help.

-Lee
lee1210 is offline   0 Reply With Quote
Old Mar 19, 2008, 09:49 AM   #12
j03
Thread Starter
macrumors newbie
 
Join Date: Mar 2008
Quote:
Originally Posted by lee1210 View Post
The dreamweaver comparison is somewhat accurate. You still have to write most of the code in XCode, but it hooks in to the compiler, so you don't have to worry about that yourself, it hooks in to the debugger so you don't have to worry about that yourself, etc. It's a great tool, but I think you end up more well-rounded if you know how to use a unix editor (i prefer vi, so i was pushing that, but emacs seems to be equally popular), you know how to use a compiler and figure out some switches (i.e. -g sets up your binary with debugging symbols), you know how to run things from the command line, you know how to use a debugger, and so on.

My real reasoning here is that someday you might want to write something somewhere other than your mac. If that's the case, XCode is likely unavailable. I think the same way about Visual Studio, etc. There are times when, in languages like Java or C#, I feel like working without an IDE is much trickier. Mostly because of the breadth of their class libraries and "code completion" features that IDEs have.

I think the important thing is you just start programming in either environment, and if you want to switch later, you should be fine. If you want to use XCode as your editor, but still compile and run your program in the command line so you don't have to use vi, that's great. The only pitfall I would try to avoid is taking the power of XCode for granted. You enter your source, and then you press the "build and go" button, and that's it. You don't know what it did, but your program ran. I think it's important to know the mechanics.

Good luck, if you have any issues getting started browse the forums and if you can't come up with anything post. People here are glad to help.

-Lee
Lee, along with others : Thanks so much. I will just begin writing C in xCODE just to get down some basics, then I will look into the command line because I think as many of you said - it's something you definately should know if your not on a mac with xcode or on a pc with VS. Thanks all! I will bookmark this forum for future use for C programming thanks!!
j03 is offline   0 Reply With Quote
Old Mar 19, 2008, 11:26 AM   #13
jamesapp
macrumors 6502a
 
Join Date: Mar 2008
objective-c

trying to do exercise from chapter 13 of a book on programming with objective-c
1. write a function that calculates the average of an array of 10 floating-point values and returns the result.

i have the function to get the average i think?
i tried to put 10 elements into an array, and then tried to print the average at the end of the program. here is the function for my program.


float average (float data[])
{
int i;
float sum, average;

for (i = 0; i < 10; ++i)
sum += data[i];

average = sum / 10;

return average;
}

here are the error messages i am getting when i try to run my test program which i called exc1chap13.m

exc1chap13.m:5: error: syntax error before ‘}’ token
exc1chap13.m: In function ‘average’:
exc1chap13.m:19: error: nested functions are disabled, use -fnested-functions to re-enable
exc1chap13.m:23: error: syntax error at end of input

and here is my test program

#import <stdio.h>


float data [10] = { 10, 5, -3, 17, 82, 9, 0, 0, 8, -7 };
}
float average (float data[])
{
int i;
float sum, average;

for (i = 0; i < 10; ++i)
sum += data[i];

average = sum / 10;

return average;

int main (int argc, char *argv[])
{

printf ("the average of the array is %i\n", average);

}

if i don't get an answer:

error messages

exc1chap13.a.m: In function ‘main’:
exc1chap13.a.m:7: error: syntax error before ‘{’ token
exc1chap13.a.m:10: error: conflicting types for ‘average’
exc1chap13.a.m:7: error: previous definition of ‘average’ was here
exc1chap13.a.m:12: error: syntax error before ‘for’

test program:

#import <stdio.h>

int main (int argc, char *argv[])


float average (float data[])
{
int x[10] = {3, 7, -9, 3, 6, -1, 7, 9, 1, -5);
int i;
float sum, average;

for (i = 0; i < 10; ++i)
sum += data[i];

average = sum / 10;

printf ("the average of the array is %i\n", average);

return average;
}
jamesapp is offline   0 Reply With Quote
Old Mar 19, 2008, 11:29 AM   #14
brn2ski00
macrumors 68020
 
brn2ski00's Avatar
 
Join Date: Aug 2007
Depends on the type of C programming you are going to do. If you are going to just write command line apps, then XCode is overkill. But if you plan to use C as the Controller piece of a large app, then XCode would be your best bet.

It boils down to needs.
__________________
iMac 27-inch 2.7GHz Quad-Core i5
500GB Time Capsule
iPhone 5c / ATV3 / iPod Shuffle 2G
brn2ski00 is offline   0 Reply With Quote
Old Mar 19, 2008, 03:11 PM   #15
fatpwnage
macrumors newbie
 
Join Date: Mar 2008
May I ask why you want to learn C instead of C++? C++ is better than C in every way, so learning C is backwards seeming.
fatpwnage is offline   0 Reply With Quote
Old Mar 19, 2008, 03:29 PM   #16
szark
macrumors Demi-God
 
szark's Avatar
 
Join Date: May 2002
Location: Arid-Zone-A
Quote:
Originally Posted by jamesapp View Post
and here is my test program

#import <stdio.h>


float data [10] = { 10, 5, -3, 17, 82, 9, 0, 0, 8, -7 };
}
float average (float data[])
{
int i;
float sum, average;

for (i = 0; i < 10; ++i)
sum += data[i];

average = sum / 10;

return average;

int main (int argc, char *argv[])
{

printf ("the average of the array is %i\n", average);

}
The bracket in red should be after the line in blue. You also need to call the function "average" from your main() function.

You might also ask yourself: What is the value of "sum" before you start adding data values to it?
__________________
...let's climb those fences with signs that say...Don't...
szark is offline   0 Reply With Quote
Old Mar 19, 2008, 03:30 PM   #17
admanimal
macrumors 68040
 
Join Date: Apr 2005
Quote:
Originally Posted by fatpwnage View Post
May I ask why you want to learn C instead of C++? C++ is better than C in every way, so learning C is backwards seeming.
That is a highly subjective opinion you have there. Besides, learning C first is not backwards, since C++ is a superset of C. You have to know C to know C++, whether you realize it or not. If the OP wants to learn Objective-C eventually, learning C++ first would be overkill.
admanimal is offline   0 Reply With Quote
Old Mar 19, 2008, 03:32 PM   #18
ChrisA
macrumors G4
 
Join Date: Jan 2006
Location: Redondo Beach, California
Quote:
Originally Posted by fatpwnage View Post
May I ask why you want to learn C instead of C++? C++ is better than C in every way, so learning C is backwards seeming.
In every way?

Perhaps he wants to program a microcontroller or write real-time software or become a "kernel hacker". There was a thread here yesterday about learning asembly language. I've been working in software development for a few years (going back to the 70s) and I can count the number of desktop GUI apps I've written at maybe four total.
ChrisA is offline   0 Reply With Quote
Old Mar 19, 2008, 04:15 PM   #19
yeroen
macrumors 6502a
 
yeroen's Avatar
 
Join Date: Mar 2007
Location: Cambridge, MA
Quote:
Originally Posted by fatpwnage View Post
May I ask why you want to learn C instead of C++? C++ is better than C in every way, so learning C is backwards seeming.
The beginning developer intending to learn C++ will be well served by the astringent of pure C.

C++ can be a very elegant and powerful tool in the right hands. When wielded by the inexperienced programmer, it often leads to buggier, more brittle, less maintainable, and less comprehensible code. C++ increases the number of subtleties, traps, pitfalls, and gotchas by an order of magnitude over pure C. IMHO, best to make all the beginner mistakes in C before moving on to making varsity mistakes in C++.

Besides, for operating systems, compilers, real-time and embedded software, even the bulk of open-source application software, a facility with C and the C APIs remain a pre-requisite.
__________________
all Jarvis, all the time

Last edited by yeroen; Mar 19, 2008 at 04:25 PM.
yeroen is offline   0 Reply With Quote
Old Mar 19, 2008, 04:41 PM   #20
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: The Land of Hope and Glory
Quote:
Originally Posted by fatpwnage View Post
May I ask why you want to learn C instead of C++? C++ is better than C in every way, so learning C is backwards seeming.
Hardly. C++ is just a hacked version of C.
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote
Old Mar 19, 2008, 07:25 PM   #21
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by fatpwnage View Post
May I ask why you want to learn C instead of C++? C++ is better than C in every way, so learning C is backwards seeming.
C++ can be great, or awful. It was mentioned that C++ is a superset, but it is not a proper superset and there are some operator precedence issues, etc.. There are valid reasons to want to know C, and C++ is not the path to get there.

It's not an issue to ask, but is presumptuous to state that C++ is better in every way. It also seems to me that learning C++ first is backwards, being that C++ is nearly a superset of C. Learning addition before algebra is not backwards.
lee1210 is offline   0 Reply With Quote
Old Mar 20, 2008, 05:32 PM   #22
psingh01
macrumors 65816
 
Join Date: Apr 2004
Use XCode. Don't waste time with VIM or any other editors some "hardcore programmer" will guide you to. You'll know when you need to learn such a thing, but not when you are starting.
psingh01 is offline   0 Reply With Quote
Old Mar 20, 2008, 06:54 PM   #23
trogdor!
macrumors regular
 
Join Date: Mar 2006
Heres my take on learning C.

Start out with just text editor or just a syntax highlighting text app and learn the basics. In formal school training, we were not allowed to use high functionality IDE's for the first 2 quarters because they wanted us to get the basics with our first assignments. It really does help you actually understand things better and once you starting using IDE's. Once you feel good programing some simple stuff in text editors and remembering code syntax and form, then you can start trying some IDE's and watch how they really help large projects come together by helping you find built in functions quickly and help you code quicker. Many say only true hardcore programmers know how to code in straight text. That is true to some degree, but it is a waste of time once you have an idea what your doing. IDE's can save lots of time by helping you debug quicker and identify errors. Your end goal should also to not just be able to program, but to be able to program with efficient use of memory and performance (algorithm timing and complexity).

As for starting out though, it really does help to learn the basics through just text editing.

As for languages, C is one the best ways to start out programming. Once you learn it this way, you appreciate any newer languages. Also, once you learn one high level language, the others come really easy after that.
trogdor! is offline   0 Reply With Quote
Old Mar 22, 2008, 12:58 PM   #24
zippyfly
macrumors regular
 
Join Date: Mar 2008
I humbly disagree with most of the comments here.

I don't recommend VIM and don't recommend Xcode either.

I do recommend using TERMINAL and learning at least the basic Bash Shell commands to list directory contents (ls) and change directories (cd).

Now, install the free and wonderful TextWrangler (google it) including the command line tools. You can then type "edit hello.m" at the command line.

In exchange for this little tidbit of information, I need help from the pros. I am STILL stick on pointers. I know what they are in C but I am confused by how they are used when creating and referencing objects in ObjC.

When do I use the * and when do I not in ObjC (not C)??

Why does the * sometimes exist on its own??

It is terribly confusing. PLEASE HELP. ANYONE! None of the web pages or text books explain this well. It must be a conspiracy or something! Haha.

Thanks!
zippyfly is offline   0 Reply With Quote
Old Mar 22, 2008, 01:16 PM   #25
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by zippyfly View Post
I humbly disagree with most of the comments here.

I don't recommend VIM and don't recommend Xcode either.

I do recommend using TERMINAL and learning at least the basic Bash Shell commands to list directory contents (ls) and change directories (cd).

Now, install the free and wonderful TextWrangler (google it) including the command line tools. You can then type "edit hello.m" at the command line.

In exchange for this little tidbit of information, I need help from the pros. I am STILL stick on pointers. I know what they are in C but I am confused by how they are used when creating and referencing objects in ObjC.

When do I use the * and when do I not in ObjC (not C)??

Why does the * sometimes exist on its own??

It is terribly confusing. PLEASE HELP. ANYONE! None of the web pages or text books explain this well. It must be a conspiracy or something! Haha.

Thanks!
Some code (in a new thread) would probably help people assist you. If you are working with classes (if you aren't, it's pretty much just C), you'll probably be using a lot of pointers. I have done very little Obj-C programming, but essentially anything with an init method is going to give you a pointer. The possible exception would be when using what is essentially a struct. An example is NSRange. You don't have to call anything on it, you just set its fields. There are some methods, but you don't need to initialize it.

As for TextWrangler, if you will only program on the Mac, I'm sure it would fit the bill. TextWrangler is not cross platform, though. That's the only reason I will, stubbornly and annoyingly, continue to insist on vim.

-Lee
lee1210 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Learning to programme for iOS: expectations and where to start? Yixian iPhone/iPad Programming 1 Apr 13, 2014 01:24 PM
Learning to Code: Where to Start? ToomeyND Mac Programming 11 Mar 5, 2014 01:27 PM
How can i start learning to develop apps? Twoody96 iPhone/iPad Programming 4 Apr 3, 2013 10:34 AM
Book to start learning C-objective themis.b iPhone/iPad Programming 12 Mar 29, 2013 10:43 AM
How did you start learning how to program? tekboi Mac Programming 30 Mar 24, 2013 11:26 PM

Forum Jump

All times are GMT -5. The time now is 03:18 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC