Note : All the programs are tested under Turbo C/C++
compilers.
It is assumed
that,
Ø
Programs run under DOS environment,
Ø
The underlying machine is an x86 system,
Ø
Program is compiled using Turbo C/C++ compiler.
The program
output may depend on the information based on this assumptions (for example
sizeof(int) == 2 may be assumed).
Predict the output or error(s)
for the following:
1. main()
{
char
s[ ]="man";
int
i;
for(i=0;s[
i ];i++)
printf("\n%c%c%c%c",s[
i ],*(s+i),*(i+s),i[s]);
}
Answer:
mmmm
aaaa
nnnn
Explanation:
s[i],
*(i+s), *(s+i), i[s] are all different ways of expressing the same idea.
Generally array name is the base address
for that array. Here s is the base
address. i is the index
number/displacement from the base address. So, indirecting it with * is same as
s[i]. i[s] may be surprising. But in the
case of C it is same as s[i].
2. main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++) {
printf("
%d ",*c);
++q; }
for(j=0;j<5;j++){
printf("
%d ",*p);
++p; }
}
Answer:
2
2 2 2 2 2 3 4 6 5
Explanation:
Initially
pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value
2 will be printed 5 times. In second loop p
itself is incremented. So the values 2 3 4 6 5 will be printed.
3. VImp. main()
{
char
string[]="Hello World";
display(string);
}
void display(char *string)
{
printf("%s",string);
}
Answer:
Compiler Error
: Type mismatch in redeclaration of function display
Explanation
:
In
third line, when the function display
is encountered, the compiler doesn't know anything about the function display.
It assumes the arguments and return types to be integers, (which is the default
type). When it sees the actual function display,
the arguments and type contradicts with what it has assumed previously. Hence a
compile time error occurs.
4. VImp.#include<stdio.h>
main()
{
char
s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%d",++*p +
++*str1-32);
}
Answer:
77
Explanation:
p
is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p
is pointing to '\n' and that is incremented by one." the ASCII value of
'\n' is 10, which is then incremented to 11. The value of ++*p is 11. ++*str1,
str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII
value of 'b' is 98.
Now performing (11 + 98 – 32), we get
77("M");
So we get the output 77 :: "M"
(Ascii is 77).
5. Imp. Need to check. #include<stdio.h>
main()
{
int a[2][2][2] = { {10,2,3,4},
{5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}
Answer:
SomeGarbageValue---1
Explanation:
p=&a[2][2][2] you declare only two 2D arrays, but you are
trying to access the third 2D(which you are not declared) it will print garbage
values. *q=***a starting address of a is assigned integer pointer. Now q is
pointing to starting address of a. If you print *q, it will print first element
of 3D array.
6. Imp. #include<stdio.h>
main()
{
struct xx
{
int x=3;
char name[]="hello";
};
struct xx *s;
printf("%d",s->x);
printf("%s",s->name);
}
Answer:
Compiler
Error
Explanation:
You
should not initialize variables in declaration
7. Main()
{
static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
int i;
char *t;
t=names[3];
names[3]=names[4];
names[4]=t;
for (i=0;i<=4;i++)
printf("%s",names[i]);
}
Answer:
Compiler
error: Lvalue required in function main
Explanation:
Array
names are pointer constants. So it cannot be modified.
8. #include<stdio.h>
main()
{
char s[]={'a','b','c','\n','c','\0'};
char *p,*str,*str1;
p=&s[3];
str=p;
str1=s;
printf("%d",++*p + ++*str1-32);
}
Answer:
M
Explanation:
p
is pointing to character '\n'.str1 is pointing to character 'a' ++*p
meAnswer:"p is pointing to '\n' and that is incremented by one." the
ASCII value of '\n' is 10. then it is incremented to 11. the value of ++*p is
11. ++*str1 meAnswer:"str1 is pointing to 'a' that is incremented by 1 and
it becomes 'b'. ASCII value of 'b' is 98. both 11 and 98 is added and result is
subtracted from 32.
i.e.
(11+98-32)=77("M");
9. #include<stdio.h>
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
Explanation:
The given array is a 3-D
one. It can also be viewed as a 1-D array.
2
|
4
|
7
|
8
|
3
|
4
|
2
|
2
|
2
|
3
|
3
|
4
|
100
102 104 106 108
110 112 114
116 118 120
122
thus, for the
first printf statement a, *a, **a give
address of first element . since the
indirection ***a gives the value. Hence, the first line of the output.
for the second
printf a+1 increases in the third dimension thus points to value at 114, *a+1
increments in second dimension thus points to 104, **a +1 increments the first
dimension thus points to 102 and ***a+1 first gets the value at first location
and then increments it by 1. Hence, the output.
10. #include<stdio.h>
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.
No comments:
Post a Comment