How can I fscanf for both float and double, as I declared a global precision once in single precision (float) and once in double precision (double)... so when I run it, I have to change it everytime that I convert to a different type
in double (%lf) in single (%f)
so is there's a way that works for both and no need to change it every time?
printf()to automatically select between
doubledepending on the type of the parameter you passed it - Mysticial 2012-04-04 01:03
printf.. - R.. 2012-04-04 01:27
scanf(). But yeah, my question still holds - Mysticial 2012-04-04 01:27
Been a while since I did any coding in C, so you may want to confirm via experimentation, but all single-precision floats are valid double-precision floats, so you could just use '%lf' in the fscanf, store to a temporary double-precision float, and then use logic to assign it to the correct variable.
Alternatively, if you're using "#define"s, you could just do something like:
#define USE_DOUBLE #ifdef USE_DOUBLE #define STORTYPE double #define SCANSTRING "%lf" #else #define STORTYPE float #define SCANSTRING "%f" #endif ... STORTYPE a[x][y]; fscanf(fp1,SCANSTRING, &a[i][j]);