且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用C冲突的类型,为什么呢?

更新时间:2023-11-05 17:56:04

您遇到的问题,因为你已经不是提供了一个原型printhex()然后再使用它

You run into problems because you've not provided a prototype for printhex() before you use it.

要么把 printhex()物理之前 print2()在源文件中,或添加一个声明:

Either put printhex() physically before print2() in the source file, or add a declaration:

extern void printhex(unsigned int u);

print2()定义。 (不要声明 printhex() print2();即使它在语法上是合法的,这是不好的做法这样做。)

before print2() is defined. (Don't declare printhex() inside print2(); even though it is syntactically legal, it is bad practice to do so.)

当编译器在整个呼叫 printhex(临时),它假设(下C89规则),它是一个返回 int类型的函数运行与一个不确定的参数列表(但不是其中之一是一个正式的变量参数列表 - 可变参数的功能,如的printf()必须始终有范围的原型)。当您随后将其定义为返回无效,它就会被打乱,并报告冲突的类型错误。根据C99的规则,你应该使用一个功能之前,有范围的原型。

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). When you subsequently define it as returning void, it gets upset and reports the conflicting type error. Under C99 rules, you are supposed to have a prototype in scope before using a function.

我想在你的布局进行评论;这是一个有点非正统的。

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)

是:

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

如果我在写他们。我写的职能,在定义明确的(无效)所以它的函数原型符号相匹配。其实,如果我写他们,他们倒是更可能是 prefixed与静态。如果我写的功能是不会在源文件是在外部使用的话,我让它自动静态的。此外,如果使用前的功能被定义为静态的,那么我不需要的功能的第二个声明。如果一个函数不是静态的,应该有,在我的书,是声明函数的标题,而标题应该在这两个定义函数的文件,并在使用该函数的文件中使用。这将确保一致性。

if I were writing them. I write functions with the explicit (void) in the definition so it matches the prototype notation for the function. Actually, if I was writing them, they'd more likely be prefixed with 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.

此外,您使用 ABS(临时)这是有点奇怪;它是 unsigned int类型转换为一个带符号的 INT 的长篇大论方式,但你会做的更好写:

Also, you use abs(temp) which is slightly odd; it is a long-winded way of converting the unsigned int to a signed int, but you'd do better to write:

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

最后,我建议你使用的更多的在循环空白:

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

逗号或分号之前没有空间;围绕二元运算符空间(而不是一元运算符,或者非常紧密的结合运营商,如标或成员( - 方式&gt; )运算符)。

您输出十六进制交错带小数点;你可能会发现它更好地使用:

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.