954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

unsigned int in xcode

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);



}

vincenzorm117
Newbie Poster
23 posts since Feb 2010
Reputation Points: 6
Solved Threads: 1
 

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

vincenzorm117
Newbie Poster
23 posts since Feb 2010
Reputation Points: 6
Solved Threads: 1
 

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?

vincenzorm117
Newbie Poster
23 posts since Feb 2010
Reputation Points: 6
Solved Threads: 1
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
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).

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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?

vincenzorm117
Newbie Poster
23 posts since Feb 2010
Reputation Points: 6
Solved Threads: 1
 

Um, are the names not obvious?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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?

vincenzorm117
Newbie Poster
23 posts since Feb 2010
Reputation Points: 6
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: