Reputation: 23
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);
Upvotes: 0
Views: 230
Reputation: 134336
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
, orn
conversion specifier applies to an argument with type pointer tosigned char
orunsigned char
.
Upvotes: 2