zoukankan      html  css  js  c++  java
  • c语言之条件编译

    #include<stdio.h>
    #include<iostream>
    #define CORRECT "myfirst"
    int main() { char str[10]; int func(char* str1, char* str2); printf("输出你最喜欢的: "); scanf("%s", str); //如果没有定义CORRECT宏名,则在接下来进行定义 #ifndef CORRECT #define CORRECT "test" #endif // CORRECT if (func(str, CORRECT) == 0) { printf("谢谢你 "); } else { printf("不好意思了 "); } system("pause"); return 0; } int func(char* str1, char* str2) { return strcmp(str1, str2); }

    判断是否有该宏存在,如果没有再进行定义。

    这里我遇到个问题,假设我在定义时#define CORRECT "my first"里面进行空格,那么之后无论我怎么输入,总是得到(func(str, CORRECT)=-1。还不知道是什么原因,应该是不允许字符串中有空格?

    第二种方式:

    #include<stdio.h>
    #include<iostream>
    #define ROUND 1
    #define PI 3.14
    
    int main() {
        int a,b;
        double c;
        a = 2;
        b = 3;
    //如果ROUND为1,那么运行接下来的语句
    #if ROUND
        c = (a + b)*PI*2;
        printf("%.2f
    ", c);
    //否则运行下面的语句
    #else
        c = a + b;
    #endif
        system("pause");
        return 0;
    }

    使用条件编译可以只允许编译源程序汇总满足条件的程序段,使生成的目标程序较短,从而减少了内存的开销并提高了程序的效率。

  • 相关阅读:
    python3--shelve 模块
    python3--常用模块
    python3 时间复杂度
    Python3 正则表达式
    python--冒泡排序
    python3--正则表达式
    python3--算法基础:二维数组转90度
    python3--算法基础:二分查找/折半查找
    python3--递归
    dedecms单独调用指定文章
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12128965.html
Copyright © 2011-2022 走看看