Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...