Warning while using strlen in C - c

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.

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));

Related

Scanf in boolean [duplicate]

In the following example, I'm trying to scan the value of boolean type of variable. When I compile in GCC, I get following warning,
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘_Bool *’ [-Wformat=]
scanf("%d",&b);
code:
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool b;
scanf("%d",&b);
printf("%d\n",b);
}
My question is, Is there any format specifier of bool in C?
There is no format specifier for the bool type in C.
For printf, you can rely on the implicit promotion to int, and use %d as the specified formatter.
For scanf, you ought to read it into an int, and convert appropriately. Again, using %d.
There is no specifier in C for bool.
You should typecast it in printf() to avoid warnings if you want.
But there is no dedicated format specifier for represent bool type.
Try below to avoid scanf() warning:
scanf("%d",(int*)&b)

I want to create a program to compare two strings using string functions in C.What is wrong in my code?I'm a beginner in C

#include<stdio.h>
#include<curses.h>
#include<string.h>
void main()
{ const char *str1[20];
const char *str2[20];
int comp;
printf("Enter the first string:\n");
scanf("%s",&str1);
printf("Enter the second string:\n");
scanf("%s",&str2);
comp=strcmp(str1,str2);
}
This was compiled in gcc compiler 4.8 . A detailed explanation will be appreciated.
Error message:
strcmp.c: In function ‘main’:
strcmp.c:10:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘const char * (*)[20]’ [-Wformat=]
scanf("%s",&str1);
^
strcmp.c:12:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘const char * (*)[20]’ [-Wformat=]
scanf("%s",&str2);
^
strcmp.c:13:14: warning: passing argument 1 of ‘strcmp’ from incompatible pointer type [-Wincompatible-pointer-types]
comp=strcmp(str1,str2);
^~~~
In file included from strcmp.c:3:0:
/usr/include/string.h:140:12: note: expected ‘const char *’ but argument is of type ‘const char **’
extern int strcmp (const char *__s1, const char *__s2)
^~~~~~
strcmp.c:13:19: warning: passing argument 2 of ‘strcmp’ from incompatible pointer type [-Wincompatible-pointer-types]
comp=strcmp(str1,str2);
^~~~
In file included from strcmp.c:3:0:
/usr/include/string.h:140:12: note: expected ‘const char *’ but argument is of type ‘const char **’
extern int strcmp (const char *__s1, const char *__s2)
const char *str1[20];
Should be
char str1[20];
You want a mutable array of characters, not an array of pointers.
scanf("%s",&str1);
Should be:
scanf("%s",str1);
The scanf function needs the address to store the input in. That's equivalent to &str1[0] which is what str1 decays into.
Your declared arrays are of the wrong type and qualifier.
The first thing is the incorrect declarations of the string buffers. You want an array of characters, but const char* str[20] declares an array of const character pointers (that is why the compiler is talking about argument 2 with type const char * (*)[20] being the wrong type). You do not want const here either since you want the characters to be changeable. char str[20] declares a string of characters which is what you really want.
Another point in C coding is that the address of an array is the same as the name of an array, so &str1 and str1 mean the same thing.
Change const char *str1[20]; to char str1[20];,
and scanf("%s",&str1); to scanf("%s",&str1[0]);
Same with second string.
As it currently is, you're declaring, not an array of characters, but an array of pointers to characters.
You're not printing any value, so hard to know the result of the comparison.
You did not explain exactly what your code is supposed to do, but I guess that
const char *str1[20];
(the above declares an array of 20 pointers to constant strings, or zones of several char-s)
should probably be
char str1[256];
this declares a single string -which should be writable by the computer, since you read it from thje user- of at most 256 bytes, and you want it to be null-terminated
(I'm using 256, because 80 bytes is really not a lot - and 20 bytes is not enough -; you want UTF8 everywhere, and 80 bytes can be much less than 80 characters).
BTW
scanf("%s",&str1);
is poor taste (may you wanted scanf("%19s", str1);). I suggest to use fgets and code:
if (!fgets(str1, sizeof(str1)-1, stdin)) {
perror("fgets str1");
exit(EXIT_FAILURE);
}
(Hint, you should always test for successful input).
BTW, always improve your code to get no warnings, and yes you need to compile with gcc -Wall -g (all warnings and debug info) to be able to use the gdb debugger (which you need to learn).

Want to remove warning without change data-type to char

I want to remove warning without change data-type to char.
#include<stdio.h>
#include<stdlib.h>
main()
{
unsigned char ch;
printf("Hello This is Problematic\n");
scanf("%d",&ch);
printf("1\n");
}
This generates the warning
test.c:7:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘unsigned char *’ [-Wformat=] scanf("%d",&ch);
Actually,
scanf("%d",&ch);
leaves your program with undefined behavior as the supplied argument is not the correct type for the conversion specifier. You need to write
scanf("%hhu",&ch);
Quoting C11, chapter §7.21.6.2
hh
Specifies that a following d, i, o, u, x, X, or n conversion specifier applies
to an argument with type pointer to signed char or unsigned char.

How to store ASCII code of character in integer in C?

I am working on my C coursework and I need to get char as input and I want to store the ASCII value of that character in int variable. I know this can be done in the following way:
int main()
{
int x;
scanf("%c", &x);
printf("%d\n",x);
}
The problem here is that whenever I compile this program in gcc with -Wformat enabled, I get this warning:
Warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int *’
According to the specifications of the coursework, I would loose some marks if there are any compiler warnings. So, is there any another way I can do the desired task without any compiler warnings?
In my actual program, I am scanning in an array and I would also need to store other integers which would be out of the range for char, so I cannot use char x
int main()
{
signed char x;
scanf("%c", &x);
printf("%d\n",x);
}
Correctly is to define x as char but int works as well.
Char is defined by the C standard to be a data type able to keep any ASCII character, i.e. at least 127 possible values. The sign does not matter, it is not specified.
If you need to use a char as an integer the best is to define it either with signed char or unsigned char, depending what you want to do.
On the other hand, printf("%d\n",x) will work correcrly because the type coercions will transform the input signed char to integer anyway, as printf%d expects.

What do I do to stop an error "expected expression before ‘char’"?

I'm compiling my work and this error kept on appearing no matter how I edit my code:
expected expression before ‘char’
and
format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
as of the second error, I've tried to use typecasting but the problem is really persistent. Does anyone know how to?
This is a part of my code:
while ( char my_wget (char web_address[BUFLEN]) != EOF ) {
printf ("%s", (char) web_address[BUFLEN]);
Because you've got a syntax error where you wrote char and char is not allowed.
Maybe you had in mind:
int ch;
char web_address[BUFLEN];
while ((ch = my_wget(web_address)) != EOF)
printf("%s\n", web_address);
With the correct declaration for my_wget() around (such as extern int my_wget(char *buffer);), that should compile. You can't declare variables everywhere.
The second error is because web_address[BUFLEN] is a character (certainly in my code; it seems to be in yours, too, since the compiler managed to identify the type sufficiently to generate the error). It is also one beyond the end of the array if you declared it as I did. Treating a char value (probably an 8-bit quantity) as an address (pointer; probably a 32-bit or 64-bit quantity) is wrong, which is why the compiler complained.
In the printf() statement, try changing the part char to char*
Same applies to the condition in the while loop. Change the char before web_address to (char*)
I find it weird that you write "char" before my_wget(). Can you please be more specific?
See this code below
#include <stdio.h>
int main()
{
char c;
printf ("%s", st);
}
when i compile it , i get the same warning message.
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’
so i change the program to
#include <stdio.h>
int main()
{
char *str = "string";
printf ("%s", st);
}
And now the program compiles properly.
So being a newcomer to c , this is how you learn the language , write the smallest example , to prove that you have a firm grip over the concept.

Resources