These statements determine and print the absolute value of variable n. If n is negative, then n < 0 evaluates as true and n is assigned the value of -n, which is then printed in the next statement assuming variable n to be of some integer type. However, if the value of n is positive, it remains unchanged and is printed.
Note that the above if statement works with both integral and floating point types. Recall that we can also use the abs and fabs functions to determine the absolute value of an integer and floating point expression, respectively.
Illustrates the determination of absolute values
#include <stdio.h>
#include <math.h>
void main()
{
int n = -765;
long int m = - 786548;
float x = -78.0;
double y = -67.78654;
long double z = - 564.564327;
clrscr();
printf("abs(n)= %d,\t labs(m) = %d\n", abs(n), labs(m));
printf("fabs(x)= %f\nfabs(y) = %f\nfabs(z)= %f\n", fabs(x), fabs(y),fabsl (z) );
}
Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.
Related Articles
Basic Courses
Advance Courses