473,395 Members | 1,652 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,395 software developers and data experts.

custom printf() function

Hi Everyone,

I have the following custom printf() function in the my program unit
and when i execute the program, the actual printf() function is invoked
and i get no error, no warning. However when i rename printf to printf1
in the following program unit, it invokes my custom function... what
makes this happen?

int printf(char* p,int a)
{
return(0);
}

int main()
{
struct sample
{
int a;
}a,*b;
b = &a;
b->a = 5;
++(*b).a;
printf("%d\n",b->a);
}

Jan 13 '07 #1
19 7519
In article <11**********************@38g2000cwa.googlegroups. com>,
<sa*****@yahoo.co.inwrote:
>Hi Everyone,

I have the following custom printf() function in the my program unit
and when i execute the program, the actual printf() function is invoked
and i get no error, no warning. However when i rename printf to printf1
in the following program unit, it invokes my custom function... what
makes this happen?

int printf(char* p,int a)
{
puts("Here we are"); /* Add this line */
> return(0);
}

int main()
{
struct sample
{
int a;
}a,*b;
b = &a;
b->a = 5;
++(*b).a;
printf("%d\n",b->a);
}
The droids will tell you that you can't invade the implementation name
space, and that, alas, printf is in that name space.

When I took your program and added the line shown above, gcc compiled it
and ran it OK (but, quite correctly, generated the following warning:

x.c:2: warning: conflicting types for built-in function `printf'

But it will work correctly on most implementations anyway.

Jan 13 '07 #2
When I took your program and added the line shown above, gcc compiled it
and ran it OK (but, quite correctly, generated the following warning:

x.c:2: warning: conflicting types for built-in function `printf'

But it will work correctly on most implementations anyway.
What exactly do you mean by correctly? are you saying it would invoke
the actual printf() function or the the customized printf() function?

Jan 13 '07 #3
sam_...@yahoo.co.in wrote:
When I took your program and added the line shown above, gcc compiled it
and ran it OK (but, quite correctly, generated the following warning:

x.c:2: warning: conflicting types for built-in function `printf'

But it will work correctly on most implementations anyway.

What exactly do you mean by correctly? are you saying it would invoke
the actual printf() function or the the customized printf() function?
I think it typically depends on the compiler and mode it is compiled
in. For example, some compilers generate "in-line" calls for built-in,
and standard library functions, depending on the flags it is given.
Because you can always compile modules seperately, there is no way for
the compiler to know that you have overridden a built-in function until
after it has emitted its code. Most compilers will nevertheless take
your overridden definition so long as you make sure the flags its given
tell it not to generate inline code for intrinsics.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jan 13 '07 #4
sa*****@yahoo.co.in said:
Hi Everyone,

I have the following custom printf() function in the my program unit
and when i execute the program, the actual printf() function is invoked
and i get no error, no warning. However when i rename printf to printf1
in the following program unit, it invokes my custom function... what
makes this happen?
Undefined behaviour makes this (or something else, or nothing) happen.

If you stick to the rules, so does C. If you break the rules, C is free to
break them too. You don't want that to happen.

Key learning point: Don't Break The Rules.

One of the rules is: don't tread on the implementation.

Naming a function 'printf' constitutes treading on the implementation.

So Don't Do That.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 13 '07 #5
sa*****@yahoo.co.in writes:
>When I took your program and added the line shown above, gcc compiled it
and ran it OK (but, quite correctly, generated the following warning:

x.c:2: warning: conflicting types for built-in function `printf'

But it will work correctly on most implementations anyway.

What exactly do you mean by correctly? are you saying it would invoke
the actual printf() function or the the customized printf() function?
Neither behavior is either correct or incorrect.

The standard says:

All identifiers with external linkage in any of the following
subclauses (including the future library directions) are always
reserved for use as identifiers with external linkage.

The phrase "the following subclauses" refers to the descriptions of
the standard headers, including <stdio.hwhich declares printf().

You *could* declare your printf as static.

Or, better yet, don't try to use the name "printf" for anything other
than the standard function of that name, unless your goal is to
confuse anyone reading your code.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 13 '07 #6
In article <ln************@nuthaus.mib.org>,
the totally psychotic Keith Thompson <ks***@mib.orgwrote:
>sa*****@yahoo.co.in writes:
>>When I took your program and added the line shown above, gcc compiled it
and ran it OK (but, quite correctly, generated the following warning:

x.c:2: warning: conflicting types for built-in function `printf'

But it will work correctly on most implementations anyway.

What exactly do you mean by correctly? are you saying it would invoke
the actual printf() function or the the customized printf() function?

Neither behavior is either correct or incorrect.
I think I already granted (in the part you snipped, as they say) that
the droids (that's you, in case you're wondering) would say that it is
undefined, blah, blah, blah.

Jan 13 '07 #7
On Jan 14, 12:30 am, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
x.c:2: warning: conflicting types for built-in function `printf'
or perhaps more simply, use a #define:

int custom_printf(char *in, ...) {XXX}
#define printf custom_printf

This way, there will be no 'conflicting types', and there will never be
any doubt as to which function is called, and it will look exactly like
the 'normal' printf...
You can then choose to enable it or not without having to change your
code, etc.

F.

Jan 14 '07 #8
Franchie wrote:
On Jan 14, 12:30 am, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
x.c:2: warning: conflicting types for built-in function `printf'

or perhaps more simply, use a #define:

int custom_printf(char *in, ...) {XXX}
#define printf custom_printf

This way, there will be no 'conflicting types', and there will never be
any doubt as to which function is called, and it will look exactly like
the 'normal' printf...
You can then choose to enable it or not without having to change your
code, etc.

F.
Your attribution is incorrect. "Kenny McCormack" did not write what
you've attributed to him above.

Jan 14 '07 #9
On Jan 14, 7:01 pm, "santosh" <santosh....@gmail.comwrote:
Franchie wrote:
On Jan 14, 12:30 am, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
x.c:2: warning: conflicting types for built-in function `printf'
<...>
Your attribution is incorrect. "Kenny McCormack" did not write what
you've attributed to him above.
First came up here:
http://groups-beta.google.com/group/...9953d8ed2e2827
But you're right, I forgot to change the post date, and put in
excessive indentation, so all my apologies.

Disclaimer: I did not intend this quote as a statement. Simply, it was
the result of the compilation of using duplicate printfs, and seemed an
appropriate starting-point for the post. No comment intended, I hope no
offense taken.

F.

Jan 14 '07 #10
In article <11**********************@v45g2000cwv.googlegroups .com>,
Franchie <Fr******************@gmail.comwrote:
>On Jan 14, 12:30 am, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
> x.c:2: warning: conflicting types for built-in function `printf'

or perhaps more simply, use a #define:

int custom_printf(char *in, ...) {XXX}
#define printf custom_printf

This way, there will be no 'conflicting types', and there will never be
any doubt as to which function is called, and it will look exactly like
the 'normal' printf...
You can then choose to enable it or not without having to change your
code, etc.

F.
Yes, but that's not the point. The point is to seek knowledge,
something that is actively discouraged by the droids of this group.

Jan 14 '07 #11
In article <11*********************@51g2000cwl.googlegroups.c om>,
Franchie <Fr******************@gmail.comwrote:
....
>Disclaimer: I did not intend this quote as a statement. Simply, it was
the result of the compilation of using duplicate printfs, and seemed an
appropriate starting-point for the post. No comment intended, I hope no
offense taken.
Don't worry about it. Anyone with two brain cells to run together could
figure it out.

Admittedly, that lets out most of the droids of this ng, but, really,
when you get down to it, do you care about those fuckwits?

Jan 14 '07 #12
On Jan 14, 8:38 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
In article <1168781613.853281.311...@v45g2000cwv.googlegroups .com>,
Yes, but that's not the point. The point is to seek knowledge,
something that is actively discouraged by the droids of this group.
Lol, for the sake of longevity, I will attempt to stay neutral on the
subject of droids ;-)

But back to the knowledge part... As I understand, the problem is due
to the compiler linking the default c library, etc...
Is there a standard way of explicitly preventing that? I seem to recall
that the oskit project recoded an entire subset (ie: some bits from :-)
the c library so that it had no external dependencies, and linked that
instead of the default link library... They did that with a gcc
extension if I recall correctly, but is this standard practice?

Jan 14 '07 #13
Franchie wrote:
On Jan 14, 8:38 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
In article <1168781613.853281.311...@v45g2000cwv.googlegroups .com>,
Yes, but that's not the point. The point is to seek knowledge,
something that is actively discouraged by the droids of this group.

Lol, for the sake of longevity, I will attempt to stay neutral on the
subject of droids ;-)

But back to the knowledge part... As I understand, the problem is due
to the compiler linking the default c library, etc...
Is there a standard way of explicitly preventing that?
Not unless you take control of the linking step.
I seem to recall
that the oskit project recoded an entire subset (ie: some bits from :-)
the c library so that it had no external dependencies, and linked that
instead of the default link library... They did that with a gcc
extension if I recall correctly, but is this standard practice?
Yes. IIRC the --nodefaultlibs, --nostdlib and --nostartfiles switches
do this. But these are all non-standard and vary with implementations.

Jan 14 '07 #14
On Sun, 14 Jan 2007 13:50:53 -0600, Franchie wrote
(in article <11**********************@s34g2000cwa.googlegroups .com>):
On Jan 14, 8:38 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
>In article <1168781613.853281.311...@v45g2000cwv.googlegroups .com>,
Yes, but that's not the point. The point is to seek knowledge,
something that is actively discouraged by the droids of this group.

Lol, for the sake of longevity, I will attempt to stay neutral on the
subject of droids ;-)
A wise policy.
But back to the knowledge part... As I understand, the problem is due
to the compiler linking the default c library, etc...
It's not really a "problem", it's expected behavior.
Is there a standard way of explicitly preventing that?
A better question might be, is it normal for people to try and provide
different behavior for a function name that is part of standard C, when
they can easily implement anything they want by using a different name,
such as perhaps "xprintf()", and then do whatever they like, without
having to jump through hoops?
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Jan 14 '07 #15
Kenny McCormack wrote:
<sa*****@yahoo.co.inwrote:
I have the following custom printf() function in the my program unit
and when i execute the program, the actual printf() function is invoked
and i get no error, no warning. rintf is in that name space.

When I took your program and added the line shown above, gcc compiled it
and ran it OK

But it will work correctly on most implementations anyway.
Obviously it doesn't work "correctly" on the OP's implementation ,
so that wasn't a very useful answer on your part.

Jan 14 '07 #16
Richard Heathfield wrote:
Naming a function 'printf' constitutes treading on the implementation.
Not if the function has internal linkage, and stdio.h is not included

Jan 15 '07 #17
Old Wolf wrote:
>
Richard Heathfield wrote:
Naming a function 'printf'
constitutes treading on the implementation.

Not if the function has internal linkage, and stdio.h is not included
printf has external linkage
and it doesn't matter whether or not stdio.h has been included.
The standard says "always reserved".
N869
7.1.3 Reserved identifiers

-- All identifiers with external linkage in any of the
following subclauses (including the future library
directions) are always reserved for use as identifiers
with external linkage.

--
pete
Jan 24 '07 #18
pete wrote:
Old Wolf wrote:

Richard Heathfield wrote:
Naming a function 'printf'
constitutes treading on the implementation.
Not if the function has internal linkage, and stdio.h is not included

printf has external linkage
The standard library function printf has external linkage.
and it doesn't matter whether or not stdio.h has been included.
The standard says "always reserved".
N869
7.1.3 Reserved identifiers

-- All identifiers with external linkage in any of the
following subclauses (including the future library
directions) are always reserved for use as identifiers
with external linkage.
"always reserved for use as identifiers with external linkage", not
"always reserved for any use"

This is a strictly conforming program:

/* no stdio.h */

static int printf(void)
{
return 0;
}

int main(void)
{
return printf();
}

Jan 24 '07 #19
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= wrote:
>
pete wrote:
Old Wolf wrote:
>
Richard Heathfield wrote:
Naming a function 'printf'
constitutes treading on the implementation.
>
Not if the function has internal linkage, and stdio.h is not included
printf has external linkage

The standard library function printf has external linkage.
and it doesn't matter whether or not stdio.h has been included.
The standard says "always reserved".
N869
7.1.3 Reserved identifiers

-- All identifiers with external linkage in any of the
following subclauses (including the future library
directions) are always reserved for use as identifiers
with external linkage.

"always reserved for use as identifiers with external linkage", not
"always reserved for any use"

This is a strictly conforming program:

/* no stdio.h */

static int printf(void)
{
return 0;
}

int main(void)
{
return printf();
}
Thanks.

--
pete
Jan 24 '07 #20

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

Similar topics

12
by: Matt Garman | last post by:
I'd like to create a "custom output facility". In other words, I want an object whose use is similar to std::cout/std::cerr, but offers more flexibility. Instead of simply writing the parameter...
12
by: Franz | last post by:
Greetings, I have created the following types: typedef struct _tbl_certificate{ int cert_id; char *cert_name; }tbl_certificate; typedef struct _cameraObj{ tbl_camera camera;
8
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
18
by: sam_cit | last post by:
Hi Everyone, int main() { printf("not included stdio.h"); } Yes, i haven't included stdio.h and my compiler would generate a warning and would assume that it would return a int, my question...
34
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
2
revolter00
by: revolter00 | last post by:
hi all, this is a piece of code from the program that i`m currently working on.. . . #define SIZE 10 . . . int opt, NumARR, L, MaxINT, sum=0, input_int;
1
by: Alexander1234 | last post by:
Hi, I've searched many groups and discussions, but nothing I found suited my needs. My problem is rather simple: I want to use a STL map in a class with a custom constructor but don't know how to...
30
by: Tarique | last post by:
I have tried to write my custom scanf function on the lines of minprintf provided in K&R2.In the function myscanf() i access memory directly using the address passed to the function .Can it be...
43
by: Jrdman | last post by:
someone has an idea on how the printf function is programmed ?
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.