You'll want to use limits.h which provides the following constants (as per the linked reference):
SCHAR_MIN : minimum value for a signed char
SCHAR_MAX : maximum value for a signed char
UCHAR_MAX : maximum value for an unsigned char
CHAR_MIN : minimum value for a char
CHAR_MAX : maximum value for a char
SHRT_MIN : minimum value for a short
SHRT_MAX : maximum value for a short
USHRT_MAX : maximum value for an unsigned short
INT_MIN : minimum value for an int
INT_MAX : maximum value for an int
UINT_MAX : maximum value for an unsigned int
LONG_MIN : minimum value for a long
LONG_MAX : maximum value for a long
ULONG_MAX : maximum value for an unsigned long
LLONG_MIN : minimum value for a long long
LLONG_MAX : maximum value for a long long
ULLONG_MAX : maximum value for an unsigned long long
PTRDIFF_MIN : minimum value of ptrdiff_t
PTRDIFF_MAX : maximum value of ptrdiff_t
SIZE_MAX : maximum value of size_t
SIG_ATOMIC_MIN : minimum value of sig_atomic_t
SIG_ATOMIC_MAX : maximum value of sig_atomic_t
WINT_MIN : minimum value of wint_t
WINT_MAX : maximum value of wint_t
WCHAR_MIN : minimum value of wchar_t
WCHAR_MAX : maximum value of wchar_t
CHAR_BIT : number of bits in a char
MB_LEN_MAX : maximum length of a multibyte character in bytes
Where U*_MIN is omitted for obvious reasons (any unsigned type has a minimum value of 0).
Similarly float.h provides limits for float and double types:
FLT_MIN : smallest normalised positive value of a float
FLT_MAX : largest positive finite value of a float
DBL_MIN : smallest normalised positive value of a double
DBL_MAX : largest positive finite value of a double
LDBL_MIN : smallest normalised positive value of a long double
LDBL_MAX : largest positive finite value of a long double
FLT_DIG : the number of decimal digits guaranteed to be preserved converting from text to float and back to text
DBL_DIG : the number of decimal digits guaranteed to be preserved converting from text to double and back to text
LDBL_DIG : the number of decimal digits guaranteed to be preserved converting from text to long double and back to text
Floating point types are symmetrical around zero, so the most negative finite number is the negation of the most positive finite number - eg float ranges from -FLT_MAX to FLT_MAX.
Do note that floating point types can only exactly represent a small, finite number of values within their range. As the absolute values stored get larger, the spacing between adjacent numbers that can be exactly represented also gets larger.
Answer from Mark Elliot on Stack OverflowYou'll want to use limits.h which provides the following constants (as per the linked reference):
SCHAR_MIN : minimum value for a signed char
SCHAR_MAX : maximum value for a signed char
UCHAR_MAX : maximum value for an unsigned char
CHAR_MIN : minimum value for a char
CHAR_MAX : maximum value for a char
SHRT_MIN : minimum value for a short
SHRT_MAX : maximum value for a short
USHRT_MAX : maximum value for an unsigned short
INT_MIN : minimum value for an int
INT_MAX : maximum value for an int
UINT_MAX : maximum value for an unsigned int
LONG_MIN : minimum value for a long
LONG_MAX : maximum value for a long
ULONG_MAX : maximum value for an unsigned long
LLONG_MIN : minimum value for a long long
LLONG_MAX : maximum value for a long long
ULLONG_MAX : maximum value for an unsigned long long
PTRDIFF_MIN : minimum value of ptrdiff_t
PTRDIFF_MAX : maximum value of ptrdiff_t
SIZE_MAX : maximum value of size_t
SIG_ATOMIC_MIN : minimum value of sig_atomic_t
SIG_ATOMIC_MAX : maximum value of sig_atomic_t
WINT_MIN : minimum value of wint_t
WINT_MAX : maximum value of wint_t
WCHAR_MIN : minimum value of wchar_t
WCHAR_MAX : maximum value of wchar_t
CHAR_BIT : number of bits in a char
MB_LEN_MAX : maximum length of a multibyte character in bytes
Where U*_MIN is omitted for obvious reasons (any unsigned type has a minimum value of 0).
Similarly float.h provides limits for float and double types:
FLT_MIN : smallest normalised positive value of a float
FLT_MAX : largest positive finite value of a float
DBL_MIN : smallest normalised positive value of a double
DBL_MAX : largest positive finite value of a double
LDBL_MIN : smallest normalised positive value of a long double
LDBL_MAX : largest positive finite value of a long double
FLT_DIG : the number of decimal digits guaranteed to be preserved converting from text to float and back to text
DBL_DIG : the number of decimal digits guaranteed to be preserved converting from text to double and back to text
LDBL_DIG : the number of decimal digits guaranteed to be preserved converting from text to long double and back to text
Floating point types are symmetrical around zero, so the most negative finite number is the negation of the most positive finite number - eg float ranges from -FLT_MAX to FLT_MAX.
Do note that floating point types can only exactly represent a small, finite number of values within their range. As the absolute values stored get larger, the spacing between adjacent numbers that can be exactly represented also gets larger.
"But glyph", I hear you asking, "what if I have to determine the maximum value for an opaque type whose maximum might eventually change?" You might continue: "What if it's a typedef in a library I don't control?"
I'm glad you asked, because I just spent a couple of hours cooking up a solution (which I then had to throw away, because it didn't solve my actual problem).
You can use this handy maxof macro to determine the size of any valid integer type.
#define issigned(t) (((t)(-1)) < ((t) 0))
#define umaxof(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | \
(0xFULL << ((sizeof(t) * 8ULL) - 4ULL)))
#define smaxof(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | \
(0x7ULL << ((sizeof(t) * 8ULL) - 4ULL)))
#define maxof(t) ((unsigned long long) (issigned(t) ? smaxof(t) : umaxof(t)))
You can use it like so:
int main(int argc, char** argv) {
printf("schar: %llx uchar: %llx\n", maxof(char), maxof(unsigned char));
printf("sshort: %llx ushort: %llx\n", maxof(short), maxof(unsigned short));
printf("sint: %llx uint: %llx\n", maxof(int), maxof(unsigned int));
printf("slong: %llx ulong: %llx\n", maxof(long), maxof(unsigned long));
printf("slong long: %llx ulong long: %llx\n",
maxof(long long), maxof(unsigned long long));
return 0;
}
If you'd like, you can toss a '(t)' onto the front of those macros so they give you a result of the type that you're asking about, and you don't have to do casting to avoid warnings.
Such a function is not defined by the C standard library. You can try defining a macro that calculates it:
#define MAX_VALUE(a) (((unsigned long long)1 << (sizeof(a) * CHAR_BIT)) - 1)
When using it, be careful it is assigned to a type large enough. For example:
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#define IS_TYPE_SIGNED(a) ((a-1) < 0)
#define MAX_VALUE_UNSIGNED(a) (((unsigned long long)1 << \
(sizeof(a) * CHAR_BIT)) - 1)
#define MAX_VALUE_SIGNED(a) (MAX_VALUE_UNSIGNED(a) >> 1)
#define MAX_VALUE(a) (IS_TYPE_SIGNED(a) ? \
MAX_VALUE_SIGNED(a) : MAX_VALUE_UNSIGNED(a))
int main(void)
{
unsigned int i = 0;
signed int j = 0;
printf("%llu\n", MAX_VALUE(i));
printf("%llu\n", MAX_VALUE(j));
return EXIT_SUCCESS;
}
This prints out:
4294967295
2147483647
You can do it quite easily with a C11 type-generic expression:
#define maxvalue(type) _Generic(type, int: INT_MAX, \
unsigned int: UINT_MAX)
It's not a function, but I think it does what you want. Here's a simple example program:
#include <stdio.h>
#include <limits.h>
#define maxvalue(type) _Generic(type, int: INT_MAX, \
unsigned int: UINT_MAX)
int main(void)
{
int i;
unsigned int ui;
printf("%u\n", maxvalue(i));
printf("%u\n", maxvalue(ui));
return 0;
}
And its output:
$ clang -Wall -Werror -Wextra -pedantic -std=c11 example.c -o example
$ ./example
2147483647
4294967295
My answers are larger than yours because my system has 32-bit integers. You appear to have a 16-bit machine.
c - How can I write a program to find maximum value of integer variable - Stack Overflow
C code to find max and min values: unexpected results
C functions in finding a max value - Stack Overflow
Get maximum value of multiple variables
As ı know the maximum value of a integer variable is 2147483647
That's a wrong assumption. The maximum value of an int can vary across systems. For example, on a 16-bit machine maximum value of int is not 2147483647.
You don't need to find it yourself. There are pre-defined macros which you can use from <limits.h>. For example, INT_MAX represents the maximum value an int can hold.
You cannot find this value programatically by means of computation. There is no way to detect that you're "at the end", and incrementing the largest integer has undefined behaviour.
That's why the largest and smallest values of integral types are made available to you by the standard library in the <limits.h> header. It is your responsibility to ensure that the results of operations fit into the desired types.
Hi everyone,
I'm trying to find the maximum and minimum values in a C array, but I'm running into a problem. My code calculates the maximum value correctly, but the minimum value is always a very large negative number, even when all the values in the array are positive.
I've tried initializing the min variable to a large positive number, but it doesn't seem to help.
Here's my code:
#include <stdio.h>
int main(void)
{
int i, sum = 0;
int numbers [5];
int min, max, average;
printf("enter 5 numbers:\n");
for (i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
sum += numbers[i];
}
max = numbers[i];
min = numbers[i];
for (i = 0; i < 5 ; i++)
{
if (numbers[i] > max)
{
max = numbers[i];
}
if (numbers[i] < min)
{
min = numbers[i];
}
}
average = (double)sum/5;
printf("Average is %d and sum is %d\n", average, sum);
printf("Max number is %d and the min number is %d\n", max, min);
}Can anyone help me figure out what's going wrong?
Thanks!
This should work fine.
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d %d %d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z){
int max;
max=x;
if(y>max){
max=y;
}
if(z>max){
max=z;
}
return max;
}
Try this one:
#include <stdio.h>
int largest(int x, int y, int z);
int main() {
int val1, val2, val3;
int maximum;
printf("enter value \n");
scanf("%d", &val1, &val2, &val3);
maximum = largest(val1, val2, val3);
printf("the largest integer is %d = \n", maximum);
return 0;
}
int largest(int x, int y, int z){
if (x >= y && x >= z)
return x;
if (y >= x && y >= z)
return y;
// otherwise
return z;
}
The problem is that you wanted the method to return the largest value, but simply didnt do that - the code is not compiling because the largest function is defined to "return" an int but there's no return statement anywhere in your function.
If you dont know what exactly "returning function" is then take a look at this tutorial: http://www.cplusplus.com/doc/tutorial/functions/
Is it there a possible way to get the maximum of let’s say 4 variables without using the If statement. Like I pull the maximum value out of variables A, B, C and D and assign it into a new variable
Where are
MINandMAXdefined in C, if at all?
They aren't.
What is the best way to implement these, as generically and type safe as possible (compiler extensions/builtins for mainstream compilers preferred).
As functions. I wouldn't use macros like #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)), especially if you plan to deploy your code. Either write your own, use something like standard fmax or fmin, or fix the macro using GCC's typeof (you get typesafety bonus too) in a GCC statement expression:
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
Everyone says "oh I know about double evaluation, it's no problem" and a few months down the road, you'll be debugging the silliest problems for hours on end.
Note the use of __typeof__ instead of typeof:
If you are writing a header file that must work when included in ISO C programs, write
__typeof__instead oftypeof.
It's also provided in the GNU libc (Linux) and FreeBSD versions of sys/param.h, and has the definition provided by dreamlax.
On Debian:
$ uname -sr
Linux 2.6.11
$ cat /etc/debian_version
5.0.2
$ egrep 'MIN\(|MAX\(' /usr/include/sys/param.h
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
$ head -n 2 /usr/include/sys/param.h | grep GNU
This file is part of the GNU C Library.
On FreeBSD:
$ uname -sr
FreeBSD 5.5-STABLE
$ egrep 'MIN\(|MAX\(' /usr/include/sys/param.h
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
The source repositories are here:
- GNU C Library
- FreeBSD
So I made a function to determine the maximum element of an array of integers. But when I try to print the value of the maximum element, it gives me a weird large number 1619117764., and isn't what I'm expecting. What am I doing wrong? Thank you so much in advance.
This is the error message:
C:\Users\USER\Desktop\Discrete Math\Max Value.c|25|warning: passing argument 1 of 'max_value' makes pointer from integer without a cast [-Wint-conversion]|
Here is my code:
#include <stdio.h>
int max_value (int input[])
{
int size=10, max;
max=input[0];
for(int i=1;i<size;i++)
{
if(max<input[i])
{
max = input[i];
}
}
return max;
}
int main ()
{
int x, max;
int input[] = {2, 3, 5, 10, 15, 42, 28, 88, 92};
x = max_value(max);
printf("/n The Max is: %d" ,x);
return 0;
}There is an implicit cast occurring in the while conditional which is causing the comparison to work on ints rather than chars.
If you change it to
while((char)(c + 1) > 0)
++c;
then it will print 127.
Signed integer overflow is undefined behavior. This means that a conforming C compiler is allowed to change c + 1 > 0 to true, because the addition "cannot" overflow.
This means that your program could legitimately be compiled to an endless loop.