Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250881

how to fetch the largest value of a given data type in c?

I am new to C.

for.eg say int has a largest value of 32767(but this value can be different for other system), my question is how to obtain these value for any datatype using a C program or function.

for int I was trying this approach, but it returns -1

#include<stdio.h>
void main(void){

unsigned int x=1;
unsigned long count=0;

for(x=1;;x++){

    if(x==0){break;}
    count++; 

    }

printf("%ld",count);

}

Upvotes: 0

Views: 406

Answers (6)

MOHAMED
MOHAMED

Reputation: 43518

Solution to get the max of int in running time:

int main ( )
{
    unsigned int y = (unsigned int)(~0)>>1;
    printf ("max of int is %u\r\n",y);
    return 1;
}

explaination:

y = ~0 set y with the opposite in binary of 0. If our system is 32 bits so 0 will be represent in binary with (0000...0)(32) and its opposite in binary is (1111...1)(32).

y>>1 shift the y with 1 bit so the value of y will be (0111..1)(31) which is the max of int

Upvotes: 0

user1473808
user1473808

Reputation: 19

Just print -1

#include "stdint.h"

printf("Byte :%u, Short :%hu,  Int :%u, Long Long :%llu\n",
             (uint8_t)-1, (uint16_t)-1, (uint32_t)-1, (uint64_t)-1);

The Output is:

Byte :255, Short :65535, Int :4294967295, Long Long :18446744073709551615

I am on linux...

Upvotes: 0

CAFxX
CAFxX

Reputation: 30281

What you need is #include <limits.h>

Upvotes: 1

Steve Jessop
Steve Jessop

Reputation: 279225

The header limits.h has a constant UINT_MAX, which is the value you want.

The reason you saw -1 is just that %ld is the wrong printf format for unsigned long. Use %lu.

C isn't really geared for generic programming, but the most recent standard C11 does provide macros that do different things according to the type of the argument ("type-generic expressions").

I've never used them, but I expect you could write a macro NUMERIC_LIMITS_MAX to hide the mapping int -> INT_MAX, unsigned char -> UCHAR_MAX, etc. The resulting code would not (yet) be very portable, since there aren't many partial implementations of C11, let alone complete ones.

That said, for unsigned types you can always get the max value as (unsigned long)(-1) and so on. The signed types are a bit awkward, because the standard gives implementations freedom to do a lot of strange things. In practice, INT_MAX is equal to (((unsigned int)(-1)) / 2) + 1. In theory, int is allowed to have more padding bits than unsigned int, in which case INT_MAX is smaller.

Upvotes: 4

marktani
marktani

Reputation: 7808

Check this. Generally when you have a question regarding C or C++ try to first look it up at this site, it's a great reference.

Upvotes: 3

gvl
gvl

Reputation: 943

limits.h defines a few values that represent the limits (or bounds) of types. In your case, INT_MIN and INT_MAX would be a portable way to find out the minimum and maximum values an int can hold.

http://en.wikibooks.org/wiki/C_Programming/C_Reference/limits.h

Upvotes: 6

Related Questions