The abs () method returns the absolute appropriate value integers and is specified in the stdlib.h header file. To return the absolute value of a particular number, we must include the stdlib.h header file in our C application. Only positive values are returned by the abs() function.
Consider the following scenario: If we have an integer number -2 and wish to find the absolute value, we may use the abs() method to have the positive number 2. In addition, when we have an integer number 2 and want to determine the absolute value, we can use the abs() method to return the very same value as 2. It gives the very same number if we provide it with any positive number.
Syntax
The syntax for abs() function is explained here:
Because the abs() method uses an integer data type, “a” is an int data type that may carry negative or positive integers and yield positive results.
Example 1
We are going to execute our first example in Ubuntu 20.04 Linux operating system. The compilation tool used here is GCC. Let’s launch the terminal window of the Ubuntu 20.04 operating system by looking and searching it in the activities. Once you get the access, implement the below-stated instruction in the shell.
- Nano means the file will be opened in the nano editor
- Abs means the name of the file
- .c is the file extension depicting the code will be of C programming language.
The file will be empty and opened in the GNU nano editor. We have started by adding some libraries. Then we have a main() function where we have stated two variables, “a” and “b”. We have assigned them values in the program as well. Then two printf() functions in which we are going to print the absolute values variable “a” and “b”.
After getting the whole understanding, save and close the file to get back to the terminal.
Now to get the output, write the following appended commands on the terminal of the Ubuntu 20.04 operating system.
$ ./a.out
The exact output can be seen on the terminal.
Example 2
We are going to execute our second example in Ubuntu 20.04 Linux operating system. The compilation tool used here is again GCC. Let’s search the terminal window of the Ubuntu 20.04 operating system by looking at it in the activities. Once you get the access, implement the underneath stated command in the shell.
- Nano means the file will be opened in the nano editor
- Abs2 means the name of the file
- .c is the file extension depicting the code will be of C programming language.
The file will be empty and opened in the GNU nano editor. We have started by adding some libraries. Then we have a main() function where we have declared two variables, “a” and “numb”. In this code example, we prompt the users to enter their desired values at run time to get their absolute value using the printf() statement. Then we have called the abs() function. Again we have printf() functions in which we are going to print the absolute value of variable “a” and store it in a “numb”
After getting the whole understanding, save and close the file to get back to the terminal.
Now to get the output, write the following appended commands on the terminal of the Ubuntu 20.04 operating system.
$ ./a.out
In its output, the program asks you to input the value. Then the absolute value will appear against that specific number.
Conclusion
This guide was all about the abs() function in the C programming language. We have explained the concept along with its syntax in detail. Then we have implemented two different examples in which one of them will get the number from the user at run time, and the other will be declared in the program. Now, after little amendments as per your requirements, implement both of these examples.