LoginSignup
8
4

More than 5 years have passed since last update.

unsigned doubleをunsigned intとして扱う処理系は合法か?

Last updated at Posted at 2017-06-21

C - C言語のunsigned doubleとは何か?(80733)|teratail
より。

#include<stdio.h>

int main() {
    unsigned double  aa = 0.345;
    signed double bb = 0.4509;
    double cc = 0.472873;
    printf("%f\n", aa);
    printf("%f\n", bb);
    printf("%f\n", cc);
}

gccではエラーになる

prog.cc: In function 'int main()':
prog.cc:4:22: error: 'signed' or 'unsigned' invalid for 'aa'
     unsigned double  aa = 0.345;
                      ^~
prog.cc:5:19: error: 'signed' or 'unsigned' invalid for 'bb'
     signed double bb = 0.4509;
                   ^~

ところがvisual studio 2015 ver 14.0.25431.01 update3では

warning C4076: 'unsigned': cannot be used with type 'double'
warning C4076: 'signed': cannot be used with type 'double'
warning C4477: 'printf' : format string '%f' requires an argument of type 'double', but variadic argument 1 has type 'unsigned int'
warning C4477: 'printf' : format string '%f' requires an argument of type 'double', but variadic argument 1 has type 'int'

となる。ここでC4076の説明を読みに行こう。

Compiler Warning (level 1) C4076 | Microsoft Docs

'typemod' : can not be used with type 'typename'
A type modifier, whether it is signed or unsigned, cannot be used with a noninteger type. typemod is ignored.

typemod is ignoredだぁ?いやそれignoreしちゃっていいのかい?明らかに意図に反するコードを作っちゃわないかい?

ここで疑問なのはこれは合法なのかということだ。

規格書を読む

調査中です、しばらくお待ちください。情報提供もお待ちしています...

image.png

まだ規格書読み途中ですが7.1.6見る限り特に制約はしていなさそうです。

8
4
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4