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

richard.mac

macrumors 603
Original poster
Feb 2, 2007
6,292
5
51.50024, -0.12662
in Uni atm im studying an intro to Ansi C Programming. in my first assignment today we used Cygwin which is a Unix emulator in Windows and Crimson which is a text editor to write C programs on the lab PCs.

how can i do this on my Mac without using Windows? presumably this can be done on Mac OS X as its Unix based. all the lecturers use Macs as OS X's Unix core obviously appeals to them but the lecturer atm hasnt done an example of writing and compiling a program… just gone through the theory.

i realise that i can just use Terminal in place of Cygwin, but what app do i use to write C programs that also colour codes commands to make it more user friendly? TextEdit? XCode?

also im very new to C Programming so can anyone give me any pointers or tips that helped you when you first learnt C? thanks :)
 

richard.mac

macrumors 603
Original poster
Feb 2, 2007
6,292
5
51.50024, -0.12662
oh ok thanks dude.

i assumed i needed to use Xcode but when i opened my program i couldnt compile it. so i just use Terminal to compile. Thanks!
 

dukebound85

macrumors Core
Jul 17, 2005
19,167
4,164
5045 feet above sea level
thanks for the suggestion :) does TextMate compile? otherwise ill just stick to Terminal and XCode as its included with OS X.

you can compile within xcode.

however i would use a simple editor (as its way less clutter than xcode) and terminal

(i added my text editor preference in my first post fyi)

just my 2 cents.

best of luck to you though!
 

richard.mac

macrumors 603
Original poster
Feb 2, 2007
6,292
5
51.50024, -0.12662
you can compile within xcode. …

thanks

… (i added my text editor preference in my first post fyi)

just my 2 cents.

best of luck to you though!

yeh saw that. i was going to add another reply into my last post but couldnt be bothered. i always edit my post too many times!

i just opened my program with Xcode and it colour codes. so ill just use Xcode but ill give Text Wrangler a try… actually i remember seeing Text Wrangler's icon in the lecturer's dock!
 

aaronw1986

macrumors 68030
Oct 31, 2006
2,622
10
Smultron is a free editor which I use for my C programming. It has colored syntax as well.
 

richard.mac

macrumors 603
Original poster
Feb 2, 2007
6,292
5
51.50024, -0.12662
Smultron is a free editor which I use for my C programming. It has colored syntax as well.

thanks for the suggestion buddy.

does anyone have any tips? like where you had trouble when you first learnt C. i dont think im up to scratch with my work. i need to read up more. heres a program that i wrote today.
Screencapture 1.jpg
 

aaronw1986

macrumors 68030
Oct 31, 2006
2,622
10
One tough part of C is having to manually manage memory resources unlike in Java which automatically does it for you. You may not be that far into the language yet. Just a tip, printf() statements are a great way to help debug if you run into problems.
 

richard.mac

macrumors 603
Original poster
Feb 2, 2007
6,292
5
51.50024, -0.12662
One tough part of C is having to manually manage memory resources unlike in Java which automatically does it for you. You may not be that far into the language yet. Just a tip, printf() statements are a great way to help debug if you run into problems.

i know that printf displays text on the screen e.g.

Code:
printf("The answer is %i", x);

but how does this help with debugging?
 

dukebound85

macrumors Core
Jul 17, 2005
19,167
4,164
5045 feet above sea level
i know that printf displays text on the screen e.g.

Code:
printf("The answer is %i", x);

but how does this help with debugging?

lets you confirm if the values are what you are expecting at points in the code


say you have a math program.

putting printf statements at each step will read the value at that step so it can help troubleshoot in a sense
 

richard.mac

macrumors 603
Original poster
Feb 2, 2007
6,292
5
51.50024, -0.12662
lets you confirm if the values are what you are expecting at points in the code


say you have a math program.

putting printf statements at each step will read the value at that step so it can help troubleshoot in a sense

ohhhh ok… kinda like working out a maths problem on paper using steps instead of punching numbers into a calculator?
 

richard.mac

macrumors 603
Original poster
Feb 2, 2007
6,292
5
51.50024, -0.12662
ok thanks for the tip guys! anyone have anymore? it would really help me…thanks guys

also sorry for all the questions but does anyone know a good read or a wiki or something to help with C programming beginners?
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
thanks for the suggestion buddy.

does anyone have any tips? like where you had trouble when you first learnt C. i dont think im up to scratch with my work. i need to read up more. heres a program that i wrote today.
(image snipped)

Does that really compile with -ansi and -pedantic?

For starters, if you leave off the return type of a function, "int" is assumed (but it's considered bad style), yet your program doesn't "return" anything. Also, if you want a main() with no arguments, you'll have to specify "void".

So your main() becomes:

int main(void)
{
// some code
return 0;
}

Also, I'd check whether x1 and x2 are not (accidentally) the same value, as that'd give you a division by zero.

Good luck!
 

litesgod

macrumors newbie
Apr 3, 2008
8
0
Does that really compile with -ansi and -pedantic?

For starters, if you leave off the return type of a function, "int" is assumed (but it's considered bad style), yet your program doesn't "return" anything. Also, if you want a main() with no arguments, you'll have to specify "void".

So your main() becomes:

int main(void)
{
// some code
return 0;
}

Also, I'd check whether x1 and x2 are not (accidentally) the same value, as that'd give you a division by zero.

Good luck!

All good insights. Also- do you understand what you are doing in the code (which is really the important part if you are learning)? Looking at that code, my first question would be do you understand what scanf is doing, and why you need to use &x1, &y1 etc. Why doesn't scanf("%f %f", x1, y1) work? If you can answer that, I think you are well on your way to understanding C :)
 

CaptainZap

macrumors regular
Jan 17, 2007
170
0
The K&R C Programming Language book is a great way to learn C. It's amazing how much you learn from it.
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
Along the lines of debugging, learn to use assert and macros.

in your code you can do this:

#define DEBUG 1

...


#ifdef DEBUG
printf("Hey Debugging is on!, my Value is: %d\n", myValue);
#endif

When your happy with your code, you just comment out the DEBUG definition:

/* #define DEBUG 1 */

assert is a sanity checker. You essentially give it a something that should evaluate to true. If it's false, your program will tell you exactly where you went wrong.

From your code:

assert( (x2 - x1) > 0 );
 

cruzrojas

macrumors member
Mar 26, 2007
67
0
USA
also sorry for all the questions but does anyone know a good read or a wiki or something to help with C programming beginners?

As usual I would recommend giving emacs a try, if you become proficient at it you can execute commands, compile and run your programs without leaving the editor. (nor having to reach the mouse).

About a good read that will help you with C, I came across this post
http://www.macresearch.org/programming-c-mac-beginners-tutorial
on macresearch recently. I skim through the masters of the void website and indeed is a good starting point for someone trying to learn C.

Enjoy.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
thanks for the suggestion buddy.

does anyone have any tips? like where you had trouble when you first learnt C. i dont think im up to scratch with my work. i need to read up more. heres a program that i wrote today.
View attachment 115005

Chiming in a bit late. I don't know if you were explicitly asking for commentary on code style, but it can't hurt (other than your feelings, I guess, but it shouldn't) at this stage.

Comments are very important, and proper commenting is an art. Everyone has their opinions, so this is only going to be an expression of my own.

You do not need to comment variable declarations in the vast majority of the cases. If you simply cannot express what a variable will be used for via its name, then perhaps a comment is appropriate. In the case of this program, simply stating that you are declaring variables does not add clarity to the code.

I want to be clear that I am not discouraging comments by any means, but if they only serve to muddle up the flow of the code without adding clarity, then it is generally best to omit them.

On a related note, you may want to name m and c something more clear, like slope and y_intercept.

The last thing is that you aren't using newlines (\n) at the end of your expressions in printf.

Good luck, keep posting.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.