In this video tutorial lets see how we can assign size of an array using macros and constants.
Related Read:
For Loop In C Programming Language
Introduction To Arrays: C Programming Language
Keywords, Constants, Variables: C
Page Contents
Disadvantage of Not Using Macro or Constant To Assign Array Size
If requirement of the program/software changes and you need to increase or decrease the array size, then you’ll have to be very careful and scan through the entire source code and make changes at multiple locations. Even if you skip changing the array size information at one place, you’ll start getting wrong results.
And if you have any business logic which makes use of array size, then you’ll have hard time rectifying and debugging the code. It’ll take unnecessary time and effort to make it work correctly once again.
By making use of Macros or constant variables you can handle this very efficiently. You can make the change at one place and it’ll take effect at all the places in your source code.
Watch the video below for demonstration of effectiveness of using macros and constants for assigning size of an array.
Video Tutorial: Assign Size of Array using Macro and Constant: C Program
[youtube https://www.youtube.com/watch?v=MQCyoPbaAD4]
Source Code: Size of Array using Macro and Constant: C Program
Without using Macros and/or constants
#include<stdio.h> int main() { int a[5], i; printf("Enter 5 integer numbers\n"); for(i = 0; i < 5; i++) scanf("%d", &a[i]); printf("Array elements are:\n"); for(i = 0; i < 5; i++) printf("%d\n", a[i]); return 0; }
Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
1
2
3
4
5
Here we have a array variable with size 5. We enter 5 integer variables and we display those elements using for loop. In for loop condition we mention the number of times it has to iterate.
Now assume that the requirement changes and we need to increase the size of array from 5 to 7:
#include<stdio.h> int main() { int a[7], i; printf("Enter 7 integer numbers\n"); for(i = 0; i < 7; i++) scanf("%d", &a[i]); printf("Array elements are:\n"); for(i = 0; i < 7; i++) printf("%d\n", a[i]); return 0; }
Output:
Enter 7 integer numbers
1
2
3
4
5
6
7
Array elements are:
1
2
3
4
5
6
7
As you can see we made edits at 4 places. Its a very simple program and even in that we had to make 4 edits. What if the program is huge and the requirement changes?
Macros
Assign Array size using Macros
#include<stdio.h> #define N 5 int main() { int a[N], i; printf("Enter %d integer numbers\n", N); for(i = 0; i < N; i++) scanf("%d", &a[i]); printf("Array elements are:\n"); for(i = 0; i < N; i++) printf("%d\n", a[i]); return 0; }
Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
1
2
3
4
5
Observe the changes we’ve made in the source code. We’re defining a macro here. Macro template is N and macro expansion is 5. We replace the value 5 inside main method by macro name N.
#include<stdio.h> #define N 7 int main() { int a[N], i; printf("Enter %d integer numbers\n", N); for(i = 0; i < N; i++) scanf("%d", &a[i]); printf("Array elements are:\n"); for(i = 0; i < N; i++) printf("%d\n", a[i]); return 0; }
Output:
Enter 7 integer numbers
1
2
3
4
5
6
7
Array elements are:
1
2
3
4
5
6
7
Assume that the requirement changes and the client wants a list size of 7. Now instead of editing at multiple places, we only change the macro expansion from 5 to 7, and it starts working as intended.
Constants
Assign Array size using Constants
#include<stdio.h> int main() { const int N = 5; int a[N], i; printf("Enter %d integer numbers\n", N); for(i = 0; i < N; i++) scanf("%d", &a[i]); printf("Array elements are:\n"); for(i = 0; i < N; i++) printf("%d\n", a[i]); return 0; }
Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
1
2
3
4
5
Observe the changes we’ve made in the source code. We’ve declared and initialized a constant variable N. Constant variable name is N and its value is 5. We replace the value 5 inside main method by constant variable N.
#include<stdio.h> int main() { const int N = 7; int a[N], i; printf("Enter %d integer numbers\n", N); for(i = 0; i < N; i++) scanf("%d", &a[i]); printf("Array elements are:\n"); for(i = 0; i < N; i++) printf("%d\n", a[i]); return 0; }
Output:
Enter 7 integer numbers
1
2
3
4
5
6
7
Array elements are:
1
2
3
4
5
6
7
Assume that the requirement changes and the client wants a list size of 7. Now instead of editing at multiple places, we only change the value of constant variable N from 5 to 7, and the program works as intended.
Note: It’s always considered best practice to either use macros or constant variables to assign array size. This practice will prove to be very advantageous while writing big programs.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert