Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the abs () function and div () function of C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use the abs () function and div () function in C language". In daily operation, I believe many people have doubts about how to use the abs () function and div () function in C language. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about how to use the abs () function and div () function in C language. Next, please follow the editor to study!

Many functions are provided in the C language library, so that when you need to calculate, you can directly borrow the library function without having to rewrite the function yourself.

Int _ cdecl abs (int _ X); long _ cdecl labs (long _ X); div_t _ cdecl div (int _ Numerator,int _ Denominator); ldiv_t _ cdecl ldiv (long _ Numerator,long _ Denominator)

The abs function is used to calculate the absolute value of an integer, its argument is the integer to be calculated, and the return value is the calculated absolute value. Write a simple piece of code to demonstrate the use of this function.

# include # include int main (int argc, char** argv) {int x1 = 56 return x2 =-89; int y1 gift y2; y1 = abs (x1); y2 = abs (x2); printf ("% d% d", y1Powery2); return 0;}

Define a positive integer and a negative integer, then use the abs function to calculate the absolute values, and then print out the results of the calculation.

If the number passed in is not an integer, what is the output?

Pass a positive floating point number and a negative floating point number into the abs function, respectively, and the calculated result is still a positive integer, indicating that after passing parameters, the abs function will force the passed parameters into integers and then calculate them.

The function of the labs function is the same as that of the abs function, but it is mainly used to calculate the absolute value of long integers.

The div function is used to calculate the quotient and remainder of the division of two digits. It has two parameters, divides the first parameter (numerator) by the second parameter (denominator), and then stores the quotient and the remainder in the structure, and the result returned is a structure. Its structure is defined as follows:

Typedef struct _ div_t {int quot; int rem;} div_t; typedef struct _ ldiv_t {long quot; long rem;} ldiv_t

Quot is the quotient and rem is the remainder. Let's demonstrate the use of this function through a piece of code.

# include # include int main (int argc, char** argv) {int x1 = 10dint x2 = 3; div_t ret; ret = div (x1memx2); printf ("% d% d", ret.quot,ret.rem); return 0;}

Calculate the quotient and remainder of 10 / 3, and the output result is as follows:

Similarly, negative integers can also be calculated using this function.

The function of the ldiv function is the same as that of the div function, but it is mainly used to calculate the division of long integers.

At this point, the study on "how to use the abs () function and div () function in C language is over. I hope to be able to solve your doubts." The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report