Results 1 to 13 of 13

Thread: Reason for the following programm's output!

  1. #1
    Junior Member
    Join Date
    Apr 2008
    Answers
    2

    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?


  2. #2
    Expert Member
    Join Date
    May 2008
    Answers
    100

    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);


  3. #3
    Junior Member
    Join Date
    May 2008
    Answers
    1

    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.


  4. #4
    Junior Member
    Join Date
    Jun 2008
    Answers
    2

    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.


  5. #5
    Junior Member
    Join Date
    Jul 2008
    Answers
    1

    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.


  6. #6
    Junior Member
    Join Date
    May 2008
    Answers
    1

    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.


  7. #7
    Junior Member
    Join Date
    Jul 2007
    Answers
    18

    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


  8. #8
    Junior Member
    Join Date
    Aug 2007
    Answers
    1

    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()


  9. #9
    Junior Member
    Join Date
    Oct 2007
    Answers
    4

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


  10. #10

    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.


  11. #11
    Junior Member
    Join Date
    Jul 2008
    Answers
    1

    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


  12. #12
    Junior Member
    Join Date
    Jul 2008
    Answers
    5

    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.


  13. #13
    Junior Member
    Join Date
    Aug 2008
    Answers
    1

    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.


  14.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact