int f(int a) { return ++a; }int f(unsigned int a) { return --a; }cout << f(5)

Discussion in 'Programmer's Corner' started by summeranson, Sep 24, 2009.

  1. summeranson

    Thread Starter Member

    Feb 12, 2009
    28
    0
    2. What will be the result of:

    int f(int a) { return ++a; }
    int f(unsigned int a) { return --a; }
    cout << f(5);


    A. Undefined behaviour

    B. I don't know

    C. Compiler error

    D. 6

    E. 5

    F. 4
     
  2. summeranson

    Thread Starter Member

    Feb 12, 2009
    28
    0
    any body know what is the answer? i think it is D right?
     
  3. X-Istence

    New Member

    Sep 24, 2009
    1
    0
    D is indeed the correct answer.

    The web page located at http://cpp.comsci.us/etymology/literals.html describes the various literals that are allowed in C/C++ to describe the various data types. A number on its own is considered an int, and as such the first function list will be executed as it is the closest match for the variable type.

    If the author had put 5U it would have become an unsigned integer and the program would have output an unsigned int 4.
     
  4. summeranson

    Thread Starter Member

    Feb 12, 2009
    28
    0
    thanks X-Istence
     
  5. delmasli

    New Member

    Sep 17, 2009
    17
    0
    sure
    the answer is 6
     
Loading...