0
votes

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

1
What are you trying to achieve? This is a warning about a serious bug in your program. The entire behavior of your program is undefined.StoryTeller - Unslander Monica
%d --> "%hhu" or %c, main() --> int main(void)BLUEPIXY
scanf to int, then check length and copy to charIvan Ivanov

1 Answers

2
votes

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.