C Base Chowder

Source: Internet
Author: User
Tags rand random seed
1. #ifndef #define#endif

"#ifndef/#define/#endif in the header file prevents the header file from being repeatedly referenced."


"Repeated references" refers to a header file that has been contained multiple times in the same CPP file, which is often caused by include nesting. For example: There is a a.h file #include "c.h" and b.cpp files are imported #include "a.h" and #include "c.h" at this time will cause c.h duplicate references.


The consequences of a duplicate reference to a header file:

Some header file duplicate references just increase the workload of compiling work, do not cause too much problem, just low compilation efficiency, but for large engineering, the compilation of inefficient it would be a painful thing.
Some header files are duplicated, causing errors, such as defining a global variable in a header file (although this is not recommended, but it is actually permitted by the C specification), which can cause duplicate definitions.


Not all header files should be added to the #ifndef/#define/#endif the code, but in any case, with #ifnde xxx #define XXX #endif或者其他方式避免头文件重复包含, only the advantages of no harm.
2. extern int g

extern can be placed before a variable or function to mark the definition of a variable or function in another file, prompting the compiler to find its definition in other modules when it encounters this variable and function. Additionally, extern can be used for link designation.
3, typed struct ... Reference: https://www.cnblogs.com/qyaizs/articles/2039101.html

To define a struct type in C, use typedef:
typedef struct STUDENT
{
int A;
}stu;
So when you declare a variable, you can: Stu stu1 (If you do not have a TypeDef, you must use struct Student stu1; to declare)
The Stu here is actually the alias of struct student. Stu==struct Student
In addition here also can not write Student (so also cannot struct Student stu1;, must be Stu stu1;)
typedef struct
{
int A;
}stu;
4, typedef enum ... Reference: https://www.cnblogs.com/wgang171412/p/5382291.html

Use the TypeDef keyword to define an enumeration type as an alias and use that alias to make a variable declaration.

typedef enum WORKDAY
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; The workday here is an alias for an enumerated enum workday

Workday today, tomorrow; The types of variables today and tomorrow are enumerated workday, that is, enum workday

You can also:

typedef enum
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; The workday here is an alias for an enumerated enum workday

Workday today, tomorrow; The types of variables today and tomorrow are enumerated workday, that is, enum workday

You can also use this method:

typedef enum WORKDAY
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
};

Workday today, tomorrow; The variable today and tomorrow types are enumerated workday, that is, enum workday 5, size_t

The size_t type represents the maximum length that any object in C can reach. It is an unsigned integer, because negative numbers do not make sense here. Its purpose is to provide a portable way to declare the length consistent with addressable memory areas in the system. size_t is used as the return value type for the sizeof operator and is also a parameter type for many functions, including malloc and strlen.
It is good practice to use size_t when declaring a length variable such as a character number or an array index. It is often used for loop counters, array indices, and sometimes for pointer arithmetic operations.

SIZE_T's declaration is implementation-related. It appears in one or more standard header files, such as Stdio.h and stblib.h, and is typically defined as follows:
#ifndef __size_t
#define __size_t
typedef unsigned int size_t;
#endif
The Define directive ensures that it is defined only once. The actual length depends on the implementation. Typically, it is 32 bits long on a 32-bit system, and 64 bits on a 64-bit system. In general, the maximum possible value for size_t is Size_max.

Usually size_t can be used to hold pointers, but it is not a good idea to assume that size_t is as long as the pointer. Later on, "using the sizeof operator and pointers," the intptr_t is a better choice.
Be careful when printing values for size_t types. This is an unsigned value, and if you choose the wrong format specifier, you may get unreliable results. The recommended format specifier is%zu. In some cases, however, you cannot use this specifier as an alternative, and you can consider%u or%lu.
The following example defines a variable as size_t and then prints it in two different format specifiers:
size_t Sizet =-5;
printf ("%d\n", Sizet);
printf ("%zu\n", Sizet);
Because size_t is meant to represent positive integers, it can be problematic if used to represent negative numbers. If you assign a negative number and then print with the%d and%zu format specifiers, you get the following result:
-5
4294967291
%d treats size_t as a signed integer that prints-5 because the variable holds 5. %zu the size_t as an unsigned integer. When-5 is resolved to a signed number, the high position is 1, indicating that the number is negative. When it is resolved to an unsigned number, the high 1 is treated as a power of 2. So you see that big integer when you use the%zu format specifier.

Positive numbers are displayed as shown below:
Sizet = 5;
printf ("%d\n", Sizet); Show 5
printf ("%zu\n", Sizet); Show 5

Because size_t is unsigned, be sure to give this type of variable a positive number 6, Srand (Time (0)) The computer does not have the means to produce real random numbers, is simulated by algorithm, so you only call Rand, each time out of the same thing. After setting a seed, depending on the seed, it can produce a different number. And how to guarantee the difference of the seeds. The easiest way of course is to use the time that is always ahead.
Srand (Time (0));//Set Seed rand () first;//then Generate random numbers
Srand is a random number of seeds, and you have different seeds each time, and the random numbers you get from Rand are different. In order to plant a different seed each time, the time (0) is selected, and times (0) is the current value (because every moment is different).
Srand (Time (0)) is to the algorithm to start a seed, that is, the algorithm of the random seed number, there will be a number of random numbers, in 1970.1.1 to date, the number of seconds, initialization of random number of seeds.
When a random number is generated using RAND (), the result is 0~rand_max (the value is related to the platform, at least 32767), between the random number, but it produces random numbers of pseudo-random number, the default random number of seed is 0, so each rerun the program, will produce the same random number , if the random number produced at each run is different, the current time (0) can be used as a random number seed.

Time is a function of the C language that obtains current system times, in seconds, representing the number of seconds elapsed since the UNIX standard timestamp (January 1, 1970 0:0 0 seconds, GMT).
As can be seen from the above, time (0) produces the unit is seconds , so if you want to generate multiple random numbers in a second, then you should not use time (0) as a seed, and time (0) generally only for the rerun of the case to produce different random numbers, Otherwise the random number generated in this second will be the same. As follows:

Related Article

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.