Before you read, I am using Mac's Xcode. In this program I am attempting to print the maximum and minimum values of standard C variables with every variation of modifiers. The unsigned int uses 4 bytes therefore it must have a maximum value of 4294967296 but it only display 2147483647 as its maximum value. My goal is to have xcode print an unsigned integers maximum value which is 4294967296; am I doing anything incorrectly in the program?

You can focus on the second line of variables declared in main() and the last 2 printf functions.

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int main (int argc, const char * argv[])
{
    signed int minSignedInt={-2147483648},maxSignedInt={2147483647};
    unsigned int minUnsignedInt={0},maxUnsignedInt={4294967296};
    
    
        printf( "sizeof( bool ) = %d\n", (int)sizeof( bool ) );
        printf( "sizeof( char ) = %d\n", (int)sizeof( char ) );
	printf( "sizeof( short ) = %d\n", (int)sizeof( short ) );
	printf( "sizeof( int ) = %d\n", (int)sizeof( int ) );
        printf( "sizeof( float ) = %d\n", (int)sizeof( float ) );
	printf( "sizeof( long ) = %d\n", (int)sizeof( long ) );
	printf( "sizeof( double ) = %d\n", (int)sizeof( double ) );
	printf( "sizeof( long double ) = %d\n\n", (int)sizeof( long double ) );

    printf("sizeof( rand() ) is %d\n",(int)sizeof(rand()));
    
    printf("Max. signed int (or integer) value = %d\n",maxSignedInt);
    printf("Min. signed int (or integer) value = %d\n\n",minSignedInt);
    printf("Max. Unsigned int (or integer) value = %d\n",maxUnsignedInt);
    printf("Min. Unsigned int (or integer) value = %d\n\n",minUnsignedInt);
 

  
}

Recommended Answers

All 8 Replies

The %d specifier prints a signed int. If you want to print an unsigned value, use %u.

Also, you'd be better off using INT_MIN, INT_MAX, and UINT_MAX from limits.h. That'll protect you from the off-by-one trap that you fell into with 4294967296.

The %d specifier prints a signed int. If you want to print an unsigned value, use %u.

Also, you'd be better off using INT_MIN, INT_MAX, and UINT_MAX from limits.h. That'll protect you from the off-by-one trap that you fell into with 4294967296.

Thank you very much, your proposal has completely solved my problem and your idea to use limits.h has provided a more efficient algorithm.

An additional note. Do you know if there is any defined value for floats, doubles, and long doubles similar to INT_MIN and INT_MAX?

It would be in limits.h if there are any.

It would be in limits.h if there are any.

Actually, there's a separate float.h for that information because there's more to it than just min/max (such as the radix and epsilon values).

Do you know which ones are the maximum and minimum ones for float, double, and long double? Correct me if Im wrong; is it the FLT_MIN,FLT_MAX,DBL_MIN,DBL_MAX,LDBL_MIN, and LDBL_MAX?

Um, are the names not obvious?

Never mind I figured it out. Im about done with the program. You've helped me a lot, so would you like me to send you a copy of it when I finish it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.