Q1. main(){
extern out;
printf("%d", out);}
int out=100;
Answer:
100
Explanation:
This is the correct way of writing the previous program.
Q2. main(){
show();}
void show(){
printf("I'm the greatest");}
Answer:
Compier error: Type mismatch in redeclaration of show.
Explanation:
When
the compiler sees the function show it doesn't know anything about it.
So the default return type (ie, int) is assumed. But when compiler sees
the actual definition of show mismatch occurs since it is declared as
void. Hence the error.
The solutions are as follows:
1. declare void show() in main() .
2. define show() before main().
3. declare extern void show() before the use of show().
Q.3 main( ){
int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
printf(“%u %u %u %d \n”,a,*a,**a,***a);
printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1);
}
Answer:
100, 100, 100, 2
114, 104, 102, 3
Q.4 main( ){
int a[ ] = {10,20,30,40,50},j,*p;
for(j=0; j<5; j++)
{
printf(“%d” ,*a);
a++;
}
p = a;
for(j=0; j<5; j++)
{
printf(“%d ” ,*p);
p++;
}
}
Answer:
Compiler error: lvalue required.
Explanation:
Error
is in line with statement a++. The operand must be an lvalue and may be
of any of scalar type for the any operator, array name only when
subscripted is an lvalue. Simply array name is a non-modifiable lvalue.
Q.5 main( ){
static int a[ ] = {0,1,2,3,4};
int *p[ ] = {a,a+1,a+2,a+3,a+4};
int **ptr = p;
ptr++;
printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr);
*ptr++;
printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr);
*++ptr;
printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr);
++*ptr;
printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr); }
Answer:
111
222
333
344
Q.6 main( ){
void *vp;
char ch = ‘g’, *cp = “goofy”;
int j = 20; vp = &ch;
printf(“%c”, *(char *)vp); vp = &j;
printf(“%d”,*(int *)vp); vp = cp;
printf(“%s”,(char *)vp + 3);}
Answer:
g20fy
Explanation:
Since
a void pointer is used it can be type casted to any other type
pointer. vp = &ch stores address of char ch and the next statement
prints the value stored in vp after type casting it to the proper data
type pointer. the output is ‘g’. Similarly the output from second
printf is ‘20’. The third printf statement type casts it to print the
string from the 4th value hence the output is ‘fy’.
Q.7 main ( ){
static char *s[ ] = {“black”, “white”, “yellow”, “violet”};
char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
**++p;
printf(“%s”,*--*++p + 3);}
Answer:
ck
Explanation:
In
this problem we have an array of char pointers pointing to start of 4
strings. Then we have ptr which is a pointer to a pointer of type char
and a variable p which is a pointer to a pointer to a pointer of type
char. p hold the initial value of ptr, i.e. p = s+3. The next statement
increment value in p by 1 , thus now value of p = s+2. In the printf
statement the expression is evaluated *++p causes gets value s+1 then
the pre decrement is executed and we get s+1 – 1 = s . the indirection
operator now gets the value from the array of s and adds 3 to the
starting address. The string is printed starting from this position.
Thus, the output is ‘ck’.
Q.8 main(){
int i, n;
char *x = “girl”;
n = strlen(x);
*x = x[n];
for(i=0; i {
printf(“%s\n”,x);
x++;
}
}
Answer:
(blank space)
irl
rl
l
Explanation:
Here a string (a pointer to char) is initialized with a value “girl”.
The strlen function returns the length of the string, thus n has a value
4. The next statement assigns value at the nth location (‘\0’) to the
first location. Now the string becomes “\0irl” . Now the printf
statement prints the string after each iteration it increments it
starting position. Loop starts from 0 to 4. The first time x[0] = ‘\0’
hence it prints nothing and pointer value is incremented. The second
time it prints from x[1] i.e “irl” and the third time it prints “rl” and
the last time it prints “l” and the loop terminates.