You are on page 1of 6

1.

How will you define an array?

Storage class datatype array_name[size of array]


2. Example: 1. 2. 3. Answer: static int sum[8]; auto float fun[20]; (or) float fun[20];

Is it necessary to specify the storage class in array definition? what is the default storage class for arrays?

Storage class is optional. If the array is defined with in a function or a block the default storage class is auto. If the array is defined outside of function then the default storage class is extern. 4. If the array elements are not initialized then what is the default initial value?for example: int x[5]; what is the value of x[1]? Answer: If the array is not initialized then the array has garbage value. If the array is a external or global array then the default initial value is 0 Example: #include<stdio.h> int main() { int a[5]; printf("%d",a[2]); return; } Output: ./a.out 195392

5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. a[2]=0

#include<stdio.h> int e[3]; int main() { int i,a[3]; for(i=0;i<3;i++) { printf("a[%d]=%d\t",i,a[i]); printf("e[%d]=%d\n",i,e[i]); } return; } output a[0]=-1530409104 e[0]=0 a[1]=32767 e[1]=0 e[2]=0

20. Consider we have a 10 element array what happen if we try to access the 12th element? There is no bound checking in array. So if we try to access the array element out of its size then it will return garbage value.

21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33.

#include<stdio.h> int e[10]; int main() { int i,a[10]; for(i=10;i<15;i++) { printf("a[%d]=%d\t",i,a[i]); printf("e[%d]=%d\n",i,e[i]); } return; } output

34. 35. 36. 37. 38. 39. 40.

./a.out a[10]=0 e[10]=0 a[11]=11 e[11]=0 a[12]=0 e[12]=0 a[13]=0 e[13]=0 a[14]=366693453 e[14]=0

41. Can I declare the array without specifying the size? int a[]; is it allowed? Answer: No its not allowed. error: array size missing in a 42. int a[]={1,2,3,4,5}; Is it allowed? Answer:Yes. The number of array element is optional when initial values are present 43. Consider int a[5]={10,20,30,40,50}; printf("a=%d",*a); what is the output?

44. 45. 46. 47. 48. 49. 50. }

#include<stdio.h> int main() { int a[5]={10,20,30,40,50}; printf("a=%d",*a); return;

Answer: a=10 51. Consider int a[5]={10,20,30,40,50}; printf("a=%d",a); what is the output? We cannot print the entire array elements using only the array name

#include<stdio.h> int main() { int a[5]={10,20,30,40,50}; printf("a=%d",a); return; }


52. Answer: warning: format %d expects type int, but argument 2 has type int * sunitha@sunitha-laptop:~/cpgm$ ./a.out a=425449152 53. The last character in a array must be a NULL(\0) character what happen if I missed the '\0' character? char vowels[6]={'a','e','i','o','u'}; Is it correct? int main() { char vowels[6]={ 'a','e','i','o','u'}; printf("char=%c",vowels[5]); return; } Output ./a.out char= 54. What happen If I am not giving space for '\0' character? char flag[4]={'T','R','U','E'}; is it correct? Answer This type of declaration is incorrect but the compiler wont show any error. but flag[5]={'T','R','U','E'}; is the correct form (or) flag[]={'T','R','U','E'}; 55. What happen If I am not giving space for '\0' character in string? char str[5]="Welco"; is it correct? Answer Some unwanted character will be printed at the end of the string.

#include<stdio.h> int main() { char str[5]="Welco"; printf("string=%s to india",str); return; }


56. output ./a.out string=Welco 57.

to india

x is a array pointer. what is the value for *x++?

#include<stdio.h> int main() { int x[5]={10,20,30,40,50}; printf("ans=%d",*x++); return;

}
58. output error: lvalue required as increment operand 59. Is there any way to find the size of the array? We can find the size of the array using sizeof operator.

60. #include<stdio.h>int main() 61. { 62. int x[15]; 63. printf("ans=%d",sizeof(x)); 64. return; 65. } 66. Output: ./a.out ans=60

#include<stdio.h> int main() { char x[]={'a','e','i','o','u','\0'}; printf("ans=%d",sizeof(x)); return; } Output: ./a.out ans=6
67. Can I print the characters given in single quotes as string?

68. 69. 70. 71. 72. 73. 74. 75.

#include<stdio.h> int main() { char x[]={'a','e','i','o','u','\0'}; printf("ans=%s",x); return; } Output:

./a.out ans=aeiou
76. What is the output for the following program?

77. 78. 79. 80. 81. 82. }

#include<stdio.h> int main() { char x[]={'a','e','i','o','u'}; printf("ans=%s",x); return;

Answer: ans=aeiou Some unwanted character is printed at the end of the string 83. What is the output for the following program?

#include int main() { int x[5]={10,20,30,40,50}; printf("ans=%d",(*x)++); return; } Output:


./a.out ans=10 84.

What is the output for the following program?

#include int main() { int x[5]={10,20,30,40,50}; printf("ans=%d",++*x); return; } Output ./a.out ans=11
85. Do we have any minimum or maximum size restrictions in array index? 86. What is the maximum index(sixe) number an array can have? The maximum size of an indexed array is 232 - 1 or 4,294,967,295. An attempt to create an array that is larger than the maximum size results in a run-time error. 87. Why array index is starting from zero? (or) Why arrays not store values from 1? a[n] has to implemented internally as *(a+n) so it is easy if it starts from 0; otherwise it has to be *(a+n-1)

88.

Can I declare an array with 0 size? int a[0]; is it allowed?

89. 90. 91. 92. 93. 94. 95. 96. 97.

#include<stdio.h> int main() { int x[0]; *x=20; printf("ans=%d",x[0]); return; }

98. Output ./a.out ans=20


99. Can I declare an array with negative size? int a[-5]; is it allowed?

100. #include<stdio.h> 101. int main() 102. { 103. int x[-5]; 104. *x=20; 105. printf("ans=%d",x[0]); 106. return; 107. } 108. Output compilation error: size of array x is negative
109. Can I compare 2 arrays with single operator? Single operations which involve entire arrays are not permitted in c. Thus if a and b are similar arrays (ie same datatype, same dimension and same size) assignment operations, comparison operations must be carried out on element by element basis. 110. Can I have comma(,) alone in the array list? int x[5]={,,,,}; Is it allowed? error: expected expression before , token 111. What happen if the array size is less than the number of initialized values?int a[5]={1,2,3,4,5,6,7,8,9,}; is it allowed? Answer It will give warning as "excess elements in array initializer" but it will produce the correct output for the array size and the remaining elements are garbage value. So the total output is incorrect

112. #include<stdio.h> 113. int main() 114. { 115. int i; 116. char x[3]={'a','e','i','o','u','\0'}; 117. printf("String=%s\n",x); 118. return; 119. } 120. Output ./a.out String=aeiQ

#include<stdio.h> int main() { int i,a[3]={1,2,3,4,5,6,7,8,9}; for(i=0;i<9;i++) printf("a[%d]=%d\n",i,a[i]); } output ./a.out a[0]=1 a[1]=2 a[2]=3 a[3]=3 a[4]=0 a[5]=0 a[6]=-1332024243 a[7]=32532 a[8]=0

return;

121. 122.

How to pass and access the array values to the function using call by value method? How to pass and access the array values to the function using call by reference method?

123. Array name is a pointer. If an array is passed to a function and several of its elements are altered with in the function, are these changes recognized in the calling portion of the program? Answer:If the array element is altered within the function, the alteration will be recognized in the calling portion of the program.

124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. }

#include<stdio.h> void fun(char x[],int ); int main() { char x[]={'a','e','i','o','u','\0'}; printf("before=%s\n",x); fun(x,5); printf("after=%s",x); return; } void fun(char x[],int n) { int i; for(i=0;i<n;i++) { x[i]=x[i]+i; }

output ./a.out before=aeiou after=afkry 141. Can an array can be passed from a function to the calling portion of the program via return statement?

You might also like