是否支持C超载? [英] Does C support overloading?
问题描述
我只是想知道,如果C支持过载?
由于我们使用的系统功能,如不同的无参数的printf。
帮我出
I just want to know if C supports over loading? As we use system functions like printf with different no of arguments. Help me out
推荐答案
没有,C不支持任何形式的重载(除非你算的事实,内置运营商的重载的已,成为一种形式的重载的)。
No, C doesn't support any form of overloading (unless you count the fact that the built-in operators are overloaded already, to be a form of overloading).
的printf
作品使用一种称为可变参数的功能。你让一个呼叫看起来的像它可能被重载:
printf
works using a feature called varargs. You make a call that looks like it might be overloaded:
printf("%d", 12); // int overload?
printf("%s", "hi"); // char* overload?
其实事实并非如此。世界上只有一个printf函数,但是编译器使用一种特殊的调用约定调用它,在那里你提供力所能及的参数放在顺序栈[*]上。的printf(或vprintf)检查格式字符串,并使用该工作如何阅读这些论点回来。这就是为什么printf的是不是类型安全的:
Actually it isn't. There is only one printf function, but the compiler uses a special calling convention to call it, where whatever arguments you provide are put in sequence on the stack[*]. printf (or vprintf) examines the format string and uses that to work out how to read those arguments back. This is why printf isn't type-safe:
char *format = "%d";
printf(format, "hi"); // undefined behaviour, no diagnostic required.
[*]标准实际上并不说的他们是在栈中传递,或提栈可言,但是这是自然的实现。
[*] the standard doesn't actually say they're passed on the stack, or mention a stack at all, but that's the natural implementation.
这篇关于是否支持C超载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!