使用C中的strlen警告

编程入门 行业动态 更新时间:2024-10-27 04:32:21
本文介绍了使用C中的strlen警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是新的C programming.I正在试图通过每行线和打印长度采取从文件线路输入。这是我的code -

I am new to C programming.I am trying to take input from a file line by line and print length of each line. This is my code-

#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char name[100]; printf("Enter file name\n"); scanf("%s",name); FILE * input; input=fopen(name,"r"); char line[100]; fscanf(input,"%s",line); while(!feof(input)) { printf("%d\n",strlen(line)); fscanf(input,"%s",line); } return 0; }

这是我的输入文件 -

This is my input file-

172.24.2.1 172.24.2.225 172.17.4.41 172.24.4.42 202.16.112.150 172.24.152.10

这是正确打印编译期间每个line.But的长度,我得到这样的警告 -

This is correctly printing the length of each line.But during compilation I get this warning-

In function ‘main’: main.c:24:4: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘size_t’ [-Wformat=] printf("%d\n",strlen(line));

所以我的问题是,为什么我会收到这样的警告,虽然我的输出是正确的,我怎么能解决这个问题?先谢谢了。

So my question is why am I getting this warning although my output is correct and how can I fix this? Thanks in advance.

推荐答案

功能的strlen 返回类型的对象为size_t (参见ISO 9899:2011§7.24.6.3)。您需要指定传递给的printf 参数的类型为为size_t ,而不是 INT 。为此,您可以通过添加以Z 长度修改(参见ISO 9899:2011§7.21.6.1/ 7)指定类型为size_t 。你也应该使用 U 格式符为为size_t 是一个无符号类型。

The function strlen returns an object of type size_t (cf. ISO 9899:2011§7.24.6.3). You need to specify that the argument you pass to printf has type size_t, as opposed to int. You can do that by adding a z length modifier (cf. ISO 9899:2011§7.21.6.1/7) specifying an object of type size_t. You should also use the u formatting specifier as size_t is an unsigned type.

printf("%zu\n",strlen(line));

更多推荐

使用C中的strlen警告

本文发布于:2023-07-30 00:45:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1245049.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:strlen

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!