'outchar'的冲突类型

时间:2013-12-31 23:42:28

标签: c

我是C编程的新手。我正在通过阅读章节并从赫伯特·希尔特(Herbert Schildt)的“自学C”一书中做例子来学习。我正在尝试在Dev C中运行这个程序:

#include <stdio.h>
#include <stdlib.h>

main()
{
    outchar('A');
    outchar('B');
    outchar('C');
}

outchar(char ch)
{
    printf("%c", ch);
}

但编译时出现此错误:

20  1   C:\Dev-Cpp\main.c   [Error] conflicting types for 'outchar'

21  1   C:\Dev-Cpp\main.c   [Note] an argument type that has a default 
promotion can't match an empty parameter name list declaration

15  2   C:\Dev-Cpp\main.c   [Note] previous implicit declaration of 'outchar' was here

请帮助我!

2 个答案:

答案 0 :(得分:2)

这是因为您在使用之前没有声明 outchar。这意味着编译器将假定它是一个函数,返回int并获取未定义数量的未定义参数。

在使用之前,您需要在函数中添加原型

void outchar(char);  /* Prototype (declaration) of a function to be called */

int main(void)
{
    ...
}

void outchar(char ch)
{
    ...
}

请注意main函数的声明也与您的代码不同。它实际上是官方C规范的一部分,必须返回int必须获取void参数或{{1}和int参数。

答案 1 :(得分:0)

在C中,您定义事物的顺序通常很重要。将outchar的定义移到顶部,或者在顶部提供原型,如下所示:

#include <stdio.h>
#include <stdlib.h>

void outchar(char ch);

int main()
{
    outchar('A');
    outchar('B');
    outchar('C');
    return 0;
}

void outchar(char ch)
{
    printf("%c", ch);
}

此外,您应该指定每个函数的返回类型。我为你补充说。

相关问题