473,327 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

overide?

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 '05 #1
8 1639
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.
Oct 12 '05 #2
>>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

Oct 12 '05 #3
Hi, Sandeep

I use MS VC 7.1.

Rick

Oct 12 '05 #4
Ian
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
Oct 12 '05 #5
>>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 ?

Oct 12 '05 #6
On 11 Oct 2005 19:30:45 -0700, "go***********@gmail.com"
<go***********@gmail.com> 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
Oct 12 '05 #7
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

Oct 12 '05 #8
Ian
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
Oct 12 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
Hi. I am in the httpd file in the Apache folder and need to change the part that says AllowOverride from None to All, so that when I create a ..htaccess file it wont ignore it. However in the...
0
by: Jason Evans | last post by:
Hi All, I am writing my own implementation of queue via a linked list, note not a LinkedList, and was running into trouble with the clone method. I was wondering if anyone could point out some...
11
by: David Morgenthaler | last post by:
How does one overide the iterator implied by the construct "for line in file:"? For example, suppose I have a file containing row,col pairs on each line, and I wish to write a subclass of file...
2
by: Eddie | last post by:
Hi all, I am using ZSI SOAP library for a web service. My service runs fine, but it outputs a line localhost - - "POST / HTTP/1.1" 200 - every time the web service is accessed. I traced it...
51
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class,...
1
by: juli jul | last post by:
Hello,how can I override(save) file in c#? (I am working with XmlDocumnet) Thanks! *** Sent via Developersdex http://www.developersdex.com ***
0
by: Raja | last post by:
Hi, I want to override the sys.settrace() call, create a way to trace the execution of a python program. Keep track of all objects created and destroyed. Keep track of the call pattern throughout...
2
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I have a textBox on a windows form. Can i overide the textbox OnPaint directly in the form, if so how. Or do i have to inherit the textbox control and overide it there? Regards Martin
1
by: raknin | last post by:
Hi, I have a table for which I defined a css rules like: table#showBuddyHelpList a { text-decoration: none; border-bottom: 1px dotted #f60; color: #f60; font-weight: bold;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.