码迷,mamicode.com
首页 > 其他好文 > 详细

Structure of a C program: Pre-processor directives (#include <stdlib.h>, #define)

时间:2020-05-16 00:28:44      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:file   limit   mamicode   orm   use   sed   als   stat   val   

技术图片

 

 

 Preprocessor programs provide preprocessors directives which tell the compiler to preprocess the source code before compiling.

Examples of some preprocessor directives are: #include#define#ifndef etc.

There are 4 main types of preprocessor directives:

  1. Macros
  2. File Inclusion
  3. Conditional Compilation
  4. Other directives

Macros

#include <stdio.h> 

// macro definition 
#define LIMIT 5 
int main() 
{ 
    for (int i = 0; i < LIMIT; i++) { 
        printf("%d \n",i); 
    } 

    return 0; 
} 

File Inclusion

1.Header File or Standard files: These files contains definition of pre-defined functions like printf(), scanf() etc.

#include< file_name >

2.user defined files: When a program becomes very large, it is good practice to divide it into smaller files and include whenever needed. These types of files are user defined files. These files can be included as:

#include"filename"

 

Conditional Compilation:

Conditional Compilation directives are type of directives which helps to compile a specific portion of the program or to skip compilation of some specific part of the program based on some conditions. This can be done with the help of two preprocessing commands ‘ifdef‘ and ‘endif‘.

#ifdef macro_name
    statement1;
    statement2;
    statement3;
    .
    .
    .
    statementN;
#endif

 

If the macro with name as ‘macroname‘ is defined then the block of statements will execute normally but if it is not defined, the compiler will simply skip this block of statements.

Other directives

#undef Directive: The #undef directive is used to undefine an existing macro. This directive works as:

#undef LIMIT

Using this statement will undefine the existing macro LIMIT. After this statement every “#ifdef LIMIT” statement will evaluate to false.

 

Structure of a C program: Pre-processor directives (#include <stdlib.h>, #define)

标签:file   limit   mamicode   orm   use   sed   als   stat   val   

原文地址:https://www.cnblogs.com/JasperZhao/p/12897927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!