void main()
{
int a[10];
printf("%d %d", a[-1], a[12]);
} In c compiler does not check array with its bounds, value at the computed location is displayed.
Learn competitive and Technical Aptitude C programming mcq questions and answers on Arrays and Strings with easy and logical explanations. Page-5 section-1
void main()
{
int a[10];
printf("%d %d", a[-1], a[12]);
} In c compiler does not check array with its bounds, value at the computed location is displayed.
#include<stdio.h>
void main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
printf("%u, %u", a+1, &a+1);
} >> int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; The array a[3][4] is declared as an integer array having the 3 rows and 4 colums dimensions.
>> printf("%u, %u\n", a+1, &a+1);
The base address(also the address of the first element) of array is 65472.
For a two-dimensional array like a reference to array has type "pointer to array of 4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480
Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".
Hence, begining address 65472 + 24 = 65496. So, &a+1 = 65496
Hence the output of
#include<stdio.h>
int main()
{
int arr[1] = {10};
printf("%d", 0[arr]);
return 0;
} >> int arr[1]={10}; The variable arr[1] is declared as an integer array with size '2' i.e. arr[0] and arr[1] and it's first element is initialized to value '10'(means arr[0]=10)
and arr[1] = garbage value or zero
>> printf("%d", 0[arr]); It prints the first element value of the variable arr.
Hence the output of the program is 10.
#include<stdio.h>
void main()
{
int arr[] = {12, 14, 15, 23, 45};
printf("%u, %u", arr, &arr);
} >> int arr[] = {12, 14, 15, 23, 45}; The variable arr is declared as an integer array and initialized.
>> printf("%u, %u", arr, &arr); Here,
The base address of the array is 65486.
=> arr, &arr is pointing to the base address of the array arr.
Hence the output of the program is 65486, 65486.
#include<stdio.h>
void main()
{
float arr[] = {12.4, 2.3, 4.5, 6.7};
printf("%d", sizeof(arr)/sizeof(arr[0]));
} The sizeof function return the given variable. Example: float a=10; sizeof(a) is 4 bytes
>> float arr[] = {12.4, 2.3, 4.5, 6.7}; The variable arr is declared as an floating point array and it is initialized with the values.
>> printf("%d", sizeof(arr)/sizeof(arr[0]));
The variable arr has 4 elements. The size of the float variable is 4 bytes.
Hence 4 elements x 4 bytes = 16 bytes
sizeof(arr[0]) is 4 bytes
Hence 16/4 is 4 bytes
Hence the output of the program is '4'.
#include<stdio.h>
void main()
{
int a[3][4];
fun(a);
} void fun(int p[][4]){ } is the correct way to write the function fun(). while the others are considered only the function fun() is called by using call by reference.
#include<stdio.h>
void main()
{
int size, i;
scanf("%d", &size);
int arr[size];
for(i=1; i<=size; i++)
{
scanf("%d", arr[i]);
printf("%d", arr[i]);
}
} The statement int arr[size]; produces an error, because we cannot initialize the size of array dynamically. Constant expression is required here.
Example: int arr[10];
One more point is there, that is, usually declaration is not allowed after calling any function in a current block of code. In the given program the declaration int arr[10]; is placed after a function call scanf().
1. The array int num[26]; can store 26 elements. This statement is true.
2. The expression num[1] designates the very first element in the array. This statement is false, because it designates the second element of the array.
3. It is necessary to initialize the array at the time of declaration. This statement is false.
4. The declaration num[SIZE] is allowed if SIZE is a macro. This statement is true, because the MACRO just replaces the symbol SIZE with given value.
Hence the statements '1' and '4' are correct statements.