overide?

G

gouqizi.lvcha

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
 
P

Phil Staite

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

Sandeep

Consider what happened when the compiler compiled the file (ie.
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
 
I

Ian

Sandeep said:
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
 
S

Sandeep

Could be because printf should be defined as taking a const char*, not a char*.
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 ?
 
J

Jack Klein

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

Greg

Sandeep said:
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
 
I

Ian

Sandeep said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top