Home »
C programs »
C preprocessors programs
Define a constant using Macro to use in Array declarations in C
Here, we are going to learn how and why to define a constant to declare arrays? In C programming language we can also use Macro to define a constant.
By IncludeHelp Last updated : March 10, 2024
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];
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: 10
Size of arr1: 40
Size of arr2: 40
Total elements of arr1: 10
Total elements of arr2: 10
C Preprocessors Programs »