Bill Cunningham said:
I am stumped on this one. I tried two methods and something just doesn't
seem right. I'll try my new syle.
#include <stdio.h>
#include <stdlib.h>
main() {
srand(2000); /*seed unsigned */
printf("%u",rand());
}
Now I get a number much larger than 2000. Also when I also try RAND_MAX with
srand from time to time.
You seem to be assuming that srand(2000) causes rand to return values
in the range 0 to 1999. That's incorrect. I suggest you take a few
moments to sit down and figure out just how you arrived at that
mistaken idea, and learn from it to avoid similar mistakes in the
future.
It looks to me (though I could be mistaken) that you just guessed.
That's a very bad way to determine how C standard library functions
work. (It might work better with a library with a more coherent
design, where all the design decisions actually make sense rather than
being based on random historical precedent, but even then it's a bad
idea.)
Read the documentation. It will tell you what srand() does, and what
rand() does. (It will also tell you that rand() returns an int, not
an unsigned int.)
If you want to try out a C function that you're not familiar with,
don't even *think* about writing code that uses it unless you've read
and understood the documentation first.
With main I was always told int was the default type and didn't need to be
declared. Main() has always worked. I hope this code is much more readable.
Yes, the corrected formatting makes the code much easier to read;
thank you.
As for the declaration of main, yes, most compilers will accept
"main() { ... }", but it's still a bad idea.
C89, as well as earlier pre-standard versions of the language, had an
"implicit int" rule, that many things could be declared without
specifying the type, and they would be taken to be of type int by
default. C99 (which is not yet widely implemented in full) eliminated
this rule, making "main()" illegal. But aside from whether implicit
int is allowed by a particular standard or a particular
implementation, the important point is that (with all due respect to
Dennis Ritchie), it's a *bad rule*. It's an ugly special case that
lets you save a few keystrokes but makes code harder to read.
(As I understand it, saving keystrokes was more important when C and
its ancestor languages were first designed; the teletypes of the time
required a lot more pressure to enter characters than a modern
keyboard does. That rationale has been irrelevant for many decades.)
Just because you *can* omit the "int" (sometimes), that doesn't mean
you *should*. Even ignoring the changes made in C99, if something is
of type int, you should explicitly declare it that way. And all
function declarations should be prototypes ("int main(void)" is a
prototype, "int main()" isn't).
My strong advice is that you should define main as
int main(void) { ... }
or, if you're going to use command-line arguments, as
int main(int argc, char **argv) { ... }
*and* you should explicitly return a value:
int main(void) {
/* more code here */
return 0;
}
You can get away with omitting some of these things sometimes, perhaps
most of the type, but there's no good reason why you should.