473,805 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7560
In article <11************ **********@38g2 000cwa.googlegr oups.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.c o.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.c o.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.c o.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_Keit h) 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.or g>,
the totally psychotic Keith Thompson <ks***@mib.orgw rote:
>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...@xmissio n.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(c har *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...@xmissio n.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(c har *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
In article <11************ *********@51g20 00cwl.googlegro ups.com>,
Franchie <Fr************ ******@gmail.co mwrote:
....
>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 #10

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

Similar topics

12
3220
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 to stdout/stderr, I'd like it to write to stdout, to a file, and/or call a logging function. So my output function might look something like this: OutputFacility& OutputFacility::put(const std::string& s) { cout << s; // print to stdout
12
1718
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
2724
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
2863
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 is how does the linker manage to link the function invocation to the proper
34
16009
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
1699
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
2259
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 tell it to use my constructor instead of the default. The following code illustrates my problem: #include <stdio.h> #include <map> class Test {
30
3466
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 dangerous ? I am getting the correct output though.Any help is appreciated. /*Include Files*/ /*Assisting Functions*/ int flushln(FILE *f){ /*Code*/}
43
366
by: Jrdman | last post by:
someone has an idea on how the printf function is programmed ?
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10360
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10105
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.