宏是程序中的一段代码,由宏的值替换。宏由#define指令定义。只要编译器遇到微名称,它就会用宏的定义替换该名称。宏定义不必以分号( ; )结尾。
下面是说明在C / C++中使用宏的程序:
程序1:
C
// C program to illustrate macros
#include
// Macro definition
#define LIMIT 5
// Driver Code
int main()
{
// Print the value of macro defined
printf("The value of LIMIT"
" is %d",
LIMIT);
return 0;
}
C++
// C++ program to illustrate macros
#include
using namespace std;
// Macro definition
#define LIMIT 5
// Driver Code
int main()
{
// Print the value of macro defined
cout << "The value of LIMIT"
<< " is " << LIMIT;
return 0;
}
C
// C program to illustrate macros
#include
// Macro definition
#define AREA(l, b) (l * b)
// Driver Code
int main()
{
// Given lengths l1 and l2
int l1 = 10, l2 = 5, area;
// Find the area using macros
area = AREA(l1, l2);
// Print the area
printf("Area of rectangle"
" is: %d",
area);
return 0;
}
C++
// C++ program to illustrate macros
#include
using namespace std;
// Macro definition
#define AREA(l, b) (l * b)
// Driver Code
int main()
{
// Given lengths l1 and l2
int l1 = 10, l2 = 5, area;
// Find the area using macros
area = AREA(l1, l2);
// Print the area
cout << "Area of rectangle"
<< " is: ",
area;
return 0;
}
C
// C program to illustrate macros
#include
// Macro definition
#define DATE 31
// Driver Code
int main()
{
// Print the message
printf("Lockdown will be extended"
" upto %d-MAY-2020",
DATE);
return 0;
}
C++
// C++ program to illustrate macros
#include
using namespace std;
// Macro definition
#define DATE 31
// Driver Code
int main()
{
// Print the message
cout << "Lockdown will be extended"
<< " upto " << DATE
<< "-MAY-2020";
return 0;
}
C++
// C program to illustrate macros
#include
// Macro definition
#define INSTAGRAM FOLLOWERS
#define FOLLOWERS 138
// Driver Code
int main()
{
// Print the message
printf("Geeks for Geeks have %dK"
" followers on Instagram",
INSTAGRAM);
return 0;
}
C++
// C++ program to illustrate macros
#include
using namespace std;
// Macro definition
#define INSTAGRAM FOLLOWERS
#define FOLLOWERS 138
// Driver Code
int main()
{
// Print the message
cout << "Geeks for Geeks have "
<< INSTAGRAM << "K followers on Instagram!";
return 0;
}
C
// C program to illustrate macros
#include
// Multi-line Macro definition
#define ELE 1, \
2, \
3
// Driver Code
int main()
{
// Array arr[] with elements
// defined in macros
int arr[] = { ELE };
// Print elements
printf("Elements of Array are:\n");
for (int i = 0; i < 3; i++) {
printf("%d ", arr[i]);
}
return 0;
}
C++
// C++ program to illustrate macros
#include
using namespace std;
// Multi-line Macro definition
#define ELE 1, \
2, \
3
// Driver Code
int main()
{
// Array arr[] with elements
// defined in macros
int arr[] = { ELE };
// Print elements
printf("Elements of Array are:\n");
for (int i = 0; i < 3; i++) {
cout << arr[i] << ' ';
}
return 0;
}
C
// C program to illustrate macros
#include
// Function-like Macro definition
#define min(a, b) (((a) < (b)) ? (a) : (b))
// Driver Code
int main()
{
// Given two number a and b
int a = 18;
int b = 76;
printf("Minimum value between"
" %d and %d is %d\n",
a, b, min(a, b));
return 0;
}
C++
// C++ program to illustrate macros
#include
using namespace std;
// Function-like Macro definition
#define min(a, b) (((a) < (b)) ? (a) : (b))
// Driver Code
int main()
{
// Given two number a and b
int a = 18;
int b = 76;
cout << "Minimum value between"
<< a << " and " << b
<< " is: " << min(a, b);
return 0;
}
输出:
The value of LIMIT is 5
程式2:
C
// C program to illustrate macros
#include
// Macro definition
#define AREA(l, b) (l * b)
// Driver Code
int main()
{
// Given lengths l1 and l2
int l1 = 10, l2 = 5, area;
// Find the area using macros
area = AREA(l1, l2);
// Print the area
printf("Area of rectangle"
" is: %d",
area);
return 0;
}
C++
// C++ program to illustrate macros
#include
using namespace std;
// Macro definition
#define AREA(l, b) (l * b)
// Driver Code
int main()
{
// Given lengths l1 and l2
int l1 = 10, l2 = 5, area;
// Find the area using macros
area = AREA(l1, l2);
// Print the area
cout << "Area of rectangle"
<< " is: ",
area;
return 0;
}
输出:
Area of rectangle is: 50
解释:
从上面的程序中,我们可以看到,只要编译器在程序中找到AREA(l,b) ,它就会用宏定义(即(l * b))替换它。传递给宏模板AREA(l,b)的值也将由语句(l * b)代替。因此, AREA(10,5)等于10 * 5 。
宏的类型
- 类对象宏:类对象宏是一个简单的标识符,将由代码片段替换。之所以称为类对象,是因为它看起来像使用它的代码中的对象。它通常用于将符号名称替换为以常量表示的数字/变量。
下面是一个简单的宏的图示:
C
// C program to illustrate macros
#include
// Macro definition
#define DATE 31
// Driver Code
int main()
{
// Print the message
printf("Lockdown will be extended"
" upto %d-MAY-2020",
DATE);
return 0;
}
C++
// C++ program to illustrate macros
#include
using namespace std;
// Macro definition
#define DATE 31
// Driver Code
int main()
{
// Print the message
cout << "Lockdown will be extended"
<< " upto " << DATE
<< "-MAY-2020";
return 0;
}
输出:
Lockdown will be extended upto 31-MAY-2020
2.链宏:宏内部的宏称为链宏。在链宏中,首先扩展父宏,然后扩展子宏。
以下是链宏的图示:
C++
// C program to illustrate macros
#include
// Macro definition
#define INSTAGRAM FOLLOWERS
#define FOLLOWERS 138
// Driver Code
int main()
{
// Print the message
printf("Geeks for Geeks have %dK"
" followers on Instagram",
INSTAGRAM);
return 0;
}
C++
// C++ program to illustrate macros
#include
using namespace std;
// Macro definition
#define INSTAGRAM FOLLOWERS
#define FOLLOWERS 138
// Driver Code
int main()
{
// Print the message
cout << "Geeks for Geeks have "
<< INSTAGRAM << "K followers on Instagram!";
return 0;
}
输出:
Geeks for Geeks have 138K followers on Instagram!
- 解释:
INSTAGRAM首先进行扩展以生产FOLLOWERS 。然后,扩展的宏将被扩展以产生结果138 。这称为宏链接。 - 多行宏:类似对象的宏可以有多行。因此,要创建多行宏,您必须使用反斜杠换行符。
下面是多行宏的说明:
C
// C program to illustrate macros
#include
// Multi-line Macro definition
#define ELE 1, \
2, \
3
// Driver Code
int main()
{
// Array arr[] with elements
// defined in macros
int arr[] = { ELE };
// Print elements
printf("Elements of Array are:\n");
for (int i = 0; i < 3; i++) {
printf("%d ", arr[i]);
}
return 0;
}
C++
// C++ program to illustrate macros
#include
using namespace std;
// Multi-line Macro definition
#define ELE 1, \
2, \
3
// Driver Code
int main()
{
// Array arr[] with elements
// defined in macros
int arr[] = { ELE };
// Print elements
printf("Elements of Array are:\n");
for (int i = 0; i < 3; i++) {
cout << arr[i] << ' ';
}
return 0;
}
输出:
Elements of Array are:
1 2 3
3.类函数宏:这些宏与函数调用相同。它代替了整个代码而不是函数名。必须在宏名称后紧跟一对括号。如果我们在宏名称和宏定义中的括号之间放置一个空格,则该宏将不起作用。
仅当函数名称出现在名称后并带有一对括号的情况下,函数式宏才会被加长。如果我们不这样做,则函数指针将获取实函数的地址并导致语法错误。
下面是类似函数的宏的图示:
C
// C program to illustrate macros
#include
// Function-like Macro definition
#define min(a, b) (((a) < (b)) ? (a) : (b))
// Driver Code
int main()
{
// Given two number a and b
int a = 18;
int b = 76;
printf("Minimum value between"
" %d and %d is %d\n",
a, b, min(a, b));
return 0;
}
C++
// C++ program to illustrate macros
#include
using namespace std;
// Function-like Macro definition
#define min(a, b) (((a) < (b)) ? (a) : (b))
// Driver Code
int main()
{
// Given two number a and b
int a = 18;
int b = 76;
cout << "Minimum value between"
<< a << " and " << b
<< " is: " << min(a, b);
return 0;
}
输出:
Minimum value between 18 and 76 is 18
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。