wht is the out put of the below C program which was execuited in CPP compiler under DOS enverionment
main()
{
printf("%s",printf("hello"));
}
|
wht is the out put of the below C program which was execuited in CPP compiler under DOS enverionment
main()
{
printf("%s",printf("hello"));
}
why don't you try it?
anyway, you know %s is a string and printf returns an integer, don't you?
duh
i think it will print hallo
coz the output for the second printf is word as first prinf excpict character(s) as second parameter
First we must know that the printf function, when the input is a char array,
prints each char one by one until it reaches the special char '\0' (end of string).
The following program will do the following:
First it will print the "hallo" of the inside printf(), and then it will try to print the
argument of the outside printf (=> the return value of the inside) which is an integer. So, it will start to print 4 bytes (an integer is 4 chars) but it will not stop, since there is no '\0' char in the integer. If you're lucky enough it will print some non human readble characters. But in the most cases (if the OS is safe and strict) will drop a Segmentation Fault (in Windows Access Violation) and terminate.
I am so sorry, I have to correct something I said.
As we know, if we want to access a string (char[]) we need to specify its starting address. As a result, in the printf(), if we want ot print a string (%s) we must give as an argument its starting address. So, in this case the argument of the oustide printf() is the return value of the inside printf(). It will return an integer. Then this integer we be an address number for the oustide printf(), but the address will be outside the limits of the process's memory space and so it will drop a Segmentation Fault. Even if it is inside the process's limits, the address may be not a multiple of 4 (perhaps it will be 1) and thus it will drop again Segmention Fault (for missaligned address).
« Stereograms | How to be anonymus and get to certain pages » |