旧风格的C函数的声明 [英] Old style C function declaration

查看:279
本文介绍了旧风格的C函数的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是delcared一个简单的功能,使用旧风格的语法定义的:

Here's a simple function delcared and defined using old style syntax:

#include <stdio.h>
void
error(message,a1,a2,a3,a4,a5,a6,a7)
        char *message;
        char *a1,*a2,*a3,*a4,*a5,*a6,*a7;
{
  fprintf(stderr,message,a1,a2,a3,a4,a5,a6,a7);
}
int main ()
{
  error("[ERROR %d]: %s.\n",110,"Connection timed out");
  return 0;
}

它可以编译和运行正常打印:

It can be compiled and runs correctly to print:

[错误110]:连接超时

[ERROR 110]: Connection timed out.

我看,这种风格没有关联的原型,但它如何转换INT为char *在运行时自动连提供的参数少于它的声明?

I read that this style doesn't have associated prototype, but how can it convert int to char * automatically at runtime and even the provided arguments are fewer than it's declared?

推荐答案

基本上,它的作品,因为它太愚蠢知道更好。老式的K&安培; R C基本不检查任何东西。你得逞,是因为,

Basically, it works because it's too dumb to know better. Old fashioned K&R C basically doesn't check anything. You get away with this because,


  1. 它发生的sizeof 你使用特定的架构和编译器的组合(INT)==的sizeof(字符*)。它不是真正的转换任何东西,它只是数字32位为32位。

  1. it happens that sizeof(int) == sizeof(char *) on the particular architecture and compiler combination you're using. It's not really converting anything, it just figures 32 bits is 32 bits.

当你把所有这些参数的堆栈上,它只是将他们推在printf的时候使用它们,它只是使用如需要的那些,不影响休息。然后他们消失在调用返回的时候,没有一个人的明智。但是,如果你碰巧尝试打印七个值,你只能通过六个参数,它会在运行时炸毁,有时在创造性和意想不到的方式。

When you put all those arguments on the stack, it just pushed them in. When printf uses them, it just uses the ones if needs and leaves the rest alone; they then disappear when the call returns, and no one's the wiser. However, should you ever happen to try printing seven values where you only passed six arguments, it'll blow up at run time, sometime in creative and unexpected ways.

这篇关于旧风格的C函数的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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