Answer: This group of functions return integer values. These functions comprise three categories viz. arithmetic, random numbers and string conversion functions. Arithmetic group of functions are prototyped below
int abs(int value); long int labs(long int value); div_t div(int numerator, int denominator); div_t ldiv(long int numerator, long int denominator);
Notice that ‘abs()’ function returns absolute value of its argument. If the result can’t be represented as an integer, the behaviour is undefined. For ex.
/* abs_labs.c -- absolute and long absolute values */ #include <stdio.h> #include <stdlib.h> int main(void) { printf("%d\n", abs(92.09998)); /* suffix L typecasts associated value into long */ printf("%ld\n", labs(123.88L)); return 0; }
Similarly, ‘labs()’ function works and returns absolute value of its argument as long integer. ‘div()’ function divides the first argument (numerator) by the second argument (denominator), produces quotient and remainder that are returned in a ‘div_t’ structure. The structure contains the fields
int quot; int rem;
though not necessarily in this order.
/* arith_div.c */ #include <stdio.h> #include <stdlib.h> #define NUM 23 #define DEN 87 int main(void) { div_t res; /* res is structure of type div_t */ res = div(NUM, DEN); printf("\"div(%d, %d)\" gives quotient: %d and remainder: %d\n", NUM, DEN, res.quot, res.rem); }
Output of program is written below
"div(87, 23)" gives quotient: 3 and remainder: 18 "div(23, 87)" gives quotient: 0 and remainder: 23
Notice that results of division using division operator “/” aren’t as precisely defined. ‘ldiv()’ function works the same as ‘div()’ function.
Let’s now consider random number functions which are prototyped below
int rand(void); void srand(unsigned int SEED);
Random numbers are useful in programs that shouldn’t produce same results every time they are executed, such as games and simulations. Numbers produced by above functions aren’t truly random because they are computed and are therefore repeatable. Hence they are called pseudo-random numbers.
‘rand()’ returns psudo-random number in range from zero to RAND_MAX. To generate numbers in smaller range, first take ‘random number modulo of size of desired range’ and then add or subtract an offset to the result as needed. To generate numbers in range 1 through 6, for example,
/* rand_range.c -- * program illustrates how to produce desired range of numbers */ #include <stdio.h> #include <stdlib.h> #define MYRANGE 11 int main(void) { int i; printf("On Linux system \"RAND_MAX\" is %ld\n", RAND_MAX); /* generate numbers in range 0 through 10 */ printf("Generate numbers in RANGE ZERO through TEN...\n"); for (i = MAXSIZE; i >= 1; i-- ) { printf("%3d", rand() % MAXSIZE); } puts(""); return 0; }
What do you observe in the output of above program given below
On Linux system "RAND_MAX" is 2147483647 Generate numbers in RANGE ZERO through TEN... 6 10 6 2 1 4 0 6 3 1 8 Generate numbers in RANGE ZERO through TEN... 6 10 6 2 1 4 0 6 3 1 8 Generate numbers in RANGE ZERO through TEN... 6 10 6 2 1 4 0 6 3 1 8
Notice that numbers are generated in range 0 through 10 and that same sequence generated each time program was executed. This is because of same SEED value. In order to obtain different sequence of pseudo-random numbers every time you execute program, use unique SEED value. Basing seed value on the time of day works well! We’ll consider how to seed random-number generator in post on “Random and Pseudo-random numbers”.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for C Internship
- Check C Books
- Practice BCA MCQs
- Apply for Computer Science Internship
- Practice Computer Science MCQs