C Preprocessor এর ব্যবহার ও উপযোগিতা

Submitted by tushar pramanick on Mon, 01/02/2012 - 16:42

In Chapter 2, "Writing Your First C Program," you learned how to use the #include preprocessor directive to include C header files. Since then, the #include directive has been used in every program in this book. In this lesson you'll learn more about the C preprocessor and making macro definitions with the preprocessor directives. The following topics are discussed in this hour:

    What the C preprocessor can do
    Macro definitions and macro substitutions
    The #define and #undef directives
    How to define function-like macros with #define
    The #ifdef, #ifndef, and #endif directives
    The #if, #elif, and #else directives
    How to nest #if and #elif directives




Summary
 

  •     The C preprocessor runs before the compiler. During preprocessing, all occurrences of a macro name are replaced by the macro body associated with the macro name.
  •     The C preprocessor also enables you to include additional source files to the program or compile sections of C code conditionally.
  •     The C preprocessor is not part of the C compiler.
  •     A macro statement ends with a newline character, not a semicolon.
  •     The #define directive tells the preprocessor to replace every occurrence of a macro name defined by the directive with a macro body that is associated with the macro name.
  •     The #undef directive is used to remove the definition of a macro name that has been previously defined.
  •     You can specify one or more arguments to a macro name defined by the #define directive.
  •     The #ifdef directive enables you to define code that is to be included when a particular macro name is defined.
  •     The #ifndef directive is a mirror directive to the #ifdef directive. The former enables you to define code that is to be included when a particular macro name is not defined.
  •     The #endif is used to mark the end of an #ifdef, an #ifndef, or an #if block.
  •     The #if, #elif, and #else directives enable you to select portions of code to compile.

 

Related Items

মডুলার C প্রোগ্রামিং (Modular C Programming)

কেবল মাত্র একটি ফাংশন দিয়ে কোনো বড়ো জটিল সমস্যা সমাধানের চেষ্টা করা ভাল প্রোগ্রামিংয়ের পদ্ধতি নয়। সঠিক পদ্ধতি হ'ল সমস্যাটিকে কয়েকটি ছোট ছোট এবং সরল টুকরো করে ফেলা যাতে তা আরও বিশদে বোঝা যায় । তারপরে এই ছোট এবং সরল সমস্যাগুলি সমাধান করার জন্য ছোট ছোট ফাংশন ব্লক তৈরি করা এবং পরে সেগুলি নিয়মানুযায়ী সংযোজিত করা ।

Programming Style

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.

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.