Prepare for your Next Interview
This is a discussion on Reason for the following programm's output! within the C and C++ forums, part of the Software Development category; #include #include void main() { int a=10,b=20; printf("%d %d"); getch(); } the output is 20 10. How it is possible?...
|
|||
|
Reason for the following programm's output!
#include #include void main() { int a=10,b=20; printf("%d %d"); getch(); } the output is 20 10. How it is possible?
|
| The Following User Says Thank You to mkannanmca86 For This Useful Post: | ||
| Sponsored Links |
|
|||
|
Re: Reason for the following programm's output!
if u r written exactly the code u r showing then its not known what value is printed cuz its just compiler dependent that when it does not find the value to be printed for a format specifier, then what shud it do?
and otherwise if u have written printf("%d%d",b,a); then the result is right. |
|
|||
|
Re: Reason for the following programm's output!
for the above code "printf("%d %d");" you will get the output as 0 0 and not 20 10.
I have validated the same on VS2005. If you are getting outpur as 20 10 then you must have used "printf("%d %d", b , a);" for sure. DFo validate your code please. |
|
|||
|
Re: Reason for the following programm's output!
If u do not provide any arguments for the format specifier in the printf statement, then it outputs any garbage value depending on the data type 0f the specifier.
|
|
|||
|
Re: Reason for the following programm's output!
hi the program is tacking a first loaded garbage values thats why it is printed to out put.
|
|
|||
|
Re: Reason for the following programm's output!
In turbo C++ version 4.5 , in which we can run both C++ and C programs shows the same warnings and does not execute either as C program or C++ program
the warnings are same in both languages a is assigned a value that is never used in function main() b is assigned a value that is never used in function main() |
|
|||
|
Re: Reason for the following programm's output!
ya i also got that output only but it is not dependable one if u want a specified output use printf with corresponding arguments only you may get the correct output without using the arguments but it is not dependable. i think the reason is: printf prints garbage value present in random memory space if u did not specify arguments then while selecting the random memory address to print a value, it selects the last accessed memory address which happens to be the memory address of A and B (those memory only got created when we declare that).
|
|
|||
|
Re: Reason for the following programm's output!
when the valuesare being assigned to the variables a and b. they get stored in the stack segment as u know popping take place in LIFO manner in printf statement is b is printed first and then a.
i hope the answer is correct. |
|
|||
|
Re: Reason for the following programm's output!
The reason is that the value get store in stack, as u have not given the name of variable while using printf. the values just get pop and the last value print at first and the second one at the last.
But in the some complier it print the garbage value. |
|
|||
|
Re: Reason for the following programm's output!
Actually if you don't use any variable in printf,then the values of a and b which are given earlier are stored in a stack,and on printf statement they are retreived in the LIFO manner.Thats why they are displayed in the opposite order.
![]() |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| First output value is getting overwritten by second output | Geek_Guest | QTP | 2 | 01-05-2008 03:40 AM |
| I have XP and IE7, For some reason web testing won't work in QTP | Geek_Guest | QTP | 2 | 06-13-2007 02:45 PM |
| Forms are appearing BLACK, What is the reason | Geek_Guest | Oracle Apps | 0 | 04-03-2007 11:17 AM |
| Kindly provide the Reason | joel | ASP.NET | 2 | 09-16-2006 03:49 PM |
| Technical Reason for Usage in Coldfusion | RyanJames | ColdFusion | 0 | 07-12-2006 04:02 PM |