i want to cast "55.77" which is a string to 55.77 which is a float
sscanf(str,"%5f",fl);
fl will hold 55.77
correct?
i want to cast "55.77" which is a string to 55.77 which is a float
sscanf(str,"%5f",fl);
fl will hold 55.77
correct?
sscanf(str,"%5f",&fl);
correct
fl is float or
long float or something
?
float - for the way you're using it.
double if you're doing
Code:sscanf(str, "%lf", &fl);
i need two digit approximation
float is only one digit after the point
and why you wrote %lf
?
is this code correct?Code:double entered_val; sscanf(str,"%f", &entered_val);
>> is this code correct?
No. Remember that the format specifier tells sscanf how large the variable is. Since %f is for floats the function will assume the variable is sizeof(float) instead of sizeof(double). In this case, you've only trashed the variable, but imagine if you had:
In this case the function expects sizeof(int) bytes at the address of 'c', and as a result the bytes following 'c' get trashed!Code:char c; sscanf(str,"%d", &c);
The moral: be very careful in matching your variables with format specifiers.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
so what specifier to use for a double variable
if i want two places after a point proximity
>> so what specifier to use for a double variable
Don't you have documentation on the library functions? You should always have a copy nearby.
>> if i want two places after a point proximity
sscanf doesn't deal with precision. It simply reads the string and stuffs the entire number into the variable. What you need to do is control the way the variable is *written* to the string (eg: 'str' in your code). For instance, if you're using sprintf to format the string, just use the width modifier (refer to sprintf reference for a full listing of options) to control the precision of the output.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }