c - 警告 : passing argument 3 of 'os' with different width due to prototype

标签 c gcc types compiler-warnings

声明:

/* Olson and Salop */
double os(double,double,short int);

/* Olson, Salop, and Taulberg */
double ost(double,double,short int,int,int);

代码片段:

/* Olson and Salop */

double os(double rx,double ita,short int charge)
{
    double a=0.0;
    double b=0.0;

    a=18.26/sqrt((double)charge);
    b=1.872*sqrt(ita/(double)charge);

    return a*exp(-b*rx);
}

/* Olson, Salop, and Taulberg */

double ost(double rx,double ita,short int charge,int PQN,int AQN)
{
    /* AQN - azimuthal quantum number */
    /* PQN - principle quantum number */
    return os(rx,ita,charge)*exp(0.5*(log(2.0*AQN+1.0)-lgamma((double)PQN-AQN)-lgamma((double)(PQN+AQN+1)))+lgamma((double)PQN)); /* line 64 */
}

编译器标志:

CC=gcc
WFLAGS=-W \
       -Wall \
       -Werror \
       -Wshadow \
       -Wcast-qual \
       -Wcast-align \
       -Wconversion \
       -Wwrite-strings \
       -Wpointer-arith \
       -Wnested-externs \
       -Wstrict-prototypes \
       -Wmissing-prototypes
CFLAGS=$(WFLAGS) \
       -g \
       -O2 \
       -ansi \
       -pedantic \
       -Dinline= \
       -fno-common \
       -fshort-enums
       -fno-common \
       -fshort-enums

编译器:

$ gcc --version i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1(Apple Inc. build 5666)(第 3 点) 版权所有 (C) 2007 Free Software Foundation, Inc. 这是免费软件;有关复制条件,请参阅来源。没有保修;甚至不是针对特定用途的适销性或适用性。

编译器警告/错误:

$ 制作 gcc -W -Wall -Werror -Wshadow -Wcast-qual -Wcast-align -Wconversion -Wwrite-strings -Wpointer-arith -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes -g -O2 -ansi -pedantic -Dinline= -fno-common -fshort-enums -c couplings.c cc1:警告被视为错误 couplings.c:在“ost”函数中: couplings.c:64: 警告:由于原型(prototype)传递宽度不同的 'os' 的参数 3 make: *** [couplings.o] 错误 1

为什么会出现此警告? os() 的参数 3 的所有内容都声明为 short int。我知道我可以摆脱 -Wconversion 来防止警告,但我宁愿解决真正的原因。不,这不是家庭作业。

谢谢。

最佳答案

Ubuntu 12.04

将问题中显示的代码放入文件 ost.c , 添加 #include <math.h> ,并使用 GCC 4.9.0 和选项在 Ubuntu 12.04 衍生版上编译:

$ gcc -g -O3 -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
> -Wold-style-definition -Wold-style-declaration -Werror -Wconversion -c ost.c
$

我根本没有收到任何警告。我在 GCC 4.6.3 和 4.8.2 上得到了相同的结果(也在同一台机器上)。

根据您的选择,我遇到了一个问题:lgamma()未声明。更改 -ansi-std=c99然后这个问题就消失了(lgamma() 是 C99 中的标准,但不是 -ansi 请求的 C89)。

Mac OS X 10.9.2 小牛

使用各种编译器(比您使用的版本更新的版本)在 Mac OS X 10.9.2 Mavericks 上测试同样没有问题。我测试了:

$ gcc --version
gcc (GCC) 4.9.0
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ /usr/bin/gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
$ clang --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
$

基本上,您遇到的是编译器中的错误,该错误已在较新版本的编译器中修复。

关于c - 警告 : passing argument 3 of 'os' with different width due to prototype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23370823/

相关文章:

types - 解释器和静态类型检查

c - 如何在 C 中制作 !default 大小写

c - Lex 和 Yacc 符号表生成和操作

c++ - 无效的迭代器 : how to get some debug info on it when the first time it is used?

c - malloc 是确定性的吗?

reflection - 为什么reflect包里没有 "byte"kind?

c - 输出意外

c - 用于 wandboard 的 c GPIO 编程

C编译器省略代码,没有错误

c - c中的3位无符号整数