C 编程语言中的宏是什么?
宏替换是一种提供字符串替换的机制。可通过“#deifne"实现。
它用于在执行程序之前用宏定义的第二部分替换第一部分。
第一个对象可以是函数类型或一个对象。
语法
宏的语法如下 −
#define first_part second_part
程序
在程序中,每次出现 first_part 时,它都将在整个代码中替换为 second_part。
#include<stdio.h>
#define square(a) a*a
int main(){
int b,c;
printf("enter b element:");
scanf("%d",&b);
c=square(b);//replaces c=b*b before execution of program
printf("%d",c);
return 0;
}输出
您将看到以下输出 −
enter b element:4 16
考虑另一个程序来解释宏的作用。
#include<stdio.h>
#define equation (a*b)+c
int main(){
int a,b,c,d;
printf("enter a,b,c elements:");
scanf("%d %d %d",&a,&b,&c);
d=equation;//replaces d=(a*b)+c before execution of program
printf("%d",d);
return 0;
}输出
您将看到以下输出 −
enter a,b,c elements: 4 7 9 37
广告
数据结构
联网
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP