简体   繁体   English

警告unsigned long to int in C语言

[英]warning unsigned long to int in C language

I'd have a quick question over my codes: 我对我的代码有一个快速的问题:

int main(void)
{
    int i,j,key[5][5],ikey[5][5],row,col,plen,suc;
    int devide,count,h,k,no,p1[100],e1[100],d1[100];
    char p[100],e[100],d[100],clen;

    printf("Enter your plaintext::::::::");
    gets(p);
    plen = strlen(p); // this line gets error

warning line is: 警告线是:

plen = strlen(p);

implicit conversion loses integer precision unsigned long to int. 隐式转换会丢失整数符号无符号长整数到int。

As already mentioned the function strlen(p) returns a value of type size_t . 如前所述,函数strlen(p)返回一个size_t类型的值。 Here is an abstract from the C99 standard: 这是C99标准的摘要:

size_t size_t

which is the unsigned integer type 这是无符号整数类型

Now the point here is that what exact type it is( unsigned , unsigned long etc.) is actually implementation-defined, which means it varies depending on the platform. 现在的要点是,它的确切类型( unsignedunsigned long等)实际上是实现定义的,这意味着它取决于平台而有所不同。 Obviously you have it defined as unsigned long which means that by 显然,您已将其定义为unsigned long ,这意味着

plen = strlen(p);

you convert unsigned long to int which the compiler can't ignore because in some cases this conversion may lose the value's precision. 您将unsigned long转换为编译器无法忽略的int ,因为在某些情况下,这种转换可能会失去值的精度。

The correct declaration: 正确的声明:

#include <stddef.h>

size_t plen;
// ...
plen = strlen(p);

As others have already pointed out, strlen() returns a size_t . 正如其他人已经指出的那样, strlen()返回size_t

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

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