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 Overflow
Top answer
1 of 11
107

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.

2 of 11
39

"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.

Discussions

c - How can I write a program to find maximum value of integer variable - Stack Overflow
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 . For example, INT_MAX represents the maximum value an int can hold. More on stackoverflow.com
🌐 stackoverflow.com
C code to find max and min values: unexpected results
After the first loop, the value of i is 5, and the following is undefined, you are reading past the last element of the array numbers: max = numbers[i]; min = numbers[i]; To understand why, note that the loop for (i = 0; i < 5; i++) { /* loop body */ } is exactly equivalent to i = 0; while (i < 5) { /* loop body */ i++; } The while loop stops when the test is no longer true, that is, at i=5. Therefore, just after the loop, i=5. Then, you are reading a number in memory, just after the array. It's invalid, but the program simply reads the value there. It happens to be a large negative number. If you initialize min and max at the beginning of the function, but leave those two lines, the initial value is just overwritten with this invalid value. The simplest to fix this would be to initialize with numbers[0]. And the next for loop may start at i=1, since the 0 case is already taken into account. You have another problem: since average is an int, the exact average will be truncated. You may declare average as a double instead, and change the printf format specifier accordingly. More on reddit.com
🌐 r/cprogramming
13
7
January 12, 2025
C functions in finding a max value - Stack Overflow
How to write a C Function that takes three integers as arguments and returns the value of the largest one. int largest(int x,int y,int z) { int val1,val2,val3; int maximum; printf("... More on stackoverflow.com
🌐 stackoverflow.com
Get maximum value of multiple variables
First, you can write the max of four numbers in terms of a function that takes the max of two numbers: int max(int x, int y); // function to find the maximum of two numbers int max_value = max(A, B); max_value = max(max_value, C); max_value = max(max_value, D); Now we just need to write our max function. If you mean just without using the keyword if, then you can use a ternary expression: int max(int x, int y) { return x > y ? x : y; } However, that's really just an if in disguise. If you want something without writing a conditional branch at all, you can do something like: int max(int x, int y) { int x_is_bigger = x > y; return x * x_is_bigger + y * (!x_is_bigger); } This works because x_is_bigger is either one if x is bigger, in which case we return x * 1 + y * 0, or zero if x is smaller (or equal), in which case we return x * 0 + y * 1. More on reddit.com
🌐 r/C_Programming
7
3
November 23, 2020
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-find-maximum-value-in-an-array-in-c
How to Find Maximum Value in an Array in C? - GeeksforGeeks
July 23, 2025 - Initialize a variable maxVal with value arr[0]. Traverse through the whole array. During traversal: If maxVal is less than the current value of array update the value of maxVal.
🌐
Reddit
reddit.com › r/cprogramming › c code to find max and min values: unexpected results
r/cprogramming on Reddit: C code to find max and min values: unexpected results
January 12, 2025 -

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!

🌐
w3resource
w3resource.com › c-programming-exercises › variadic › c-variadic-exercise-3.php
C Variadic function - Maximum and minimum of a variable number of integers
November 1, 2025 - Write a C program to find the maximum and minimum values of a variable number of integers passed as arguments to a function using variadic functions. ... #include <stdio.h> #include <stdarg.h> int max(int count, ...) { va_list args; int i, max, num; /* Initialize the argument list */ va_start(args, count); /* Get the first argument and set it as the maximum value */ max = va_arg(args, int); /* Compare the remaining arguments to the current maximum value */ for (i = 1; i < count; i++) { num = va_arg(args, int); if (num > max) { max = num; } } /* Clean up the argument list */ va_end(args); retur
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › int-max-min-c-plus-plus
Using INT_MAX and INT_MIN in C/C++ | DigitalOcean
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
🌐
Quora
quora.com › What-is-the-max-value-of-INT-in-C
What is the max value of INT in C? - Quora
Answer (1 of 12): From the standards (although, an older revision): 5.2.4.2.1 Sizes of integer types 1 The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the following shal...
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › limits_h.htm
C Library - <limits.h>
These limits specify that a variable cannot store any value beyond these limits, for example an unsigned character can store up to a maximum value of 255.
🌐
W3Schools
w3schools.com › c › ref_math_fmax.php
C Math fmax() Function
C Examples C Real-Life Examples ... printf("%f", fmax(96, 2048)); Try it Yourself » · The fmax() function returns the number with the highest value from a pair of numbers....
🌐
Codeforwin
codeforwin.org › home › c program to find maximum and minimum using functions
C program to find maximum and minimum using functions - Codeforwin
July 20, 2025 - Write a C program to input two or more numbers from user and find maximum and minimum of the given numbers using functions.
Top answer
1 of 16
549

Where are MIN and MAX defined 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 of typeof.

2 of 16
131

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
🌐
O'Reilly
oreilly.com › library › view › c-cookbook › 0596007612 › ch03s08.html
3.7. Getting the Minimum and Maximum Values for a Numeric Type - C++ Cookbook [Book]
November 8, 2005 - Use the numeric_limits class template in the <limits> header to get, among other things, the largest and smallest possible values for a numeric type (see Example 3-9). Example 3-9. Getting numeric limits · #include <iostream> #include <limits> ...
Authors: D. Ryan StephensChristopher DigginsJonathan TurkanisJeff Cogswell
Published: 2005
Pages: 594
🌐
Reddit
reddit.com › r/homeworkhelp › [c programming: finding maximum element in an array using functions] what's wrong with my code?
r/HomeworkHelp on Reddit: [C Programming: Finding Maximum Element in an Array Using Functions] What's wrong with my code?
April 22, 2021 -

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;
}
🌐
Programiz
programiz.com › c-programming › examples › array-largest-element
C Program to Find Largest Element in an Array
the first two elements of array are checked and the largest of these two elements are placed in arr[0]
🌐
Quora
quora.com › What-is-the-purpose-of-the-max-function-in-C-programming
What is the purpose of the 'max' function in C programming? - Quora
Answer (1 of 4): Same as max function in any language. Except C will be much more primitive. a more sophisticated generic rendering would be: max (collection [comparable]): comparable this says that we can take any collection where all the elements are comparable and find the value that is the...