用scanf如何读入long double和unsigned char?
用scanf如何读入long double和unsigned char? 语言学得不精,还请众神指点…… 网上找了一下,可是照着打不行啊。。。 以下是代码和编译信息(c++)以及版本号
#include <cstdio>
int main()
{
unsigned char test;
scanf("%hhu",&test);
printf("%hhu",test);
long double a;
scanf("%Lf",&a);
printf("%lf",a);
}
test.cpp:5:23: warning: unknown conversion type character 'h' in format [-Wformat]
test.cpp:5:23: warning: too many arguments for format [-Wformat-extra-args]
test.cpp:6:23: warning: unknown conversion type character 'h' in format [-Wformat]
test.cpp:6:23: warning: too many arguments for format [-Wformat-extra-args]
test.cpp:8:19: warning: unknown conversion type character 'L' in format [-Wformat]
test.cpp:8:19: warning: too many arguments for format [-Wformat-extra-args]
test.cpp:9:19: warning: format '%lf' expects argument of type 'double', but argument 2 has type 'long double' [-Wformat]
c++ c scanf
g++ (GCC) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[解决办法]
没见过%h, %L
unsigned字符,我们一般输入%u,%x用,但是是不安全的最好是输入到int在赋值给unsiged char
[解决办法]
unsigned char的格式输入输出单个符是%c
long double a-》lf
[解决办法]
1、unsigned char用%c
2、long double用%Lf或者是%llf
[解决办法]
VC 的long double 就是 double 是64BITS
BCB 的long double 不是 double 是80 BITS
GCC不清楚
[解决办法]
不管什么C,C++编译器,long double --->%Lf,%Le,%Lg, 都是没错的
%LE,%LG 似乎也都是没错的。
long double --->%Lf,%Le,%Lg
double ---->%lf,%le,%lg
float ---->%f,%e,%g
乱用就不好,printf,虽然,不会出大的错误,但是表示精度可能不对。
比如
double lf=10000000.3543454353423;
printf ("%f", lf);//表示精度可能就不够了;
float f =323.43543;
printf ("%lf", f);//表示精度是够了,不过没必要,精度太高,浪费了
//float不需要这么高的精度。
scanf 就跟更不能乱用了。
因为传递地址,参数本身没有问题,数据输入可能越界,或者不足。
那就根本对不上号。
scanf("%f",&lf); //变量lf需要输入8字节,可是%f 格式只输入4字节的浮点数。
//然后把lf的地址, 当作float类型的地址 ,存储输入的数据,
//只改变了 lf 的一部分,数据完全是错的。
scanf("%lf",&f); //变量f只需要输入4字节,可是%lf 格式输入8字节的浮点数。于是,变量f的邻居,就幸福了,天上掉陷饼了,
这下,莫名其妙的,修改了另一个变量的值。
不知道,编写这样代码的人,是该哭还是该笑呢???