Q:

Define a constant using Macro to use in Array declarations in C

belongs to collection: C Preprocessors Programs

0

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.

Macro definition:

    #define MAX 10 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to redefine a Macro in C?... >>
<< Define a Macro to set Nth bit to Zero in C...