Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overide?

Reply
Thread Tools

overide?

 
 
gouqizi.lvcha@gmail.com
Guest
Posts: n/a
 
      10-12-2005
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

 
Reply With Quote
 
 
 
 
Phil Staite
Guest
Posts: n/a
 
      10-12-2005
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.
 
Reply With Quote
 
 
 
 
Sandeep
Guest
Posts: n/a
 
      10-12-2005
>>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

 
Reply With Quote
 
gouqizi.lvcha@gmail.com
Guest
Posts: n/a
 
      10-12-2005
Hi, Sandeep

I use MS VC 7.1.

Rick

 
Reply With Quote
 
Ian
Guest
Posts: n/a
 
      10-12-2005
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
 
Reply With Quote
 
Sandeep
Guest
Posts: n/a
 
      10-12-2005
>>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 ?

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      10-12-2005
On 11 Oct 2005 19:30:45 -0700, "(E-Mail Removed)"
<(E-Mail Removed)> 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/~a...FAQ-acllc.html
 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      10-12-2005
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

 
Reply With Quote
 
Ian
Guest
Posts: n/a
 
      10-12-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments