【问题标题】:issue with understanding macros in C programming在 C 编程中理解宏的问题
【发布时间】:2017-12-07 11:16:31
【问题描述】:

如果我将“1”作为输入,以下代码的输出是什么?为什么?

仅供参考 ::iam 使用 gcc 构建代码。

#include <stdio.h>
int main() 
{
    // k integer to hold user input
    int k=0;

    scanf("%d",&k);

    switch(k)
    {
        case 1 :
        #define first_case
        break;
        case 2 :
         #define second_case
        break;
        case 3 :    
         #define third_case
        break;
        case 4 : 
        #define fourth_case
        break;
    }

#ifdef first_case printf("first_case\n"); #endif

#ifdef second_case printf("second_case\n"); #endif

#ifdef 第三个案例 printf("第三种情况\n"); #endif

#ifdef 第四种情况 printf("第四种情况\n"); #endif

}

【问题讨论】:

  • 您需要了解预处理器宏是什么。我建议开始阅读你的 C 教科书。
  • 预处理器是纯编译时的东西。它在运行时不做任何事情。
  • 我投票结束这个问题,因为不理解该语言的基本语法并不能提出有用的问题,也不能证明预期的背景研究水平。
  • 要了解如何评估宏,请将-E 添加到编译器命令行以查看预处理器输出的内容,即编译器实际编译的内容。
  • 请使用-E 编译您的代码,例如gcc -E myfile.c 并查看输出。这正是 C 编译器“看到”的 -E 强制“仅预处理器”编译。

标签: c macros


【解决方案1】:

宏 (#define MACRO ...) 由 C 预处理器实际编译过程发生之前处理。

所以编译器只有在文件被预处理后才会“看到”这个:

int main()
{
  // your code goes here
  int k = 0;

  scanf("%d", &k);
  switch (k)
  {
  case 1:
    break;
  case 2:
    break;
  case 3:
    break;
  case 4:
    break;
  }

  printf("typed first_case\n");
  printf("typed second_case\n");
  printf("typed third_case\n");
  printf("typed fourth_case\n");
}

你可以这样写你的程序,结果会完全一样:

#define first_case
#define second_case
#define third_case
#define fourth_case

/* Find the longest line among the giving inputs and print it */

#include <stdio.h>
int main()
{
  // your code goes here
  int k = 0;

  scanf("%d", &k);
  switch (k)
  {
  case 1:
    printf("case 1\n");
    break;
  case 2:
    break;
  case 3:
    break;
  case 4:
    break;
  }


#ifdef first_case
  printf("typed first_case\n");
#endif
#ifdef second_case
  printf("typed second_case\n");
#endif

#ifdef third_case
  printf("typed third_case\n");
#endif
#ifdef fourth_case
  printf("typed fourth_case\n");
#endif

}

【讨论】:

    【解决方案2】:

    正如其他人所说,无法在运行时设置或更改预处理器宏,您只需要一个普通的自动变量。例如:

    int k=0;
    int first_case = 0;   // 0 resolves to False
    int second_case = 0;  // 0 resolves to False
    int third_case = 0;   // 0 resolves to False
    int fourth_case = 0;  // 0 resolves to False
    
    scanf("%d",&k);
    switch(k)
    {   
        case 1 :
            printf("case 1\n");
            first_case = 1;
            break;
        case 2 :
            second_case = 1;
            break;
        case 3 :  
            third_case = 1;
            break;
        case 4 :
            fourth_case = 1;
            break;
    }
    
    if (first_case)
        printf("typed first_case\n");
    
    if (second_case)
        printf("typed second_case\n");
    
    if (third_case)
        printf("typed third_case\n");
    
    if (fourth_case)
        printf("typed fourth_case\n");
    

    话虽如此,您要进行两次相同的测试,所以大多数人只会使用case,可能会使用default

    scanf("%d",&k);
    switch(k)
    {   
        case 1 :
            printf("typed first_case\n");
            break;
        case 2 :
            printf("typed second_case\n");
            break;  
        case 3 :    
            printf("typed third_case\n");
            break;
        case 4 : 
            printf("typed fourth_case\n");
            break;
        default :
            fprintf(stderr, "Invalid response: %d\n", k);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2016-03-21
      相关资源
      最近更新 更多