Division is the last of the four basic arithmetic operations taught. Addition is easiest, followed by subtraction. Multiplication is merely aggressive addition. Division is a weird un-multiplication combined with subtraction thing, which is probably why educators save the concept for last.
In programming, as in all computerdom, division is handled by the /
operator. It’s one of the common symbols used to express division, as illustrated in Figure 1.
The symbol on the far right in Figure 1 isn’t used in higher mathematics, or even in some countries. It’s the long division construction, popular in American grammar schools, and it’s what I call the gazinta. Also, it reads backwards from the other functions.
For example, the first three expressions in Figure 1 say the same thing: 5 divided by 4. The fourth says, “4 goes into 5,” which is still “5 divided by 4,” but the way I remember it is to say “goes into” or “gazinta.”
The first three forms shown in Figure 1 express ratios, which is really what division is all about. The fourth form — the gazinta — produces a result and a remainder. So the ratio of 5/4 is 1.25, but the gazinta is 1 with 1 remainder.
The C language features a gazinta function, disappointingly called div() and not gazinta(). The function is defined in the stdlib.h
header.
The div() function requires two arguments, a numerator and a denominator. The numerator is the number on top the fraction (or ratio). The denominator is on the bottom. So in 5/4, the value 5 is the numerator and 4 is the denominator. For a gazinta, the denominator appears first, then the numerator is inside the ⟌ thing.
At this point, you’re probably wondering why bother with a div() function, and why place it in stdlib.h
and not the math.h
header? That’s because the div() function doesn’t just divide the numbers. Instead it returns a div_t structure (also defined in stdlib.h
). The div_t structure contains two int elements: quot
and rem
for quotient and remainder, respectively.
So just like the gazinta, the div() function divides two values, returning the quotient and remainder. Here’s sample code:
#include <stdio.h> #include <stdlib.h> int main() { div_t result; int n,d; printf("Enter the numerator: "); scanf("%d",&n); printf("Enter the denominator: "); scanf("%d",&d); result = div(n,d); printf("%d divided by %d equals %d with %d remaining.\n", n, d, result.quot, result.rem); return(0); }
A div_t structure result
is created at Line 6. The numerator and denominator values are obtained in Lines 9 through 12. The div() function is called at Line 14, and the results displayed in a printf() statement that spans Lines 15 through 19.
Here’s a sample run:
Enter the numerator: 5
Enter the denominator: 4
5 divided by 4 equals 1 with 1 remaining.
This type of code doesn’t deal with real numbers (float values) because, just like long division, all values are integers. It’s also different from the modulus operator (%
) because two values are generated, not just the remainder.
Thanks for this post – for some reason I was completely unaware of the div function. I am sure that on a few occasions I have written two lines of code to do what div does in one operation.
BTW, there are also ldiv and lldiv versions, returning ldiv_t and lldiv_t respectively.
When I was at school (England, 1970s) we used what you call gazinta notation. I’m not sure how they teach arithmetic here these days.
“disappointingly called div() and not gazinta()” – macro? 🙂
I didn’t think up “gazinta.” It comes from a movie or a cartoon where some character used the term to describe long division.
Regarding the function, occasionally I plow through the libraries to see what’s available. After using C for so long, it surprises me how many functions are available that I’ve never used or didn’t hear of or – worse – wrote my own function that does the same thing!