First, the random number generation function
1, need to add header file #include <stdlib.h> and #include <time.h>
2, Rand is a pseudo-random number generator, and each call to Rand produces the same random number.
3. Any random number can occur if you call Srand before calling Rand.
4, as long as the Srand function can be guaranteed each time the value of the parameter is different, then the RAND function will certainly produce a different random number.
5. Example:
intMainvoid){ intT = (int) time (NULL); Srand (t); //random number seed inti; for(i=0;i<Ten; i++) {printf ("%d\n", Rand ());//generate random numbers (different random numbers will be generated for each run) } return 0;}
Two, string input and output function
1. scanf function
Char a[100] = {0};
scanf ("%s", a); Indicates that a string is entered, scanf is identified with a return key or a space as input, but the enter is not itself part of the string.
Note: If the array length in the scanf parameter is less than the length of the user's keyboard input, then the scanf buffer overflows, causing the program to crash.
For example:
#include <stdio.h>intMainvoid){ Chars[Ten] = {0}; scanf ("%s", s); inti; for(i=0;i<Ten; i++) {printf ("%d", S[i]); } printf ("%s\n", s); return 0;}
2. Use of the Gets () function
1, get () input, can not just like "%s" or "%d" or the like character escapes, can only receive the input string.
2. Example:
#include <stdio.h>#include<stdlib.h>intMainvoid){ Chars[ -] = {0}; Gets (s); //Input: Hello World, The Get () function is also getting user input, it puts the acquired string into S, and only treats the enter as the end flag, but also has an overflow problemprintf"-------\ n"); printf ("%s\n", s);//output: Hello World return 0;}
3, get () Gets the user input, the atoi () function converts the string to a number, the header file is added #include <stdlib.h>
#include <stdio.h>#include<stdlib.h>intMainvoid){ Chara[ -] = {0}; Charb[ -] = {0}; Gets (a); //gets the first input, the object of a can only be an array, cannot be escaped (string converted to a number), need to use special functionsgets (b); intI1 = Atoi (a);//Converts a string to an integer intI2 =atoi (b); printf ("%d\n", i1+i2); return 0;}
4, fgets () function usage--gets () function of the upgraded version
#include <stdio.h>#include<stdlib.h>intMainvoid) { Charc[Ten] = {0}; Fgets (c,sizeof(c), stdin);//The first parameter is an array of char, the second parameter is the size of the array, the unit bytes, and the third parameter represents the standard input. //Input: Hello Worldprintf"%s\n", c);//output: Hello WOR--It also includes 0 of the end of the string, and Fgets () is automatically truncated to prevent overflow, so it's safe .//when calling fgets (), buffer overflow can be avoided as long as the second parameter is guaranteed to be less than the actual size of the array. return 0; }
5, puts () function, the user's input is printed out
#include <stdio.h> #include <stdlib.h>int Main (void ) { char d[100 ] = {0 }; Gets (d); printf ( " ------\ n " ); Puts (d); // auto output with newline return 0 ; }
6, fputs () function, is the puts file operation version
#include <stdio.h>#include<stdlib.h>intMainvoid) { Chare[ -] = {0}; Fgets (E,sizeof(e), stdin);//Hello World Myloveprintf"----------\ n"); Fputs (e,stdout); //Hello World Mylove return 0; }
C language random number and string input and output