How does this implementation of randomization work in C?

前端 未结 1 905
孤独总比滥情好
孤独总比滥情好 2021-01-29 05:23

Since I found this particular documentation on https://www.tutorialspoint.com/c_standard_library/c_function_rand.htm,I have been thinking about this particular

相关标签:
1条回答
  • 2021-01-29 05:33

    Yes, it will yield the same result. But the example is badly written.

    I would be careful reading Tutorialspoint. It's a site known for bad C code, and many bad habits you see in questions here at SO can be traced to that site. Ok, it's anecdotal evidence, but I did ask a user here why they cast the result of malloc, and they responded that they had learned that on Tutorialspoint. You can actually see (at least) four examples in this short snippet.

    1. They cast the result from the call to time() which is completely unnecessary and just clutters the code.
    2. For some reason they use the variable t, which is completely useless in this example. If you read the documentation for time() you'll see that just passing NULL is perfectly adequate in this example.
    3. Why use the variable n? For this short example it's perfectly ok with a hardcoded value. And when you use variables to avoid hardcoded values, you should declare them const and give them a much more descriptive name than n.
    4. Omitted #include<time.h> which would be ok if they also omitted the rest of the includes.
    5. Using int main() instead of int main(void).

    For 5, I'd say that in most cases, this does not matter for the main function, but declaring other functions as for example int foo() with empty parenthesis instead of int foo(void) could cause problems, because they mean different things. From the C standard:

    The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

    Here is a question related to that: What are the semantics of function pointers with empty parentheses in each C standard?

    One could also argue about a few other things, but some people would disagree about these.

    1. Why declare i outside the for loop? Declaring it inside have been legal since C99, which is 20 years old.
    2. Why end the function with return 0? Omitting this is also ok since C99. You only need to have a return in main if you want to return something else than 0. Personally, in general I find "it's good practice" as a complete nonsense statement unless there are some good arguments to why it should be good practice.

    These are good to remember if your goal is to maintain very old C code in environments where you don't have compilers that supports C99. But how common is that?

    So if I got to rewrite the example at tutorialspoint, i'd write it like this:

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    int main(void){
        srand(time(NULL)); 
    
        for (int i = 0; i < 5; i++) {
            printf("%d\n", rand() % 50);
        }
    }
    

    Another horrible example can be found here: https://www.tutorialspoint.com/c_standard_library/c_function_gets.htm

    The function gets is removed from standard C, because it's very dangerous. Yet, the site does not even mention that.

    Also, they teach you to cast the result of malloc https://www.tutorialspoint.com/c_standard_library/c_function_malloc.htm which is completely unnecessary. Read why here: Do I cast the result of malloc?

    And although they mention that malloc returns NULL on failure, they don't show in the examples how to properly error check it. Same goes for functions like scanf.

    0 讨论(0)
提交回复
热议问题