Programming Style
In this section, I'd like to briefly highlight some points that will help you write clean programs that can easily be read, understood, and maintained.
First, make sure the variable or function names in your program describe the meanings of the variables or tasks of the functions precisely and concisely.
Put comments into your code so that you or the other readers can have clues as to what your code is doing, or at least what the code intends to do, but might do incorrectly.
Whenever possible, keep using local variables, not global variables. Try to avoid sharing global data among functions; instead, pass the shared data as arguments to functions.
You should be careful when using C operators, especially the assignment operator (=) and the conditional operator (==), because misuse of these two operators can lead to an unexpected result and make the debugging very difficult.
Avoid using the goto statement; instead, use other control flow statements whenever needed.
Use named constants in your program, instead of numeric constants, because named constants can make your program more readable, and you will have to go to only one place to update the values of constants.
You should put parentheses around each constant expression or argument defined by a preprocessor directive to avoid side effects.
Also, you should set up a reasonable rule for spacing and indentation so that you can follow the rule consistently in all the programs you write. The rule should help make your programs easy to read.
Programming Style
Related Items
মডুলার C প্রোগ্রামিং (Modular C Programming)
Exercises : Answer the following Question
To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.
Question and Answer
Q Is the C preprocessor part of the C compiler?
A No. The C preprocessor is not part of the C compiler. With its own line-oriented grammar and syntax, the C preprocessor runs before the compiler in order to handle named constants, macros, and inclusion of files.
Compiling Your Code Under Conditions
Compiling Your Code Under Conditions
You can select portions of your C program that you want to compile by using a set of preprocessor directives. This is useful, especially when you're testing a piece of new code or debugging a portion of code.
The #define and #undef Directives
The #define and #undef Directives
The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).