Note : All the programs are tested under Turbo C/C++
compilers.
All the programs
are tested on DOS environment, machine is an x86 system and 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()
{
printf("%p",main);
}
Answer:
Some address will be
printed.
Explanation:
Function
names are just addresses (just like array names are addresses).
main() is also
a function. So the address of function main will be printed. %p in printf
specifies that the argument is an address. They are printed as hexadecimal
numbers.
2. main()
{
clrscr();
}
clrscr();
Answer:
No
output/error
Explanation:
The
first clrscr() occurs inside a function. So it becomes a function call. In the
second clrscr(); is a function declaration (because it is not inside any
function).
3. main()
{
int
i;
printf("%d",scanf("%d",&i)); // value 10 is given as input here
}
Answer:
1
Explanation:
Scanf
returns number of items successfully read and not 1/0. Here 10 is given as input which should have
been scanned successfully. So number of items read is 1.
4. main()
{
printf("%d",
out);
}
int
out=100;
Answer:
Compiler
error: undefined symbol out in function main.
Explanation:
The
rule is that a variable is available for use from the point of declaration.
Even though a is a global variable, it is not available for main. Hence an
error.
5. 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().
6. main()
{
char name[10],s[12];
scanf("
\"%[^\"]\"",s);
}
How scanf will execute?
Answer:
First
it checks for the leading white space and discards it.Then it matches with a
quotation mark and then it reads all
character upto another quotation mark.
7. main()
{
main();
}
Answer:
Runtime error : Stack overflow.
Explanation:
main
function calls itself again and again. Each time the function is called its
return address is stored in the call stack. Since there is no condition to
terminate the function call, the call stack overflows at runtime. So it
terminates the program and results in an error.
8. main()
{
int k=1;
printf("%d==1 is
""%s",k,k==1?"TRUE":"FALSE");
}
Answer:
1==1
is TRUE
Explanation:
When
two strings are placed together (or separated by white-space) they are
concatenated (this is called as "stringization" operation). So the
string is as if it is given as "%d==1 is %s". The conditional
operator( ?: ) evaluates to "TRUE".
9. main()
{
int y;
scanf("%d",&y); //
input given is 2000
if( (y%4==0 && y%100 != 0)
|| y%100 == 0 )
printf("%d is a leap year");
else
printf("%d is not a leap year");
}
Answer:
2000
is a leap year
Explanation:
An
ordinary program to check if leap year or not.
10. main()
{
int i=_l_abc(10);
printf("%d\n",--i);
}
int
_l_abc(int i)
{
return(i++);
}
Answer:
9
Explanation:
return(i++)
it will first return i and then increments. i.e. 10 will be returned.