开发者

Using XCode to learn C with K&R, but getting too many errors

开发者 https://www.devze.com 2022-12-20 00:49 出处:网络
I\'m want to learn C programming with K&R using XCode, but I can\'t even get the Hello World to wo开发者_StackOverflowrk right - it\'s giving me errors it shouldn\'t, I guess because it\'s being v

I'm want to learn C programming with K&R using XCode, but I can't even get the Hello World to wo开发者_StackOverflowrk right - it's giving me errors it shouldn't, I guess because it's being very technical. Can I get XCode to relax on requirements? Would greatly appreciate some advice! Thanks.


Xcode Bah! Just compile from the command line:

gcc myfile.c -o myfile


You don't need Xcode, but if you want to ... Xcode has hello world built in. Make a new project "Command Line Utility", "Standard Tool", give it a name, have a look in "Source", and you'll see the Hello World program:

#include <stdio.h>

int main (int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");
    return 0;
}

Click "Build and Run" and it goes, you see:

Running…
Hello, World!

Debugger stopped.
Program exited with status value:0.


I will agree with ergosys above. Xcode is realistically made for Objective-C coding, and even its "Command Line Utility" template is sheer overkill. Anything in K&R will compile flawlessly with

gcc -Wall filename.c

except maybe some of the more advanced stuff. K&R doesn't go into object compiling or linking extensively, since it's only meant to teach you the language. Grasp compilation though, and learning C will be much easier. I much prefer using Makefile's or gcc/g++ than IDE's like Xcode or Eclipse.


I'm gonna assume that XCode is demanding Ansi Standard C, while you are using an early edition of the K&R book, whch still uses the old style.

Unless you are doing this just so you can interprete some ancient C code, don't bother with the old style. Use the Ansi Standard syntax.


Honestly, XCode is way more than you need if using K&R. K&R is about basics, XCode is about making you pull your hair out.

I actually prefer to use the command line instead of an IDE on the Mac.

At least for K&R, using the command line is the way you want to go. Try:

gcc -o outputfile code.c

If you want to get into iPhone/iPod or Cocoa applications, then use Xcode. But more times than most, XCode is overkill and will probably just slow you down.


As I said in my comment, you shouldn't see an error from Xcode with the hello world program from K&R. You might be seeing some warnings, or you have mistyped something. Remember to select the correct language for compiling (you want C, of course).

Can I get XCode to relax on requirements?

In general, you want the compiler to be able to warn you for things that it thinks are not "right". In some cases, the warnings are harmless, and can be turned off, but in most of the cases, the warnings expose issues with your program. I like compiling my code with the maximum warnings enabled for example, because it saves me a lot of time later.

If you are getting errors, you should post (copy-paste) your code here.

Finally, look at the errata page for K&R. It has some bug fixes or corrections, but nothing for hello world.

(It could be that you're using the first edition of the book, in which case you shouldn't use that book to learn C.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号