⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex13_01.c

📁 [C语言入门经典(第4版)]整本书的源码!值得推荐!全部是最简单的源码!
💻 C
字号:
/* Exercise 13.1 Defining a COMPARE(x,y) macro */
/*
  One advantage of the compare macro is that is works with numeric values of any type.
  Another advantage is that there is no function call overhead.
  A disadvantage is the the result is a literal that is an integer. If you were to
  assume the result was the same as the arguments to the macro in the printf()
  calls for floating-point value arguments you would get the wrong result.
*/
#include <stdio.h>

#define COMPARE(x,y) (((x)<(y)) ? -1 : (((x)==(y)) ? 0 : 1))


int main(void)
{
  int a = 5, b = 5, c = 10;
  printf("\n COMPARE(%d, %d) = %d", a, b, COMPARE(a,b));
  printf("\n COMPARE(%d, %d) = %d", a, c, COMPARE(a,c));
  printf("\n COMPARE(%d, %d) = %d", c, b, COMPARE(c,b));
  printf("\n COMPARE(%d, %d) = %d", a+b, c-b, COMPARE(a+b,c-b));

  double x = 24.5, y = 28.0, z = 3.5;
  printf("\n COMPARE(%.2f, %.2f) = %d", x, y, COMPARE(x,y));
  printf("\n COMPARE(%.2f, %.2f) = %d", y, z, COMPARE(y,z));
  printf("\n COMPARE(%.2f, %.2f) = %d", x+z, y, COMPARE(x+z,y));
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -