- How will you define an array?
Storage class datatype array_name[size of array] - 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?
Answer:
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. - 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
#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 a[2]=0 e[2]=0
- 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.
#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 ./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
- 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’ - int a[]={1,2,3,4,5}; Is it allowed?
Answer:Yes. The number of array element is optional when initial values are present
- Consider int a[5]={10,20,30,40,50}; printf("a=%d",*a); what is the output?
#include<stdio.h> int main() { int a[5]={10,20,30,40,50}; printf("a=%d",*a); return; }
Answer:
a=10 - 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;
}
Answer:
warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
sunitha@sunitha-laptop:~/cpgm$ ./a.out
a=425449152 - 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= - 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'}; - 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;
}
output
./a.out
string=Welco 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;
}
output
error: lvalue required as increment operand - Is there any way to find the size of the array?
We can find the size of the array using sizeof operator.#include<stdio.h>int main() { int x[15]; printf("ans=%d",sizeof(x)); return; } 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
- Can I print the characters given in single quotes as string?
#include<stdio.h> int main() { char x[]={'a','e','i','o','u','\0'}; printf("ans=%s",x); return; } Output: ./a.out ans=aeiou
- What is the output for the following program?
#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 - What is the output for the following program?
./a.out#includeint main(){ int x[5]={10,20,30,40,50};printf("ans=%d",(*x)++);return;} Output:
ans=10 - What is the output for the following program?
#includeint main(){int x[5]={10,20,30,40,50};printf("ans=%d",++*x);return;} Output./a.outans=11 - Do we have any minimum or maximum size restrictions in array index?
- 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. - 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) - Can I declare an array with 0 size? int a[0]; is it allowed?
#include<stdio.h> int main() { int x[0]; *x=20; printf("ans=%d",x[0]); return; } Output ./a.out ans=20
- Can I declare an array with negative size? int a[-5]; is it allowed?
#include<stdio.h> int main() { int x[-5]; *x=20; printf("ans=%d",x[0]); return; } Output compilation error: size of array ‘x’ is negative
- 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. - Can I have comma(,) alone in the array list? int x[5]={,,,,}; Is it allowed?
error: expected expression before ‘,’ token - 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#include<stdio.h> int main() { int i; char x[3]={'a','e','i','o','u','\0'}; printf("String=%s\n",x); return; } 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]); return; } 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
- 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?
- 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.
output#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; } }
./a.out
before=aeiou
after=afkry
- Can an array can be passed from a function to the calling portion of the program via return statement?
Pages
The greatest mistake you can make in life is to be continually fearing you will make one
Tuesday, 18 January 2011
Some questions about Arrays with answers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment