One, overflow 1.short type
Data usage must be within range, otherwise unsigned generates overflow, signed error.
1Print"Short Max%d","Short Minimum Value", shrt_max,shrt_min);//32767,-32767
2Print"unsigned short max%d","unsigned short minimum value", Ushrt_max,0);//65535,0
3
4Unsigned Shortnum1=65535+2;
5printf"%d",sizeof(num));//2 bytes
6 Print"%d", NUM1);//num1=1, overflow
7
8 ShortNum2=32767+1;
9printf"%d",);//num2=-32768, error
2.int type
1). 32-bit platform,%d can be represented by the range of int, depending on the platform, the range of int is also different, 32 bits is 4byte.
1printf"%d,%d", Int_max, int_min);//2147483647,-2147483648
2 //first two numbers out of bounds, after a normal
3printf"\n%d,%d,%u", Int_max +1, Int_min-1, Int_max +1);//-2147483648,2147483647,2147483648
4 //Uint_max exceeds the range indicated by%d
5printf"\n%d,%d", Uint_max,0);//-1,0
6 //Normal situation
7printf"\n%u,%d", Uint_max,0);//4294967295,0
8 //Uint_max exceeds the range indicated by%u
9printf"\n%u,%d", Uint_max +1,0);//0,0
3.float type
1). Why int and float are allocated 4 bytes, and the value range of the two is not the same
Because int uses a common notation, and float is a scientific notation, the two formats are different, and the float type has a portion of the binary that represents the exponent. Why take an exponent, because the number of two numbers in the float type is infinite, so it cannot be used in the normal notation one by one.
2). The number of bytes occupied
1printf"%d",sizeof(1.0));//8
2printf"%d",sizeof(1.0f));//4
3printf"%d",sizeof(1u));//4 (unsigned shaping)
3). example. Calculate the perimeter and area of a triangle
1#define_crt_secure_no_warnings
2#include <stdio.h>
3#include <math.h>
4voidMain ()
5{
6floatA, B, c,s,p;
7scanf"%f%f%f", &a,&b,&c);
8P= (A + B + c)/2;
9s = sqrt (p* (p-a) * (p-b) * (p-c));
Tenprintf"%f,%f", p, s);
OneSystem"Pause");//Do not GetChar () because the carriage return is also treated as a character
A}4. Considerations for using printf functions
1). The nature of printf: no matter what type, it is converted to a string in its own way
1intnum1=1;
2printf"%d", NUM1);//1
3 printf"%f", NUM1);//0.000000
2). When printing with printf, you must type a match, or else an error occurs
1printf"%d,%u",-1,-1);//-1,4294967295
2 printf ("%x", -10);//fffffff6, unsigned hex
3 printf ("%o",-10);//37777777766, unsigned octal
Two, random number 1.rand () function
1).The rand function is a true random number generator, and Srand () sets the seed for the random number used by Rand (). If you do not call Srand () before the first call to Rand (), then the system will automatically call Srand () for you. Calling Rand () with the same number of identical numbers causes the same sequence of random numbers to be generated.
2).returns a [0,Rand_max] The random integer between.
If the program is called once, the random number generated at each invocation is the same. However, if the loop is called, the resulting random number is different.
1intnum = rand ();
2printf"%d\n", num);
The random number generated each time is the same (41).
1 for(size_t i =0; I <Ten; i++)
2{
3intnum = rand ();
4printf"%d\n", num);
5}
2). Generate 0~n random number rand ()%n2.srand () function
1).void Srand (unsigned seed)The parameter seed is the seed of Rand (), which is used to initialize the starting value of rand (). The system automatically calls Srand () before calling Rand (), and if the user has called Srand () before Rand () to specify a value for the parameter seed, rand () will use the value of the seed as the resultingPseudo-Random number, and if the user has not called Srand () before Rand (), the system defaults to 1 as the initial value of the pseudo-random number. If a fixed value is given, the sequence of random numbers produced by Rand () is the same every time.
Example:1time_t time1;//Defining a Time type variable
2Srand ((unsignedintTime (&TIME1));//random number generator, time (&time1) function to get assignment to time1
3intNum=rand ();
4printf"%d", num);
2). Method for generating 100~200 random numbers: rand ()%100+100
Learn c 4th heaven (overflow, random number)