Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在这个C代码中如何进行转换? #包括 #包括 无效打印长度(字符*s,字符*t){ 无符号整数c=0; int len=((strlen(s)-strlen(t))>c?strlen(s):strlen(t); printf(“%d\n”,len); } void main(){ char*x=“abc”; char*y=“defgh”; 打印长度(x,y); }_C_String_Unsigned - Fatal编程技术网

在这个C代码中如何进行转换? #包括 #包括 无效打印长度(字符*s,字符*t){ 无符号整数c=0; int len=((strlen(s)-strlen(t))>c?strlen(s):strlen(t); printf(“%d\n”,len); } void main(){ char*x=“abc”; char*y=“defgh”; 打印长度(x,y); }

在这个C代码中如何进行转换? #包括 #包括 无效打印长度(字符*s,字符*t){ 无符号整数c=0; int len=((strlen(s)-strlen(t))>c?strlen(s):strlen(t); printf(“%d\n”,len); } void main(){ char*x=“abc”; char*y=“defgh”; 打印长度(x,y); },c,string,unsigned,C,String,Unsigned,当我编译它时,它给出了3,但是,我不明白转换是如何发生的:(strlen(s)-strlen(t))>c这是非常糟糕的代码(strlen(s)-strlen(t))总是>=0,因为它是无符号的数学。strlen()返回的类型是size\u t,是一些未签名的类型。因此,除非值相等,否则由于无符号数学环绕,差值始终为正数 然后int len=strlen(s)即使s的长度与t不同 使用类似代码的更好方法是只添加 #include<stdio.h> #include<string.

当我编译它时,它给出了3,但是,我不明白转换是如何发生的:(strlen(s)-strlen(t))>c

这是非常糟糕的代码
(strlen(s)-strlen(t))
总是>=0,因为它是无符号的数学。
strlen()
返回的类型是
size\u t
,是一些未签名的类型。因此,除非值相等,否则由于无符号数学环绕,差值始终为正数

然后
int len=strlen(s)
即使
s
的长度与
t
不同

使用类似代码的更好方法是只添加

#include<stdio.h>
#include<string.h>

void printlength(char *s, char *t) {
    unsigned int c=0;
    int len = ((strlen(s) - strlen(t)) > c) ? strlen(s) : strlen(t);
    printf("%d\n", len);
}

void main() {
    char *x = "abc";
    char *y = "defgh";
    printlength(x,y);
}


注意:在极少使用
SIZE\u MAX的平台上,这是非常糟糕的代码
(strlen(s)-strlen(t))
总是>=0,因为它是无符号数学。因此优化器可以转换为
printf(“%d\n”,(int)strlen(s))
@M.M,除非字符串长度相同,则
printf(“%d\n”,(int)strlen(t))
@chux如果长度相同,则可以用
strlen(t)
替换
strlen(s)
@M.M是的!在那一点上,你的大脑比我的强。总是吗?如果SIZE_MAX@PaulHankin没有差异,因为通过与
unsigned int
variable@PaulHankin减法中使用的类型不受
c
类型的影响,因此差值始终>=0。结果比较将应用于
size\u t
unsigned
中较宽的一个。尽管未指定,但从未遇到过
无符号
大小\u t
更宽的平台-也许某些64位图形处理器可能会使用您的前提。此外@PaulHankin premise
size\u MAX@Barry没有类型转换(例如
(int)
)根据整数升级进行类型转换。比较
采用左侧(
size\u t
或很少
int
)和右侧(
unsigned
)的类型,并将一方提升到另一方的更高级别,这当然是
size\u t
unsigned
。在任何情况下,比较都是通过一些无符号的数学来完成的。
// ((strlen(s) - strlen(t)) > c) 
(strlen(s) > (c + strlen(t))