Working of the C Preprocessor

前端 未结 7 838
轻奢々
轻奢々 2021-01-19 05:12

How does the following piece of code work, in other words what is the algorithm of the C preprocessor? Does this work on all compilers?

#include 

        
7条回答
  •  被撕碎了的回忆
    2021-01-19 05:38

    The preprocessor just replaces the symbols sequentially whenever they appear. The order of the definitions does not matter in this case, b is replaced by a first, and the printf statement becomes

    printf("%i", a);
    

    and after a is replaced by 170, it becomes

    printf("%i", 170);
    

    If the order of definition was changed, i.e

    #define a 170
    #define b a
    

    Then preprocessor replaces a first, and the 2nd definition becomes

    #define b 170
    

    So, finally the printf statement becomes

    printf("%i",170);
    

    This works for any compiler.

提交回复
热议问题