简体   繁体   English

C中的冲突类型,为什么?

[英]Conflicting types in C, why?

I'm trying to write a basic practice with C, working with Binary and Hex. 我正在尝试用C语言编写基本练习,使用Binary和Hex。 I made a method to print the multiples of 2 (powers of 2) and a separate method to print out the Hex form of that multiple of 2. 我做了一个方法来打印2的倍数(2的幂)和一个单独的方法来打印出2的倍数的十六进制形式。

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

const char one = 1;
const int bits = 31;

void    print2       ()
{
    unsigned int  u = (int)one;
    unsigned int j;

    printf("The powers of 2 are:\n");

    for(j=0;j<bits;j++)
    {
            unsigned int temp = u<<j;
            printf("%d\n",abs(temp));
            printhex(temp);
    }

    printf("\n\n");
}

void    printhex       (unsigned int   u)
{
    printf("0x%08X\n",u);
}

int main ()
{
    print2();
    return(EXIT_SUCCESS);
}

What I don't understand is why I get an error "conflicting types" from calling on the method "printHex". 我不明白的是为什么我从调用方法“printHex”得到错误的“冲突类型”。 I specifically ask for an unsigned integer and when I call the method within the parameter (which I assume to be unsigned integer "temp"), the compiler does not accept. 我特别要求一个无符号整数,当我在参数中调用该方法时(我假设它是无符号整数“temp”),编译器不接受。

You run into problems because you've not provided a prototype for printhex() before you use it. 您遇到问题是因为在使用之前没有为printhex()提供原型。

Either put printhex() physically before print2() in the source file, or add a declaration: printhex()物理地放在源文件中的print2()之前,或者添加一个声明:

extern void printhex(unsigned int u);

before print2() is defined. 在定义print2()之前。 (Don't declare printhex() inside print2() ; even though it is syntactically legal, it is bad practice to do so.) (不要在print2()声明printhex() ;尽管它在语法上是合法的,但这样做是不好的做法。)


When the compiler runs across the call printhex(temp) , it assumes (under C89 rules) that it is a function that returns an int with an indeterminate argument list (but not one which is formally a variable argument list — varargs functions like printf() must always have a prototype in scope). 当编译器在调用printhex(temp) ,它假设(在C89规则下)它是一个返回带有不确定参数列表的int的函数(但不是一个正式变量参数列表的函数 - varargs函数类似于printf()必须始终在范围内有原型)。 When you subsequently define it as returning void , it gets upset and reports the conflicting type error. 当您随后将其定义为返回void ,它会被沮丧并报告冲突类型错误。 Under C99 rules, you are supposed to have a prototype in scope before using a function. 根据C99规则,在使用函数之前,您应该在范围内使用原型。

I'd like to comment on your layout; 我想评论你的布局; it's a little unorthodox. 这有点不正统。

The function definitions don't need as many blanks in them (use less white space in the function definitions): 函数定义中不需要那么多空格(在函数定义中使用较少的空格):

void    print2       ()
void    printhex       (unsigned int   u)

would be: 将会:

void print2(void)
void printhex(unsigned int u)

if I were writing them. 如果我正在写它们 I write functions with the explicit (void) in the definition so it matches the prototype notation for the function. 我在定义中使用explicit (void)编写函数,因此它匹配函数的原型符号。 Actually, if I was writing them, they'd more likely be prefixed with static . 实际上,如果我正在编写它们,它们更可能以static为前缀。 If the function I write is not going to be used outside the source file it is in, then I make it static automatically. 如果我写的函数不会在它所在的源文件之外使用,那么我会自动将其设置为静态。 Further, if the function is defined as static before it is used, then I don't need a second declaration of the function. 此外,如果函数在使用之前被定义为静态,那么我不需要函数的第二个声明。 If a function is not static, there should, in my book, be a header that declares the function, and that header should be used in both the file that defines the function and in the files that use the function. 如果函数不是静态的,那么在我的书中,应该是一个声明函数的头,并且该头应该在定义函数的文件和使用该函数的文件中使用。 This ensures consistency. 这确保了一致性。

Also, you use abs(temp) which is slightly odd; 另外,你使用略微奇怪的abs(temp) ; it is a long-winded way of converting the unsigned int to a signed int , but you'd do better to write: unsigned int转换为signed int是一种冗长的方式,但你最好写一下:

 printf("%u\n", temp);

Finally, I'd suggest you use more white space in the for loop: 最后,我建议你在for循环中使用更多的空格:

for (j = 0; j < bits; j++)

No space before commas or semicolons; 逗号或分号前没有空格; spaces around binary operators (but not unary operators, or very tightly binding operators such as subscripts or member ( . or -> ) operators). 二元运算符周围的空格(但不是一元运算符,或非常紧密绑定的运算符,如下标或成员( .-> )运算符)。

Your output interleaves hex with decimal; 您的输出将十六进制与十进制交错; you might find it better to use: 你可能会发现它更好用:

printf("%10u ", temp);

to put the decimal value right justified on the line, leaving the hex to appear after it on the same line. 将十进制值右对齐放在行上,使十六进制在同一行后面显示。

You're trying to use (call) printhex before you declare it. 您在声明之前尝试使用(调用) printhex Either forward declare it by saying void printhex (unsigned int u) or void printhex (unsigned int) or move the definition, to before print2 . 要么通过说出void printhex (unsigned int u)void printhex (unsigned int)向前声明它,要么将定义移到print2之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM