overide?

Discussion in 'C++' started by gouqizi.lvcha@gmail.com, Oct 12, 2005.

  1. Guest

    Hi, All:

    I defined another printf , same as printf function in libC.
    my question is why compiler choose my version of printf , not choose
    the printf in libC?

    Here is a piece of code:

    #include <stdio.h>

    int printf(char *str, ...)
    {
    return 1;
    }

    int main()
    {
    printf("hello world\n")
    }

    Rick
     
    , Oct 12, 2005
    #1
    1. Advertising

  2. Phil Staite Guest

    Consider what happened when the compiler compiled the file (ie.
    "compilation unit") with your code in it. It saw it needed a printf,
    and that it had a printf already.

    So when your build tool(s) got to the link phase, it did not need a
    printf and had no reason to "pull" one from the library.
     
    Phil Staite, Oct 12, 2005
    #2
    1. Advertising

  3. Sandeep Guest

    >>Consider what happened when the compiler compiled the file (ie.
    >>"compilation unit") with your code in it. It saw it needed a printf,
    >>and that it had a printf already.


    This is compiler dependent. I Tried compiling it with g++ and cl (MS
    Compiler). While "g++" gave the output as "hello world" and "cl" gave
    no output
     
    Sandeep, Oct 12, 2005
    #3
  4. Guest

    Hi, Sandeep

    I use MS VC 7.1.

    Rick
     
    , Oct 12, 2005
    #4
  5. Ian Guest

    Sandeep wrote:
    >>>Consider what happened when the compiler compiled the file (ie.
    >>>"compilation unit") with your code in it. It saw it needed a printf,
    >>>and that it had a printf already.

    >
    >
    > This is compiler dependent. I Tried compiling it with g++ and cl (MS
    > Compiler). While "g++" gave the output as "hello world" and "cl" gave
    > no output
    >

    Could be because printf should be defined as taking a const char*, not a
    char*.

    What happens if you change it? The output should be nothing.

    Ian
     
    Ian, Oct 12, 2005
    #5
  6. Sandeep Guest

    >>Could be because printf should be defined as taking a const char*, not a char*.
    >>What happens if you change it? The output should be nothing.


    Ian,
    You are right about this. When I changed it to const char* , the output
    was nothing. Does that mean g++ does a stricter type checking than MS
    VC7.1 compiler ?
     
    Sandeep, Oct 12, 2005
    #6
  7. Jack Klein Guest

    On 11 Oct 2005 19:30:45 -0700, ""
    <> wrote in comp.lang.c++:

    > Hi, All:
    >
    > I defined another printf , same as printf function in libC.
    > my question is why compiler choose my version of printf , not choose
    > the printf in libC?
    >
    > Here is a piece of code:
    >
    > #include <stdio.h>
    >
    > int printf(char *str, ...)
    > {
    > return 1;
    > }
    >
    > int main()
    > {
    > printf("hello world\n")
    > }
    >
    > Rick


    Defining a function with the same name and signature as a standard
    library function in the global or std namespace is undefined behavior.
    What your compiler does is whatever it wants to do, there is no right
    or wrong result according to the C++ standard.

    If you want to know why your compiler does what it does when you break
    the rules, ask in a support group for your compiler. It is not a
    language issue, C++ does not know or care what happens when you create
    undefined behavior.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Oct 12, 2005
    #7
  8. Greg Guest

    Sandeep wrote:
    > >>Consider what happened when the compiler compiled the file (ie.
    > >>"compilation unit") with your code in it. It saw it needed a printf,
    > >>and that it had a printf already.

    >
    > This is compiler dependent. I Tried compiling it with g++ and cl (MS
    > Compiler). While "g++" gave the output as "hello world" and "cl" gave
    > no output


    Clearly the outcome of compiling any C++ program will always be
    compiler-dependent. The question really is whether the observed
    compiler behavior conforms to the C++ standard.

    Since the sample program includes the header file <stdio.h>, it is
    reasonable to expect that a printf routine will be declared in the
    global namespace. Were the program to include <cstdio> instead, then
    printf would be declared in the std namespace only. Unfortunately gcc
    has never had very strong support for the std namespace. And although
    gcc does add printf and the other std:: names to the std namespace when
    <cstdio> is included, gcc also continues to declare those names in the
    global namespace. Doing so is a departure from the C++ standard
    (ยง17.4.1.2/4) which is clear on the point that the standard library
    names are declared in the std namespace instead of - and not in
    addition to - global namespace declarations.

    As a consequence, the std:: namespace qualifier is never really
    necessary in a program compiled with gcc as it often is when compiling
    with a more standards observant C++ compiler.

    Greg
     
    Greg, Oct 12, 2005
    #8
  9. Ian Guest

    Sandeep wrote:
    >>>Could be because printf should be defined as taking a const char*, not a char*.
    >>>What happens if you change it? The output should be nothing.

    >
    >
    > Ian,
    > You are right about this. When I changed it to const char* , the output
    > was nothing. Does that mean g++ does a stricter type checking than MS
    > VC7.1 compiler ?
    >

    As has been pointed out, this is straying into the world of undefined
    behaviour.

    My take is that with char* as the parameter, you are defining a new
    function called printf that is distinct from the standard one. When
    calling printf with a string literal (a const char*), the standard
    printf will be called.

    So with that interpretation, gcc was correct and VC wrong.

    If you compile this with a C compiler, it will barf when the parameter
    is char*, C doesn't support function overloading.

    Ian
     
    Ian, Oct 12, 2005
    #9
    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. Steve
    Replies:
    1
    Views:
    552
    Hywel
    Sep 24, 2004
  2. Nikos
    Replies:
    5
    Views:
    2,316
  3. David Morgenthaler

    How to overide "for line in file:"

    David Morgenthaler, Feb 22, 2004, in forum: Python
    Replies:
    11
    Views:
    555
    Duncan Booth
    Feb 23, 2004
  4. Eddie
    Replies:
    2
    Views:
    264
    Eddie
    Sep 16, 2004
  5. Raja

    Overide settrace

    Raja, Apr 25, 2007, in forum: Python
    Replies:
    0
    Views:
    261
Loading...

Share This Page