Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use div or ldiv in C/C++?

Tags:

c++

c

Is there a specific reason to use ldiv or div instead of '/' or '%' to divide/modulus two variables?

like image 454
Sheraz Avatar asked Dec 30 '10 18:12

Sheraz


People also ask

What is the purpose of the div function?

The DIV function is used for integer division (x is divided by y). An integer value is returned.

What is Div in C?

div is a function in C programming language that takes two integers as parameters and returns the result of a division between them. It is specified in ANSI-C, and is included from the stdlib. h header when used.


1 Answers

Yes. C99 §7.20.6.2/2 says:

The div, ldiv, and lldiv, functions compute numer / denom and numer % denom in a single operation.

like image 177
James McNellis Avatar answered Sep 29 '22 14:09

James McNellis