Output pridiction

Discussion in 'C++' started by Anshum Kocher, Nov 11, 2011.

  1. #include<stdio.h>

    int &abc();
    int main()
    {

    int b=abc();


    getch();
    return 0;
    }
    int &abc()
    {
    int a=5;
    return a;
    }


    whats the output and why?
     
    Anshum Kocher, Nov 11, 2011
    #1
    1. Advertising

  2. On Nov 11, 7:44 am, Anshum Kocher <> wrote:
    > #include<stdio.h>
    >
    > int &abc();
    > int main()
    > {
    >
    >   int b=abc();
    >
    >    getch();
    >    return 0;}
    >
    > int &abc()
    > {
    >     int a=5;
    >     return a;
    >
    > }
    >
    > whats the output and why?



    This looks like homework...

    Did you try compiling it with any decent compiler? Take a look at
    what the warning messages tell you (leaving aside the fact that you
    didn't put any #include in that would have defined "getch()")


    ~/tmp % g++ -Wall x.cpp
    x.cpp: In function ‘int main()’:
    x.cpp:9: error: ‘getch’ was not declared in this scope
    x.cpp:7: warning: unused variable ‘b’
    x.cpp: In function ‘int& abc()’:
    x.cpp:15: warning: reference to local variable ‘a’ returned


    T:\tmp> cl.exe /EHsc x.cpp
    Microsoft (R) C/C++ Optimizing Compiler Version 15.00.30729.01 for x64
    Copyright (C) Microsoft Corporation. All rights reserved.

    x.cpp
    x.cpp(9) : error C3861: 'getch': identifier not found
    x.cpp(16) : warning C4172: returning address of local variable or
    temporary


    - Kevin B. McCarty
     
    Kevin McCarty, Nov 11, 2011
    #2
    1. Advertising

  3. Anshum Kocher

    red floyd Guest

    On 11/11/2011 7:44 AM, Anshum Kocher wrote:
    > #include<stdio.h>
    >
    > int&abc();
    > int main()
    > {
    >
    > int b=abc();
    >
    >
    > getch();
    > return 0;
    > }
    > int&abc()
    > {
    > int a=5;
    > return a;
    > }
    >
    >
    > whats the output and why?


    The answer is already available on line.

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.2
     
    red floyd, Nov 11, 2011
    #3
  4. Anshum Kocher

    yongjie Guest

    On Nov 11, 11:44 pm, Anshum Kocher <> wrote:
    > #include<stdio.h>
    >
    > int &abc();
    > int main()
    > {
    >
    >   int b=abc();
    >
    >    getch();
    >    return 0;}
    >
    > int &abc()
    > {
    >     int a=5;
    >     return a;
    >
    > }
    >
    > whats the output and why?


    u r returning a reference to a local value.
     
    yongjie, Nov 14, 2011
    #4
  5. On Nov 11, 3:44 pm, Anshum Kocher <> wrote:
    > #include<stdio.h>
    >
    > int &abc();
    > int main()
    > {
    >
    >   int b=abc();
    >
    >    getch();
    >    return 0;}
    >
    > int &abc()
    > {
    >     int a=5;
    >     return a;
    >
    > }
    >
    > whats the output and why?


    a suffusion of yellow, because one hand clapping sounds like that
     
    Nick Keighley, Nov 14, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Bentley

    Output / Debug window output bug?

    John Bentley, Sep 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    614
    John Bentley
    Sep 10, 2003
  2. chuck amadi
    Replies:
    1
    Views:
    509
    Larry Bates
    Jun 23, 2004
  3. Paul
    Replies:
    1
    Views:
    446
  4. jmr
    Replies:
    2
    Views:
    350
  5. pratap
    Replies:
    3
    Views:
    488
    James Kanze
    Jul 19, 2007
Loading...

Share This Page