C语言必会100题(10)。宏#define命令练习(1)/宏#define命令练习(2)/宏#define命令练习(3)/#if #ifdef和#ifndef的综合应用/#include 的应用练习

特此鸣谢:鱼C_小甲鱼(B站up主)不二如是(鱼C论坛大佬)
题目来源:https://fishc.com.cn
注:这些题在网上都可以搜到,题下面的代码大多是流传下来的答案(我重新排了一下版,增加了可读性),部分是本人经过深思熟虑后编写的。

46,宏#define命令练习(1)

  • 题目:宏#define命令练习(1)
  • 程序源代码:
    #include "stdio.h"
    #include "conio.h"
    
    #define TRUE 1
    #define FALSE 0
    #define SQ(x) (x)*(x)
    
    int main()
    {
        int num;
        int again = 1;
    
        printf("\40: Program will stop if input value less than 50.\n");
    
        while(again) {
    
            printf("\40:Please input number==>");
            scanf("%d", &num);
    
            printf("\40:The square for this number is %d \n", SQ(num));
    
            if(num >= 50) {
    
                again =TRUE;
    
            } else {
    
                again = FALSE;
            }
        }
    
        getch();
    }

47,宏#define命令练习(2)

  • 题目:宏#define命令练习(2)
  • 程序源代码:
    #include "stdio.h"
    #include "conio.h"
    
    // 宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上"\"
    // 就是说:两行命令以上组成的一个宏定义需要用\来进行连接
    #define exchange(a,b) { \
                            int t;\
                            t=a;\
                            a=b;\
                            b=t;\
                          }
                          
    int main(void)
    {
        int x = 10;
        int y = 20;
    
        printf("x = %d; y = %d\n", x, y);
    
        exchange(x, y);
        printf("x = %d; y = %d\n", x, y);
    
        getch();
    }

48,宏#define命令练习(3)

  • 题目:宏#define命令练习(3)
  • 程序源代码:
    #include "stdio.h"
    #include "conio.h"
    
    #define LAG >
    #define SMA <
    #define EQ ==
    
    int main(void)
    {
        int i = 10;
        int j = 20;
    
        if(i LAG j) {
    
            printf("\40: %d larger than %d \n",i,j);
    
        } else if(i EQ j) {
    
            printf("\40: %d equal to %d \n",i,j);
    
        } else if(i SMA j) {
    
            printf("\40:%d smaller than %d \n",i,j);
    
        } else {
    
            printf("\40: No such value.\n");
        }
    
        getch();
    }

49,#if #ifdef和#ifndef的综合应用

  • 题目:#if #ifdef和#ifndef的综合应用
  • 程序源代码
    // 可以看一下这篇文章
    // https://blog.csdn.net/wordwarwordwar/article/details/84932183
    
    // #ifdef              判断某个宏是否被定义,若已定义,执行随后的语句
    // #ifndef            与#ifdef相反,判断某个宏是否未被定义
    // #else              与#if, #ifdef, #ifndef对应, 若这些条件不满足,则执行#else之后的语句,相当于C语法中的else
    // #endif             #if, #ifdef, #ifndef这些条件命令的结束标志.
    // defined          与#if, #elif配合使用,判断某个宏是否被定义
    
    #include "stdio.h"
    #include "conio.h"
    
    #define MAX
    #define MAXIMUM(x, y) (x > y) ? x : y
    #define MINIMUM(x, y) (x > y) ? y : x
    
    int main()
    {
        int a = 10, b = 20;
    
        #ifdef MAX
            printf("\40: The larger one is %d\n", MAXIMUM(a, b));
        #else
            printf("\40: The lower one is %d\n",MINIMUM(a, b));
        #endif
    
        #ifndef MIN
            printf("\40: The lower one is %d\n",MINIMUM(a, b));
        #else
            printf("\40: The larger one is %d\n",MAXIMUM(a, b));
        #endif
    
        // #undef 是在后面取消以前定义的宏定义
        #undef MAX
        #ifdef MAX
            printf("\40: The larger one is %d\n",MAXIMUM(a, b));
        #else
            printf("\40: The lower one is %d\n",MINIMUM(a, b));
        #endif
    
        #define MIN
        #ifndef MIN
            printf("\40: The lower one is %d\n",MINIMUM(a, b));
        #else
            printf("\40: The larger one is %d\n",MAXIMUM(a, b));
        #endif
    
        getch();
    }

50,#include 的应用练习

  • 题目:#include 的应用练习
  • 程序源代码:
    main.c文件:
    #include <stdio.h>
    // 这个不能改为<test.h>
    #include "test.h" 
    
    int main()
    {
        int i = 10;
        int j = 20;
    
        if(i LAG j) {
    
           printf("\40: %d larger than %d \n", i, j);
    
        } else if (i EQ j) {
    
            printf("\40: %d equal to %d \n", i, j);
    
        } else if (i SMA j) {
    
            printf("\40:%d smaller than %d \n", i, j);
    
        } else {
    
            printf("\40: No such value.\n");
        }
    
        return 0;
    }
    
    test.h文件:
    #ifndef TEST_H_INCLUDED
    #define TEST_H_INCLUDED
    
    #define LAG >
    #define SMA <
    #define EQ ==
    
    #endif // TEST_H_INCLUDED
    

     

posted @ 2022-03-20 17:43  炸天帮帮主  阅读(70)  评论(0编辑  收藏  举报