美文网首页
C语言中Macro的scope

C语言中Macro的scope

作者: EamonXia | 来源:发表于2019-05-15 11:14 被阅读0次

Question:

  How were these macros works?

# define i 20
void fun();

int main(){
  printf("%d",i);
  fun();
  printf("%d",i);
  return 0;
}

void fun(){
  #undef i
  #define i 30
}

gives outputs as 2020,however

# define i 20
void fun(){
  #undef i
  #define i 30
}

int main(){
  printf("%d",i);
  fun();
  printf("%d",i);
  return 0;
}

gives output as 3030.

Answer:

  C Preprocessor works top to bottom irrespective of function calls. It is effective from that point (line) in whatever the file that macro is defined, until corresponding undef or till end of the translation unit.

So, your code would become like this,

# define i 20
               // from now on, all token i should become 20
void fun();
int main()
{
  printf("%d",i);   // printf("%d",20);
  fun();
  printf("%d",i);   // printf("%d",20);
  return 0;
}
void fun()
{
#undef i  // from now on, forget token i
#define i 30 // from now on, all token i should become 30
}

your second code would become like this:

# define i 20  // from now on, all token i should become 20
void fun()
{
#undef i // from now on, forget i
#define i 30  // from now on, all token i should become 30
}
int main()
{
  printf("%d",i);    //  printf("%d",30);
  fun();
  printf("%d",i);    // printf("%d",30);
  return 0;
}

so the outputs is 3030
  In addition,Note that despite other answers asserting that there is no scope, C preprocessor macros do have a well-defined scope, but that scope is only relevant to the preprocessing phase, not any other phase of translation.That scope is "From point of definition until the end of the current translation unit, or until the macro is undefined using #undefine or redefined using#define
  On the other hand,we can use gcc -E -o hello.pp.c hello.cto stop after pre-processing,In this way we can watch the “source file” hello.pp.c which is the outputs of the preprocessor.
[C Primer Plus 6th edtion P760]

Reference:

Frome Source code to executable
C preprocessor - Scope of macros in C?

相关文章

  • C语言中Macro的scope

    Question:   How were these macros works? gives outputs as...

  • CPP常识 04 -- 宏,#号##号,可变参数

    文章来自于这里:c语言中的宏,#号##号,可变参数 C(和C++)中的宏(Macro)属于编译器预处理的范畴,属于...

  • C语言基础6

    结构体概述 1 在C语言中,结构体(struct)指的是一种数据结构,是C语 言中构造类型的其中之一。 2 在实际...

  • swift day2

    for in循环 Nested function 嵌套函数 Closure scope用来定义编程语言中的各种实体...

  • JS中的作用域

    作用域 编程语言中作用于域的定义 Scope refers to the visibility of variab...

  • OC简介

    OC与C对比 1. 源文件对比 C语言中常见源文件.h头文件,.c文件.h 头文件,用于存放函数声明.c C语...

  • WBCategoryKit

    中文说明 Some useful Objective-C ategories and Macro,that con...

  • iOS开发中你真的会用#define么!!!?

    前言: 不得不说在C系语言(C, Objective-C, C++...)中宏(macro)是个强大的东西, 虽然...

  • Maven Scope的依赖传递

    scope的依赖传递 A–>B–>C。当前项目为A,A依赖于B,B依赖于C。知道B在A项目中的scope,那么怎么...

  • C/C++ 中的宏/Macro

    宏(Macro)本质上就是代码片段,通过别名来使用。在编译前的预处理中,宏会被替换为真实所指代的代码片段,即下图中...

网友评论

      本文标题:C语言中Macro的scope

      本文链接:https://www.haomeiwen.com/subject/wxouaqtx.html