Define a constant using Macro to use in Array declarations in 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];
But, there is a good way, to define a constant by using Macro for it, so that we can easily edit, when required.
#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
Example:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer