Cao Yi wrote:
Thanks for all of you!
You're welcome but please don't top-post. The material you quote belong
before or interspersed with your text, not below it.
The %lf conversion specifier tries to convert the scanned input into a
double value, which is stored at the address specified by &cvi.
//Yes, it does!
Why are you prefixing your replies with //? Also, in future, in the
code you post, don't include C99 style comments, i.e. //. They're
mangled in Usenet which results in problems for those who want to
compile your code to verify it.
The %* tells scanf() not to store the value converted by the following
specifier into any variable.
//Oh, this is the point which confuses me so much!
// As the %ld is corresponding to &cvi, what's corresponding to %*?
Nothing. That's the purpose of the *. It tells scanf() to scan the
input according to the format specifier, but to throw away the scanned
result, since no storage is provided for it. It useful when you want to
skip past specific input fields.
I wrote a small procedure using this fract codes:
======================================
#include "stdio.h"
When you use the double quotes version of include, you're telling the
compiler to look in places other than where it would look normally by
default. Since stdio.h is a standard library header, you probably don't
want this behaviour. If you happen to have some other header named
stdio.h, the compiler is likely to include that instead of the standard
stdio.h. This could very well result in errors. Use the angled brackets
form of include when you're including standard header files and use the
double quotes form for files you supply seperately.
So modify the above include to:
#include <stdio.h>
int main() {
Under C99 standard, there're only two fully conforming forms of
prototype for main(). They are:
int main(void) and
int main(int argc, char **argv) or int main(int argc, char *argv[])
So change the above to:
int main(void)
double cvi = 0.0;
do {
printf("\nCoefficient of variation of substitution rate among sites
(must be positive)\n");
Don't include a line break directly within string literals. Use a
newline escape sequence and split a long string literal into one or
more seperate, adjacent string literals. Adjacent string literals are
automatically concactenated by the compiler. So:
printf("\nCoefficient of variation of substitution rate among
sites\n"
"(must be positive)\n");
scanf("%lf%*[^\n]", &cvi);
Always check the return value of scanf(), (and other library
functions), for failure. scanf() returns the number of fields it has
successfully converted and stored. So in this case, scanf() should
return one, if it has succeeded. So check it like:
if(scanf("%lf%*[^\n]", &cvi) != 1) deal_with_failure();
else continue_execution();
getchar();
} while (cvi <= 0.0);
Note that directly comparing floating point values may not give the
expected results. Instead you should allow for a small range of the
value above and below the value which you're testing against.
Alternatively, if your compiler supports C99 use the floating point
comparision macros provided in math.h. In this case you can do:
} while (islessequal(cvi, 0.0));
For a full list of C99 standard library functions and their
descriptions, look at the link below:
<http://www.dinkumware.com/manuals/>
Note that though most compilers support C99 partially, (often near
complete), very few claim full compliance. This means that some
functions may not be available across compilers. On the whole, it's
safer to stick with C90 for the moment. Use C99 features, only if you
really need them and you cannot portably implement them with C90 alone.
Such requirements are few.