getchar()和putchar()宏如何? [英] How are getchar() and putchar() Macros?

查看:123
本文介绍了getchar()和putchar()宏如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我对C语言中的宏的了解,它们是预定义的常量,它们将以其常量值在整个程序中使用,因此,我们继续对其进行定义,以避免进一步的复杂性并使代码更具可读性,因此人们可以读取它们会明白什么应该保持不变,什么不是。

From what I understand about macros in C, they are predefined constants that will be used throughout the program with their constant value, so we go ahead and define them to avoid further complications and make the code more readable, so people reading it will understand what is supposed to stay constant and what isn't.

我在这里和那里已经读过(C编程A Modern Approach,K.N King),我们可以将这两个函数定义为宏。

I have read here and there (C programming A Modern Approach, K.N King) that we can define these two functions as macro.

由于我是C语言的新手,所以我无法解决如何将这两个定义为宏的问题。

Since I'm somewhat new to C, I can't wrap my head around how can these two be defined as macro?

推荐答案

宏有两种类型:简单替换宏和函数式宏。

There are two types of macros: simple substitution macros and function-like macros.

替换宏替换一个符号的一个实例与另一个。例如:

Substitution macros replace one instance of a symbol with another. For example:

#define LEN 10
char str[LEN];

预处理后,它变为:

char str[10];

类似于函数的宏可以采用可以插入替换对象中的参数:

A function-like macro can take parameters that can be plugged in to whatever gets substituted:

#define MAX(a,b) ((a) > (b) ? (a) : (b))
int x = MAX(2,3);

预处理后:

int x = ((2) > (3) ? (2) : (3));

对于 getchar putchar ,它们可以定义如下:

In the case of getchar and putchar, they can be defined as follows:

#define getchar() getc(stdin)
#define putchar(c) putc(c, stdout)

这篇关于getchar()和putchar()宏如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆