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

j03

macrumors newbie
Original poster
Mar 17, 2008
4
0
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
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
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
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
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_(text_editor)

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

Muncher

macrumors 65816
Apr 19, 2007
1,465
0
California
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
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.
 

Muncher

macrumors 65816
Apr 19, 2007
1,465
0
California
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
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
 

j03

macrumors newbie
Original poster
Mar 17, 2008
4
0
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.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

j03

macrumors newbie
Original poster
Mar 17, 2008
4
0
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!!
 

jamesapp

macrumors 6502a
Mar 7, 2008
544
0
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;

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;

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;

average = sum / 10;

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

return average;
}
 

brn2ski00

macrumors 68020
Aug 16, 2007
2,242
14
MA
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.
 

fatpwnage

macrumors newbie
Mar 19, 2008
1
0
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.
 

szark

macrumors 68030
May 14, 2002
2,886
0
Arid-Zone-A
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;

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? ;)
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
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.
 

ChrisA

macrumors G5
Jan 5, 2006
12,917
2,169
Redondo Beach, California
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.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.
 

psingh01

macrumors 68000
Apr 19, 2004
1,586
629
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.
 

trogdor!

macrumors regular
Mar 7, 2006
172
0
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.
 

zippyfly

macrumors regular
Mar 22, 2008
141
0
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!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.