Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANSI C (ISO C90): Can scanf read/accept an unsigned char?

Tags:

c

gcc

c89

scanf

Simple question: Can scanf read/accept a "small integer" into an unsigned char in ANSI C?

example code un_char.c:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    unsigned char character;

    scanf("%hhu", &character);

    return EXIT_SUCCESS;
}

Compiled as:

$ gcc -Wall -ansi -pedantic -o un_char un_char.c
un_char.c: In function ‘main’:
un_char.c:8: warning: ISO C90 does not support the ‘hh’ gnu_scanf length modifier

hh isn't supported by ISO C90. So what scanf conversion can be used in this situation?

like image 317
Tim Avatar asked Feb 09 '10 17:02

Tim


1 Answers

No: C89 (C90) does not support '%hhu' to read a string of digits into an unsigned char. That is a feature in C99.

You would have to read into an unsigned integer ('%u') or unsigned short ('%hu') and then check that the result is with the range of an unsigned char.

like image 52
Jonathan Leffler Avatar answered Sep 19 '22 07:09

Jonathan Leffler