Day#18 'C' Coding Challenge

Day#18 'C' Coding Challenge

What is the output of the below code?

#include <stdio.h> void main() { char b[10] = {'a'}; printf("%c",b[7]); int a[10] = {2}; printf("%d",a[7]); }

------------------------------------------------

The above code tests below scenarios:

1. What shall be the default initialization of remaining elements of character array which is partially initialized.
2. What shall be the default initialization of remaining elements of integer array which is partially initialized.

-----------------------------------------------

Array is an user defined data type which allocates contiguous memory locations.
If an array is just declared and not initialized, then memory itself shall not be allocated and there is no sense in accessing the array.
In case, if an array is declared and partially initialized, the remaining elements shall be initialized with zero if it is integer array and initialized with NULL if it is character array.
In case of our program there is b[10] which is character array and a[10] which is integer array. In both cases they are partially initialized.
So, when b[7] is printed, NULL is printed and when a[7] is printed, 0 shall be printed.

Post a Comment

0 Comments