自定义printf()函数 [英] custom printf() function

查看:455
本文介绍了自定义printf()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我的程序单元中有以下自定义printf()函数

当我执行程序时,实际的printf ()函数被调用

我得到没有错误,没有警告。但是,当我在以下程序单元中将printf重命名为printf1

时,它会调用我的自定义函数...

是否会发生这种情况?


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 \ nn",b-> a);

}

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);
}

推荐答案

在文章< 11 ********************** @ 38g2000cwa。 Google网上论坛。 com>,

< sa ***** @ yahoo.co.inwrote:
In article <11**********************@38g2000cwa.googlegroups. com>,
<sa*****@yahoo.co.inwrote:

>大家好,

我的程序单元中有以下自定义printf()函数
当我执行程序时,实际的printf()函数被调用
我得不到错误,没有警告。但是,当我在以下程序单元中将printf重命名为printf1
时,它会调用我的自定义函数...
是什么让这种情况发生?


int printf(char * p,int a)
{
>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 here are quot;); / *添加此行* /

puts("Here we are"); /* Add this line */


>返回(0);
}
int main()
{
struct sample
{
int a;
} a,* b;

b =& a;
b-> a = 5;
++(* b).a;
printf(" ;%d \ n",b-> a);
}
> return(0);
}

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



机器人会告诉你不能入侵实施名字

空间,唉,printf在那个名字空间。


当我拿到你的程序并添加上面显示的行时,gcc编译了它

并且运行正常(但是,非常正确,生成了以下警告:


xc:2:警告:内置函数`printf的冲突类型''


但无论如何它都可以在大多数实现中正常工作。

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.


当我拿到你的程序并添加如上所示,gcc编译它
When I took your program and added the line shown above, gcc compiled it

并运行它OK(但是,非常正确,产生了以下警告:


xc:2:警告:confli内置函数`printf'的类型类型


但它无论如何都能在大多数实现中正常工作。
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.



你究竟是什么意思?你是说它会调用

实际的printf()函数或自定义的printf()函数吗?

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


sam_。 .. @ yahoo.co.in写道:
sam_...@yahoo.co.in wrote:

当我拿到你的程序并添加上面显示的行时,gcc编译它

并运行它OK(但是,非常正确,生成了以下警告:


xc:2:警告:内置函数`printf'的冲突类型'


但它无论如何都可以在大多数实现中正常工作。
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.



你究竟是什么意思正确?你在说什么它会调用

实际的printf()函数还是自定义的printf()函数?


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



我认为它通常取决于编译器和模式它被编译

in。例如,一些编译器生成内置的内联调用,

和标准库函数tions,取决于它给出的标志。

因为你总是可以单独编译模块,所以编译器无法知道你已经覆盖了一个内置模块。在函数发出代码后直到

。然而,大多数编译器会将你的覆盖定义带到

,只要你确定它给出的标志

告诉它不要为内在函数生成内联代码。


-

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

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/


这篇关于自定义printf()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆