|
|
|||
How To Avoid the Two Most Common C Programming Errors
'21st Century C' author Ben Klemens would like you to know and to do two things immediately, before you write another line of C.
First: • Use designated initializers! Say that you have a structure, like typedef struct{ double length, width, height; } a_box_struct; In the 1980s, we'd declare a box 1 foot tall by 2.5 feet long by 2 feet wide with this line: a_box_struct bigbox = {1, 2.5, 2}; Wait, did I do that right? I think it should be {2.5, 2, 1}. Wow, is that ever confusing. Better would be to use designated initializers, and spell it out: a_box_struct bigbox = {.height=1, .length=2.5, .width=2}; Simply using designated intializers will rid the world of thousands of bugs a day. Second: • Use a makefile! The make utility remembers how to assemble the compiler command line, so you don't have to. Say that you have a program, named bookburner, that is compiled from paper.c, brick.c, and kindle.c, and that must be linked to the math library. What's the command line to build the project? I don't remember, but here is a two-line makefile specifying the library and object file dependencies for the main program: ------- LDLIBS=-lm bookburner: paper.o brick.o kindle.o ------- Now, instead of working out the long and error-prone compiler invocation, I can compile the project by typing make (Ta da!) [bookisbn]9781449327149[/bookisbn] 0 Replies |
|||
|