.TH abs() "" "" "General Function (libc)"
.PC "Return the absolute value of an integer"
.B "#include <stdlib.h>"
\fBint abs(\fIn\^\fB)\fR \fBint \fIn\^\fB;\fR
.PP
.B abs()
returns the absolute value of integer \fIn\fR.
The \fIabsolute value\fR of a number is its distance from zero.
This is \fIn\fR if \fIn\fB>=0\fR, and \fI-n\fR otherwise.
.SH Example
This example prompts for a number, and returns its absolute value.
.DM
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
.DE
.DM
main()
{
	extern char *gets();
	extern int atoi();
	char string[64];
	int counter;
	int input;
.DE
.DM
	printf("Enter an integer: ");
	fflush(stdout);
	gets(string);
.DE
.DM
	for (counter=0; counter < strlen(string); counter++) {
		input = string[counter];
.DE
.DM
		if (!isascii(input)) {
			fprintf(stderr,
				"%s is not ASCII\en", string);
			exit(EXIT_FAILURE);
		}
.DE
.DM
		if (!isdigit(input))
			if (input != '-' || counter != 0) {
				fprintf(stderr,
					"%s is not a number\en", string);
				exit(1);
			}
	}
.DE
.DM
	input = atoi(string);
	printf("abs(%d) is %d\en", input, abs(input));
	exit(EXIT_SUCCESS);
}
.DE
.SH "See Also"
.Xr fabs(), fabs
.Xr floor(), floor
.Xr int, int
.Xr libc, libc
.Xr stdlib.h stdlib.h
.br
\*(AS, \(sc7.10.6.1
.br
\*(PX Standard, \(sc8.1
.SH Notes
On two's complement machines, the \fBabs()\fR of the most negative integer
is itself.
