Reputation: 1626
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
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.
Upvotes: 0
Views: 940
Reputation: 93162
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));
Upvotes: 4