C库函数 srand() #include <stdio.h> #include <stdlib.h> #include <time.h> int main () { int i, n; time_t t; n = 5; /* Intializes random number generator */ srand((unsigned) time(&t)); /* Print 5 random numbers from 0 to 50 */ for( i = 0 ; i < n ; i++ ) { printf("%d\n", rand() % 50); } return(0); }
C库函数 rand() #include <stdio.h> #include <stdlib.h> #include <time.h> int main () { int i, n; time_t t; n = 5; /* Intializes random number generator */ srand((unsigned) time(&t)); /* Print 5 random numbers from 0 to 49 */ for( i = 0 ; i < n ; i++ ) { printf("%d\n", rand() % 50); } return(0); }
C库函数 labs() #include <stdio.h> #include <stdlib.h> int main () { long int a,b; a = labs(65987L); printf("Value of a = %ld\n", a); b = labs(-1005090L); printf("Value of b = %ld\n", b); return(0); }
C库函数 abs() #include <stdio.h> #include <stdlib.h> int main () { int a, b; a = abs(5); printf("value of a = %d\n", a); b = abs(-10); printf("value of b = %d\n", b); return(0); }
C库函数 exit() #include <stdio.h> #include <stdlib.h> int main () { printf("Start of the program....\n"); printf("Exiting the program....\n"); exit(0); printf("End of the program....\n"); return(0); }
C库函数 atexit() #include <stdio.h> #include <stdlib.h> void functionA () { printf("This is functionA\n"); } int main () { /* register the termination function */ atexit(functionA ); printf("Starting main program...\n"); printf("Exiting main program...\n"); return(0); }
C库函数 abort() #include <stdio.h> #include <stdlib.h> int main () { FILE *fp; printf("Going to open nofile.txt\n"); fp = fopen( "nofile.txt","r" ); if(fp == NULL) { printf("Going to abort the program\n"); abort(); } printf("Going to close nofile.txt\n"); fclose(fp); return(0); }