Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the maximum value of a variable in C

Tags:

c

Is there a function in C that returns the maximum value of a variable like this (I will name the function "maxvalue" in example below)?

int a;
printf("%d", maxvalue(a)); // 32767
unsigned int b;
printf("%d", maxvalue(b)); // 65535

So basically the function returns values like INT_MAX when the variable is signed INT, UINT_MAX when unsigned int, etc.

like image 775
user1574556 Avatar asked Oct 06 '12 17:10

user1574556


People also ask

Is there a max function in C?

Say max() function is used to find maximum between two numbers. Second, we need to find maximum between two numbers. Hence, the function must accept two parameters of int type say, max(int num1, int num2). Finally, the function should return maximum among given two numbers.

What is the max int in C?

Value of INT_MAX is +2147483647. Value of INT_MIN is -2147483648.

How do you find a max value?

If you are given the formula y = ax2 + bx + c, then you can find the maximum value using the formula max = c - (b2 / 4a). If you have the equation y = a(x-h)2 + k and the a term is negative, then the maximum value is k.


4 Answers

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
like image 116
Mike Kwan Avatar answered Oct 24 '22 12:10

Mike Kwan


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.

like image 32
Carl Norum Avatar answered Oct 24 '22 12:10

Carl Norum


Here are macros from my library that work for types, rather than variables:

/* min and max integer values.  T is a signed or unsigned integer type. */

/* Returns 1 if T is signed, else 0. */
#define INTTYPE_SIGNED(T) ((T)-1 < (T)0)

/*
 * Returns (T)(maximum value of a T).
 *
 * Pains are taken (perhaps unnecessarily) to avoid integer overflow
 * with signed types.
 */
#define INTTYPE_MAX(T)                      \
    (((T)1 << (CHAR_BIT*sizeof(T)-INTTYPE_SIGNED(T)-1)) - 1 +   \
     ((T)1 << (CHAR_BIT*sizeof(T)-INTTYPE_SIGNED(T)-1)))

/*
 * Returns (T)(minimum value of a T).

 * Pains are taken (perhaps unnecessarily) to avoid integer overflow
 * with signed types.
 * assert: twos complement architecture
 */
#define INTTYPE_MIN(T) ((T)(-INTTYPE_MAX(T)-1))

Edit: Adapting these to the question:

/* min and max integer values.  V is a signed or unsigned integer value. */

/* Returns 1 if V has signed type, else 0. */
#define INT_VALUE_SIGNED(V) ((V)-(V)-1 < 0)

/*
 * Returns maximum value for V's type.
 *
 * Pains are taken (perhaps unnecessarily) to avoid integer overflow
 * with signed types.
 */
#define INT_VALUE_MAX(V)                        \
    (((V)-(V)+1 << (CHAR_BIT*sizeof(V)-INT_VALUE_SIGNED(V)-1)) - 1 +    \
     ((V)-(V)+1 << (CHAR_BIT*sizeof(V)-INT_VALUE_SIGNED(V)-1)))

/*
 * Returns minimum value for V's type.

 * Pains are taken (perhaps unnecessarily) to avoid integer overflow
 * with signed types.
 * assert: twos complement architecture
 */
#define INT_VALUE_MIN(V) (-INT_VALUE_MAX(V)-1)

Afterthought: These invoke UB if V is a variable, or an expression containing variables, that have not been assigned a value ... which is the case in the question as asked. They are likely to work on many implementations, but the C standard doesn't guarantee it, and they will certainly fail on an implementation that initializes uninitialized variables with trap values.

like image 35
Jim Balter Avatar answered Oct 24 '22 12:10

Jim Balter


Could easily be done using ANSI C 89:

    #include<stdio.h>
    #include<limits.h>

int main(void) {

printf("Max value of char: %d\n", CHAR_MAX);
printf("Min value of char: %d\n", CHAR_MIN);

printf("Max value of short: %d\n", SHRT_MAX);
printf("Min value of short: %d\n", SHRT_MIN);

printf("Max value of int: %d\n", INT_MAX);
printf("Min value of int: %d\n", INT_MIN);

printf("\n\n");
return 0;
}

Notice that you can include float.h and then use:

printf("Max value of Double: %d\n", DBL_MAX);

Though, it is less recommended.

Good luck, Ron

like image 24
Ron Nuni Avatar answered Oct 24 '22 12:10

Ron Nuni