函数原型声明 [英] function prototype declarations

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

问题描述

我实践的作用在C和跨来到节目....

 #包括LT&;&stdio.h中GT;诠释的main()
{
    浮动= 15.5;
    焦炭CH ='C';
    为printit(一,CH);
    返回0;
}为printit(A,CH)
{
    的printf(%F \\ N%C一个,CH);
}

我想知道,为什么上面的程序编译并没有给出错误,因为我了解到目前为止...


  1. 在C函数必须与特定的原型声明(但此方案不包含原型)


  2. 为什么程序给x'for的char变量输出'?


  3. 可以在C函数能够接受的价值没有被宣布有关类型像什么已在函数声明中所做的参数?



解决方案

首先,在C语言中没有要求一个函数提供一台样机它被调用之前。在语言版本的C99有一个要求的声明的功能之前,它叫,但依然没有要求提供一台样机。

由于编译器并没有抱怨,你必须使用C89 / 90的编译器,而不是一个C99编译。

其次,在C89 / 90,当你调用类型的未申报功能诡计传递参数浮动字符当你在做

 为printit(A,CH);

编译器将执行默认参数促销活动,实际上传递类型的值双击 INT 。你的函数必须相应地为code的工作定义。你定义你的函数为

 为printit(A,CH)
{
   ...

这定义意味着这两个参数的类型 INT 。这违反了上述要求。您code的行为是不确定的。它不再让任何意义,任何进一步的分析code或猜测为什么它打印东西它打印的方式。您code的行为,再一次,不确定的。

你(未申报)函数可能看起来正确的定义如下

  INT为printit(双一,INT CH)
{
   ...

替代地,它可以在K&放大器定义; R样式

 为printit(A,CH)
浮动;
{
   ...

这很可能会令你的code才能正常工作。然而,更好的方法是调用前,为为printit 的原型。它的原型要使用 - 无效为printit(双一,诠释CH)​​无效为printit(浮动,焦炭CH)或别的东西 - 是你来决定。

I am practice the function in c and come across to the program ....

#include<stdio.h>

int main()
{
    float a=15.5;
    char ch ='C';
    printit(a,ch);
    return 0;
}

printit(a,ch)
{
    printf("%f\n%c",a,ch);
}

I want to know that why the above program compile and not give the error as I understood so far is ...

  1. The function in c must be declared with the specific prototype (but this program does not contain the prototype)

  2. why the program give the output 'x'for the char variable ?

  3. can the function in c are capable of accepting the value without being declared about type in parameters like what has done in the function declaration ?

解决方案

Firstly, there's no requirement in C language to provide a prototype for a function before it is called. In C99 version of the language there's a requirement to declare a function before it is called, but still there's no requirement to provide a prototype.

Since your compiler did not complain, you must be using a C89/90 compiler, not a C99 compiler.

Secondly, in C89/90, when you call an undeclared function wile passing arguments of type float and char as you do in

printit(a,ch);

the compiler will perform default argument promotions and actually pass values of type double and int. Your function must be defined accordingly for the code to work. You defined your function as

printit(a, ch)  
{
   ...

That definition means that both parameters have type int. This violates the above requirement. The behavior of your code is undefined. It no longer makes any sense to analyze the code any further or guess why it prints something the way it prints it. The behavior of your code is, once again, undefined.

The proper definition for your (undeclared) function might look as follows

int printit(double a, int ch)  
{
   ...

Alternatively, it can be defined in K&R style as

printit(a, ch)  
float a;
{
   ...

That would probably make your code to work correctly. However, the much better approach would be to provide a prototype for printit before calling it. Which prototype you want to use - void printit(double a, int ch) or void printit(float a, char ch) or something else - is for you to decide.

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

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