option (B), (C) and (D) are incorrect because array declaration syntax is wrong. Only square brackets([]) must be used for declaring an array.
Arrays and Strings - C Programming MCQ Questions and Answers
Learn competitive and Technical Aptitude C programming mcq questions and answers on Arrays and Strings with easy and logical explanations. Page-4 section-1
#include<stdio.h>
void main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
} >> int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25.
>> int i, j, m; The variable i, j, m are declared as an integer type.
>> i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
>> j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
>> m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
>> printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15.
#include <stdio.h>
int main(void)
{
char p;
char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
p = (buf + 1)[5];
printf("%d", p);
return 0;
} x[i] is equivalent to *(x + i),
so (buf + 1)[5] is *(buf + 1 + 5), i.e. buf[6].
1. Memory Contiguity:
When an array is declared, the compiler allocates a block of memory large enough to hold all the elements. These elements are stored one after the other in contiguous memory addresses. For example, if you declare an array of 5 integers, the memory allocated for the array will be continuous, and each integer will be placed next to the previous one.
2. Accessing Elements:
Due to the sequential storage, the array elements can be accessed easily using their index. The index represents the position of the element in the array, and since the memory is sequential, calculating the memory address of any element becomes straightforward. The address of any element in the array can be calculated as:
Address=Base Address+(Index×Size of each element)
Here, the Base Address is the memory address of the first element in the array.
3. Efficiency:
The sequential nature of arrays makes accessing and iterating through elements efficient. Since the elements are stored contiguously, the CPU can access them quickly by simply moving through the memory locations.
4. Contrast with Other Storage Methods:
Unlike arrays, other data structures like linked lists store elements in non-contiguous memory locations. This requires pointers to link elements together, which can lead to more complex memory management and slower access times compared to arrays.
5. Implications for Multidimensional Arrays:
In multidimensional arrays (e.g., 2D arrays), the elements are still stored sequentially in memory, typically in row-major order. This means the elements of the first row are stored first, followed by the elements of the second row, and so on.
In summary, the sequential storage of array elements is a fundamental characteristic of arrays in C, contributing to their simplicity and efficiency in memory usage and access.
I. ++x
II. x+1
III. x++
IV. x*2
Statement I, III and IV is invalid.
Statement I and III : ++x and x++ are throwing en error while compile (lvalue required as increment operand )
Since, x is storing in the address of the array which is static value which cannot be change by the operand.
Statement IV : x*2 is also throw an error while compile (invalid operands to binary * (have 'int *' and 'int') )
Statement II : x+1 is throw a warning: assignment makes integer from pointer without a cast [enabled by default]
Initialization during Definition:
When an array is defined and initialized at the same time, the size of the array can be omitted. The size is automatically determined by the number of elements provided in the initialization. For example:
int arr[] = {1, 2, 3, 4, 5}; // Size is determined by the number of elements
Declaration:
When declaring an array without initializing it, the size must be specified if the array is not initialized. For example:
int arr[5]; // Size must be specified
Formal Parameter:
In function prototypes or definitions where an array is used as a formal parameter, the size does not need to be specified. For example:
void function(int arr[]); // Size not required
All of These:
This option is incorrect because the size must be specified in cases other than initialization during definition.
Therefore, the correct answer is Option A: Initialization is a part of definition.
typedef char x[10];
x myArray[5];What will sizeof(myArray) be ? (Assume one character occupies 1 byte)
typedef char x[10]; creates a new type x, which is an array of 10 characters. Then, an array of 5 elements of type x is declared as x myArray[5];.To calculate the size of
myArray, you can multiply the size of one element (sizeof(x)) by the number of elements (5):sizeof(myArray) = sizeof(x) * 5Since each element
x is an array of 10 characters, and assuming each character occupies 1 byte, the size of x is 10 bytes. Therefore:sizeof(myArray) = 10 bytes * 5 = 50 bytesSo,
sizeof(myArray) will be 50 bytes. void main()
{
int arr[10] = {1,2,3,4,5};
printf("%d", arr[5]);
} When an array is partially initialized at the time of declaration then the remaining elements of the array is initialized to 0 by default.
void main()
{
char str1[] = "abcd";
char str2[] = "abcd";
if(str1==str2)
printf("Equal");
else
printf("Unequal");
} Strings are compared using strcmp() function defined under string.h header file.