Geeks Talk

Prepare for your Next Interview




Reason for the following programm's output!

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?...


Go Back   Geeks Talk > Software Development > C and C++

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 05-17-2008
Junior Member
 
Join Date: Apr 2008
Location: India
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
mkannanmca86 is on a distinguished road
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?
Reply With Quote
The Following User Says Thank You to mkannanmca86 For This Useful Post:
Sponsored Links
  #2 (permalink)  
Old 05-21-2008
Junior Member
 
Join Date: May 2008
Location: in india
Posts: 26
Thanks: 10
Thanked 2 Times in 2 Posts
aman15490 is on a distinguished road
Thumbs up Re: Reason for the following programm's output!

you must write it as
printf("%d %d",a,b);
or it may also happen that you have written
printf("%d %d",b,a);
Reply With Quote
  #3 (permalink)  
Old 05-30-2008
Junior Member
 
Join Date: May 2008
Location: delhi
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Manisha Goel is on a distinguished road
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.
Reply With Quote
  #4 (permalink)  
Old 06-17-2008
Junior Member
 
Join Date: Jun 2008
Location: mumbai
Posts: 28
Thanks: 26
Thanked 0 Times in 0 Posts
neerajc07 is on a distinguished road
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.
Reply With Quote
  #5 (permalink)  
Old 07-01-2008
Junior Member
 
Join Date: Jul 2008
Location: Bangalore
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Kuldeep8085 is on a distinguished road
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.
Reply With Quote
  #6 (permalink)  
Old 07-01-2008
Junior Member
 
Join Date: May 2008
Location: hydarabad
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ESWAR.POTHULA is on a distinguished road
Re: Reason for the following programm's output!

Quote:
Originally Posted by mkannanmca86 View Post
#include #include void main() { int a=10,b=20; printf("%d %d"); getch(); } the output is 20 10. How it is possible?
hi the program is tacking a first loaded garbage values thats why it is printed to out put.
Reply With Quote
  #7 (permalink)  
Old 07-01-2008
Junior Member
 
Join Date: Jul 2007
Location: Banglore
Posts: 24
Thanks: 6
Thanked 4 Times in 3 Posts
sowji_mca is on a distinguished road
Re: Reason for the following programm's output!

mainly it depends on the compiler and if we not pass any arguments it give garbage values
Reply With Quote
  #8 (permalink)  
Old 07-04-2008
Junior Member
 
Join Date: Aug 2007
Location: hyderabad
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
prasad_adss is on a distinguished road
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()
Reply With Quote
  #9 (permalink)  
Old 07-16-2008
Junior Member
 
Join Date: Oct 2007
Location: chennai
Posts: 5
Thanks: 1
Thanked 1 Time in 1 Post
r.praveenkumar is on a distinguished road
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).
Reply With Quote
  #10 (permalink)  
Old 07-17-2008
Junior Member
 
Join Date: Jul 2008
Location: bangalore
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
divyamehta.2007 is on a distinguished road
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.
Reply With Quote
  #11 (permalink)  
Old 07-23-2008
Junior Member
 
Join Date: Jul 2008
Location: inida
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
subaniin is on a distinguished road
Re: Reason for the following programm's output!

i want to know about power of two numbers progam without using math.h directory in the program
Reply With Quote
  #12 (permalink)  
Old 07-24-2008
Junior Member
 
Join Date: Jul 2008
Location: Bangalore
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
rohit_tayal06 is on a distinguished road
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.
Reply With Quote
  #13 (permalink)  
Old 08-07-2008
Junior Member
 
Join Date: Aug 2008
Location: Durgapur
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
sumita_sarkar is on a distinguished road
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.
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++


Thread Tools
Display Modes


Similar Threads

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


All times are GMT -4. The time now is 12:04 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved