C语言基础习题50例(十)46-50

知足常足,终身不辱。 月圆缺,水满溢,事情到了极致一定会遭受祸患,只有懂得知足,才是富足。

习题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浏览更多优质原创内容。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid