c语言用宏定义常量
As we know that, while declaring an array we need to pass maximum number of elements, for example, if you want to declare an array for 10 elements. You need to pass 10 while declaring. Example: int arr[10];
众所周知,在声明数组时,例如,如果要声明一个包含10个元素的数组,则需要传递最大数量的元素。 您需要在声明时通过10 。 示例: int arr [10];
But, there is a good way, to define a constant by using Macro for it, so that we can easily edit, when required.
但是,有一种很好的方法,可以使用宏为其定义常量,以便我们可以在需要时轻松进行编辑。
Macro definition:
宏定义:
#define MAX 10
Example:
例:
#include <stdio.h>
#define MAX 10
int main()
{
int arr1[MAX];
int arr2[MAX];
printf("Maximum elements of the array: %d\n",MAX);
printf("Size of arr1: %d\n",sizeof(arr1));
printf("Size of arr2: %d\n",sizeof(arr2));
printf("Total elements of arr1: %d\n",sizeof(arr1)/sizeof(int));
printf("Total elements of arr2: %d\n",sizeof(arr2)/sizeof(int));
return 0;
}
Output
输出量
Maximum elements of the array: 10Size of arr1: 40Size of arr2: 40Total elements of arr1: 10Total elements of arr2: 10
翻译自: https://www.includehelp.com/c-programs/define-a-constant-using-macro-to-use-in-array-declarations-in-c.aspx
c语言用宏定义常量