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