What is the function to determine the min and max possible of value of datatypes (i.e, int, char.etc) in C?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
MIN and MAX values of any integer data type can be computed without using any library functions as below and same logic can be applied to other integer types short, int and long.
Look at the these pages on limits.h and float.h, which are included as part of the standard c library.
You'll want to use
limits.h
which provides the following constants (as per the linked reference):Where
U*_MIN
is omitted for obvious reasons (any unsigned type has a minimum value of 0).Similarly
float.h
provides limits forfloat
anddouble
types:You should read the article on
floats.h
carefully, thoughfloat
anddouble
can hold the prescribed minimum and maximum values but the precision with which each type can represent data may not match what it is you're trying to store. In particular, it's difficult to store exceptionally large numbers with extremely small fractions attached. Sofloat.h
provides a number of other constants that help you to determine if afloat
or adouble
can,in fact,represent a particular number.