Yay for code which functions at runtime!
Alright, I may have coppied this one from the ANSI C book, but you need to start somewhere. I’m having some horrible trouble getting simple C things to work on my Mac, so I’ve had to come in and use a filthy Linux machine. Hopefully I’ll be able to get the ‘ol iMac to play ball now.
This program spews forth some temperature conversions.
#include <stdio.h> /* Print Fahrenheit-Celsius table, from ANSI C */ int main() { int fahr; for (fahr = -600; fahr <= 500; fahr = fahr + 50) { printf("%3d %8.3f\n", fahr, (5.0/9.0)*(fahr-32)); } return 0; }
Whoo! I went a bit mad with the variables in the for loop just to make sure it all works as I think it does.
I would like to announce that programming in C is neither fun nor exciting. It is, in the parlance of our times, a drag. ![]()
Dude, if I can give you one bit of advice it would be this:
“Always (and I mean always) compile and run your code on the uni machines”
There is nothing worse than producing code which “works fine at home, honest!” and then getting failed when it won’t run on the lecturer/tutor’s machine.
Personally I ended up using a text editor which could save direct to FTP and having a shell screen open all the time to compile and run. A little bit of hassle to set up, but saves a huge amount of pain in the long run!!
Very good tip, Meri. Thanks
I’ll have to figure out that whole shell FTP thing first, though…
You’ll hopefully have some sort of *nix orientation class at some point. If not, I’d recommend you get yourself one of the little O’Reilly handbooks — references for basic bits and bobs on Linux. Get a good understanding of ssh, sftp and the command line now and it’ll pay serious dividends later.
Nice stuff! Works fine on my iMac G5. Just put it into your User folder, open up Terminal type:
gcc degrees.c -o degrees
And then ./degrees to run it. Cool syntax highlighting on your blog by the way!
I’ll echo Meri’s statement - completely regardless of which language you’re writing you will at some point get completely screwed over by the “it works at home!” bug (even if it’s meant to be a platform independent, scripting language like Python).
You should be able to map your network drive locally and work directly out of there, it might be a little slower than local, but you’ll know that your code actually works! http://www.bris.ac.uk/is/computing/advice/homeusers/resnet/docs/cufs.html#macx I guess.
You’ll be glad to know that this is probably the most exciting C you’ll ever write; it’s all downhill from here