知足常足,终身不辱。 月圆缺,水满溢,事情到了极致一定会遭受祸患,只有懂得知足,才是富足。
习题46
宏#define命令练习。
实现思路:
宏通过#define命令定义,分为无参宏和带参宏,可以分别进行测试。这只是一种简单的字符串代换。
代码如下:
1#include <stdio.h> 2 3#define TRUE 1 4#define FALSE 0 5#define SQR(x) (x)*(x) 6 7int main(){ 8 int num, next = TRUE; 9 while(next){ 10 printf("Please input a number:\n"); 11 scanf("%d", &num); 12 printf("Square = %d\n", SQR(num)); 13 if(num > 10){ 14 next = FALSE; 15 } 16 17 } 18 19 return 0; 20}
打印:
1Please input a number: 21 3Square = 1 4Please input a number: 55 6Square = 25 7Please input a number: 89 9Square = 81 10Please input a number: 1113 12Square = 169 13
习题47
宏#define命令练习,替换一个代码块。
实现思路: 实现在代码中使用宏就像调用函数一样(当然,实际上并不是调用函数)。
代码如下:
1#include<stdio.h> 2 3#define EXCHANGE(a,b) { int t;t=a;a=b;b=t;} 4 5int main() 6{ 7 int x = 12; 8 int y = 20; 9 printf("Before Exchange:x=%d; y=%d\n",x,y); 10 EXCHANGE(x,y); 11 printf("After Exchange:x=%d; y=%d\n",x,y); 12 13 return 0; 14}
打印:
1Before Exchange:x=12; y=20 2After Exchange:x=20; y=12 3
习题48
宏#define命令练习,替换运算符号。
实现思路: 在进行比较运算的时候用定义的宏替换掉原来的符号。
代码如下:
1#include <stdio.h> 2 3#define GT > 4#define LT < 5#define EQ == 6 7int main() 8{ 9 int i, j; 10 printf("Please input two numbers:\n"); 11 scanf("%d %d", &i, &j); 12 if(i GT j) 13 printf("%d is greater than %d \n", i, j); 14 else if(i EQ j) 15 printf("%d is equal to %d \n", i, j); 16 else if(i LT j) 17 printf("%d is smaller than %d \n", i, j); 18 else 19 printf("Error\n"); 20 return 0; 21}
打印:
1Please input two numbers: 213 45 313 is smaller than 45 4
习题49
#if、#ifdef和#ifndef的综合应用。
实现思路: 预处理程序提供了条件编译的功能,可以按不同的条件去编译不同的程序部分,因而产生不同的目标代码文件。
代码如下:
1#include<stdio.h> 2#define MAX 3#define MAXIMUM(x,y) (x>y)?x:y 4#define MINIMUM(x,y) (x>y)?y:x 5int main(){ 6 int a=12, b=20; 7 8#ifdef MAX 9 printf("%d is bigger\n", MAXIMUM(a,b)); 10#else 11 printf("%d is smaller\n", MINIMUM(a,b)); 12#endif 13 14#ifndef MIN 15 printf("%d is smaller\n", MINIMUM(a,b)); 16#else 17 printf("%d is bigger\n", MAXIMUM(a,b)); 18#endif 19 20#undef MAX 21 22#ifdef MAX 23 printf("%d is bigger\n", MAXIMUM(a,b)); 24#else 25 printf("%d is smaller\n", MINIMUM(a,b)); 26#endif 27 28#define MIN 1 29 30#ifndef MIN 31 printf("%d is smaller\n", MINIMUM(a,b)); 32#else 33 printf("%d is bigger\n", MAXIMUM(a,b)); 34#endif 35 36#if(MIN) 37 printf("%d is smaller\n", MINIMUM(a,b)); 38#else 39 printf("%d is bigger\n", MAXIMUM(a,b)); 40#endif 41 42 return 0; 43}
打印:
120 is bigger 212 is smaller 312 is smaller 420 is bigger 520 is bigger 6
习题50
#include的应用练习。
实现思路: 文件包含使用尖括号表示在包含文件目录中去查找(包含目录是由用户在配置环境时设置的),而不在源文件目录去查找; 使用双引号则表示首先在当前的源文件目录中查找,若未找到才到包含目录中去查找。
创建cp.h如下:
1#define GT > 2#define LT < 3#define EQ ==
代码如下:
1#include <stdio.h> 2#include "cp.h" 3 4 5int main() 6{ 7 int i, j; 8 printf("Please input two numbers:\n"); 9 scanf("%d %d", &i, &j); 10 if(i GT j) 11 printf("%d is greater than %d \n", i, j); 12 else if(i EQ j) 13 printf("%d is equal to %d \n", i, j); 14 else if(i LT j) 15 printf("%d is smaller than %d \n", i, j); 16 else 17 printf("Error\n"); 18 return 0; 19}
打印:
1Please input two numbers: 212 20 312 is smaller than 20 4
本文原文首发来自博客专栏C语言实战,由本人转发至https://www.helloworld.net/p/K2DS5af3lHmw,其他平台均属侵权,可点击https://blog.csdn.net/CUFEECR/article/details/106917485查看原文,也可点击https://blog.csdn.net/CUFEECR浏览更多优质原创内容。
