171) void
main()
{
char ch;
for(ch=0;ch<=127;ch++)
printf(“%c %d \n“, ch, ch);
}
Answer:
Implementaion dependent
Explanation:
The char
type may be signed or unsigned by default. If it is signed then ch++ is
executed after ch reaches 127 and rotates back to -128. Thus ch is always
smaller than 127.
172) Is
this code legal?
int *ptr;
ptr =
(int *) 0x400;
Answer:
Yes
Explanation:
The
pointer ptr will point at the integer in the memory location 0x400.
173) main()
{
char
a[4]="HELLO";
printf("%s",a);
}
Answer:
Compiler
error: Too many initializers
Explanation:
The array
a is of size 4 but the string constant requires 6 bytes to get stored.
174) main()
{
char
a[4]="HELL";
printf("%s",a);
}
Answer:
HELL%@!~@!@???@~~!
Explanation:
The
character array has the memory just enough to hold the string “HELL” and doesnt
have enough space to store the terminating null character. So it prints the
HELL correctly and continues to print garbage values till it accidentally comes across a NULL
character.
175) main()
{
int
a=10,*j;
void *k;
j=k=&a;
j++;
k++;
printf("\n
%u %u ",j,k);
}
Answer:
Compiler
error: Cannot increment a void pointer
Explanation:
Void
pointers are generic pointers and they can be used only when the type is not
known and as an intermediate address storage type. No pointer arithmetic can be
done on it and you cannot apply indirection operator (*) on void pointers.
176) main()
{
extern
int i;
{
int i=20;
{
const volatile unsigned i=30;
printf("%d",i);
}
printf("%d",i);
}
printf("%d",i);
}
int
i;
177) Printf
can be implemented by using __________
list.
Answer:
Variable
length argument lists
178) char *someFun()
{
char *temp = “string constant";
return temp;
}
int main()
{
puts(someFun());
}
Answer:
string constant
Explanation:
The program suffers no problem and
gives the output correctly because the character constants are stored in
code/data area and not allocated in stack, so this doesn’t lead to dangling
pointers.
179) char *someFun1()
{
char temp[ ] = “string";
return temp;
}
char *someFun2()
{
char temp[ ] = {‘s’,
‘t’,’r’,’i’,’n’,’g’};
return temp;
}
int main()
{
puts(someFun1());
puts(someFun2());
}
Answer:
Garbage
values.
Explanation:
Both
the functions suffer from the problem of dangling pointers. In someFun1() temp
is a character array and so the space for it is allocated in heap and is
initialized with character string “string”. This is created dynamically as the
function is called, so is also deleted dynamically on exiting the function so
the string data is not available in the calling function main() leading to
print some garbage values. The function someFun2() also suffers from the same
problem but the problem can be easily identified in this case.
No comments:
Post a Comment