C Language Programming Small tip

Source: Internet
Author: User

1. Random number generation

The random number produced by int rand (void) is the same as the last time it was run, and to be different, initialize it with the function srand ().

void Srand (unsigned int seed) initializes the random number generator.

To run a code snippet:

Srand ((unsigned) time (NULL));

Rand ();

To generate random numbers.

2. Calculate Program Run time

time_t Start=clock ();

time_t End=clock ();

printf ("The Running time is:%f\n", double (End-begin)/clocks_per_sec); How many seconds are consumed by the program execution.

The clock () calculates the CPU execution time, and if there are multiple cores in parallel, the final result is the sum of the compute time on each CPU. The constant clocks_per_sec indicates how many clock units will be in a second.

3. Program hibernation

If you want the program to hibernate for 3 seconds, Windows uses sleep (3) and Linux uses sleep.

4. Strlen is used to measure how many characters are in a character array, without '/0 ', and sizeof is used to measure how much space the array occupies, so when the character array length is computed, its value is strlen (arr) +1. (a char type occupies one byte).

C Language Programming Small tip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.