- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain the concept of Uninitialized array accessing in C language
Problem
In C language, is the program executed, if we use an uninitialized array?
Solution
If we use any uninitialized array, compiler will not generate any compilation and an execution error.
If an array is uninitialized, you may get unpredictable result.
So, it’s better we should always initialize the array elements with default values.
Example Program
Following is the C program of accessing an uninitialized array −
#include <stdio.h> int main(void){ int a[4]; int b[4] = {1}; int c[4] = {1,2,3,4}; int i; //for loop counter //printing all alements of all arrays printf("
Array a:
"); for( i=0; i<4; i++ ) printf("arr[%d]: %d
",i,a[i]); printf("
Array b:
"); for( i=0; i<4; i++) printf("arr[%d]: %d
",i,b[i]); printf("
Array c:
"); for( i=0; i<4; i++ ) printf("arr[%d]: %d
",i, c[i]); return 0; }
Output
When the above program is executed, it produces the following result −
Array a: arr[0]: 4195872 arr[1]: 0 arr[2]: 4195408 arr[3]: 0 Array b: arr[0]: 1 arr[1]: 0 arr[2]: 0 arr[3]: 0 Array c: arr[0]: 1 arr[1]: 2 arr[2]: 3 arr[3]: 4
Note
If we didn’t initialize an array, by default, it prints garbage values and never show an error.
Consider another C program for accessing an uninitialized array −
Example
#include <stdio.h> int main(void){ int A[4]; int B[4] ; int C[4] = {1,2}; int i; //for loop counter //printing all alements of all arrays printf("
Array a:
"); for( i=0; i<4; i++ ) printf("arr[%d]: %d
",i,A[i]); printf("
Array b:
"); for( i=0; i<4; i++) printf("arr[%d]: %d
",i,B[i]); printf("
Array c:
"); for( i=0; i<4; i++ ) printf("arr[%d]: %d
",i, C[i]); return 0; }
Output
When the above program is executed, it produces the following result −
Array a: arr[0]: 4195856 arr[1]: 0 arr[2]: 4195408 arr[3]: 0 Array b: arr[0]: -915120393 arr[1]: 32767 arr[2]: 0 arr[3]: 0 Array c: arr[0]: 1 arr[1]: 2 arr[2]: 0 arr[3]: 0
- Related Articles
- Explain the concept of pointer accessing in C language
- Explain the concept of Sorting in C language
- Explain the concept of stack in C language
- Explain the concept of pointers in C language
- Explain the accessing of structure variable in C language
- Explain the Random accessing files in C language
- Explain the concept of Arithmetic operators in C language
- Explain the concept of Linked list in C language
- Explain the concept of union of structures in C language
- Explain the concept of one and two dimensional array processing using C language
- Explain the concept of logical and assignment operator in C language
- Explain the concept of pointer to pointer and void pointer in C language?
- Explain bit field in C language by using structure concept
- Explain the array of structures in C language
- Explain array of pointers in C programming language

Advertisements