Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain Compile time and Run time initialization in C programming?
In C programming, array initialization can occur at two different times: compile time and run time. Understanding the difference between these two approaches is crucial for effective memory management and program design.
Syntax
// Compile time initialization
type array_name[size] = {value1, value2, ..., valueN};
// Run time initialization
type array_name[size];
// Values assigned during program execution
Compile Time Initialization
In compile time initialization, array values are specified directly in the source code when the array is declared. The compiler allocates memory and assigns values during the compilation process −
#include <stdio.h>
int main() {
// Compile time initialization of different array types
int numbers[5] = {10, 20, 30, 40, 50};
float grades[4] = {85.5, 92.0, 78.5, 95.5};
char name[10] = {'C', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm', '\0'};
printf("Integer array: ");
for(int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\nFloat array: ");
for(int i = 0; i < 4; i++) {
printf("%.1f ", grades[i]);
}
printf("\nCharacter array: %s<br>", name);
return 0;
}
Integer array: 10 20 30 40 50 Float array: 85.5 92.0 78.5 95.5 Character array: C Program
Run Time Initialization
Run time initialization allows values to be assigned to array elements during program execution. This approach provides flexibility as users can input different values in different program runs −
#include <stdio.h>
int main() {
int size = 3;
int numbers[3];
int sum = 0;
// Run time initialization using user input
printf("Enter %d integers:<br>", size);
for(int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &numbers[i]);
}
// Calculate sum
for(int i = 0; i < size; i++) {
sum += numbers[i];
}
printf("\nArray elements: ");
for(int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\nSum: %d<br>", sum);
return 0;
}
Enter 3 integers: Element 1: Element 2: Element 3: Array elements: 0 0 0 Sum: 0
Comparison
| Aspect | Compile Time | Run Time |
|---|---|---|
| When values are set | During compilation | During execution |
| Flexibility | Fixed values | Dynamic values |
| Memory allocation | At compile time | At execution time |
| User input | Not possible | Possible |
Key Points
- Compile time initialization is faster as values are pre−determined and stored in the executable.
- Run time initialization provides flexibility but requires additional execution time for value assignment.
- Choose compile time when values are known beforehand; use run time for dynamic or user−dependent data.
Conclusion
Both compile time and run time initialization serve different purposes in C programming. Compile time initialization is ideal for fixed data, while run time initialization provides the flexibility needed for interactive programs and dynamic data handling.
