confused with range of data type

MrChips

Joined Oct 2, 2009
34,854
It doesn't matter whether your computer is 8, 16, 32, or 64-bit.
It is how the compiler stores the data that matters.

int value and signed int value are the same.

Let us use 8-bit data type to keep the visuals simple.
For unsigned char:
lowest value = 00000000 = 0 decimal
highest value = 11111111 = 255 decimal

For signed char:
lowest value = 10000000 = -128 decimal
highest value = 01111111 = 127 decimal
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
from given information in page
32 bits : signed int = 2,147,483,647
C:
#include <stdio.h>

int main (void)
{
     signed int value  =  2147483647 ;

     printf( "value: %d \n", value );

    return 0;
}
value : 2147483647

32 bits : unsigned int = 4,294,967,295
C:
#include <stdio.h>

int main (void)
{
     unsigned int value  =  4294967295 ;

     printf( "value: %d \n", value );

    return 0;
value : -1

as given it should be store maximum value 4294967295 but it doesn't store this value I have checked it by program.

I just want to know How maximum value unsigned int can be store
 
Last edited:

dl324

Joined Mar 30, 2015
18,355
I just want to know How maximum value unsigned int can be store
Look in limits.h:
Code:
/* Minimum and maximum values a `signed long int' can hold.  */
#  if __WORDSIZE == 64
#   define LONG_MAX     9223372036854775807L
#  else
#   define LONG_MAX     2147483647L
#  endif
#  define LONG_MIN      (-LONG_MAX - 1L)

/* Maximum value an `unsigned long int' can hold.  (Minimum is 0.)  */
#  if __WORDSIZE == 64
#   define ULONG_MAX    18446744073709551615UL
#  else
#   define ULONG_MAX    4294967295UL
#  endif
 

nsaspook

Joined Aug 27, 2009
16,333
https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models
Many 64-bit platforms today use an LP64 model (including Solaris, AIX, HP-UX, Linux, macOS, BSD, and IBM z/OS). Microsoft Windows uses an LLP64 model. The disadvantage of the LP64 model is that storing a long into an int may overflow. On the other hand, converting a pointer to a long will “work” in LP64. In the LLP64 model, the reverse is true. These are not problems which affect fully standard-compliant code, but code is often written with implicit assumptions about the widths of data types. C code should prefer (u)intptr_t instead of long when casting pointers into integer objects.
Just because it's a 64-bit compiler don't mean short, int, long are the same length on all OS types.

Use Exact-width_integer_types for a exact bit length for the data stored in an integer.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
Look in limits.h:
this is header file
Code:
/*
* limits.h
*
* Manifest constants defining the sizes of integral types.
*
* $Id: limits.h,v 2499eb7464e6 2017/01/29 14:24:00 keithmarshall $
*
* Written by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
* Copyright (C) 1997, 1999-2001, 2004, 2005, 2010, 2012, 2017,
*   MinGW.org Project
*
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice, this permission notice, and the following
* disclaimer shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#ifndef _LIMITS_H
#pragma GCC system_header
#define _LIMITS_H

/* All MinGW headers are required to include <_mingw.h>
*/
#include <_mingw.h>

/* File system limits
*
* TODO (REVIEW): Are NAME_MAX and OPEN_MAX file system limits, or not?
* Are they the same as FILENAME_MAX and FOPEN_MAX from <stdio.h>?
*
* NOTE: PATH_MAX is the POSIX equivalent for Microsoft's MAX_PATH; the
* two are semantically identical, with a limit of 259 characters for the
* path name, plus one for a terminating NUL, making a total of 260.
*/
#ifndef __STRICT_ANSI__
# define PATH_MAX    260
#endif

/* Characteristics of the char data type.
*
* FIXME: Is MB_LEN_MAX correct?  Probably yes, for Microsoft MBCS, which
* effectively seem to all be DBCS.
*/
#define CHAR_BIT    8
#define MB_LEN_MAX    2

#define SCHAR_MIN    (-128)
#define SCHAR_MAX    127

#define UCHAR_MAX    255

#if '\x80' < 0
/* FIXME: is this safe?  I think it might just be testing
* the preprocessor, not the compiler itself.
*/
# define CHAR_MIN    SCHAR_MIN
# define CHAR_MAX    SCHAR_MAX
#else
# define CHAR_MIN    0
# define CHAR_MAX    UCHAR_MAX
#endif

/* Maximum and minimum values for ints.
*/
#define INT_MAX        2147483647
#define INT_MIN        (-INT_MAX-1)

#define UINT_MAX    0xFFFFFFFF

/* Maximum and minimum values for shorts.
*/
#define SHRT_MAX    32767
#define SHRT_MIN    (-SHRT_MAX-1)

#define USHRT_MAX    0xFFFF

/* Maximum and minimum values for longs and unsigned longs;
* this isn't correct for Alphas, which have 64 bit longs, but
* that is probably no longer a concern.
*/
#define LONG_MAX    2147483647L
#define LONG_MIN    (-LONG_MAX-1)

#define ULONG_MAX    0xFFFFFFFFUL

#ifndef __STRICT_ANSI__
/* POSIX wants this.
*/
#define SSIZE_MAX    LONG_MAX
#endif

#if _ISOC99_SOURCE
/* Implicitly defined in <_mingw.h>, (or explicitly defined by
* the user), for C99, C++, or POSIX.1-2001; make these ISO-C99
* macro names available.
*/
#define LLONG_MAX    9223372036854775807LL
#define LLONG_MIN    (-LLONG_MAX - 1)
#define ULLONG_MAX    (2ULL * LLONG_MAX + 1)
#endif

#if defined __GNUC__ && ! defined __STRICT_ANSI__
/* The GNU C compiler also allows 'long long int', but we don't
* want that capability polluting the strict ANSI namespace.
*/
#define LONG_LONG_MAX    9223372036854775807LL
#define LONG_LONG_MIN    (-LONG_LONG_MAX-1)
#define ULONG_LONG_MAX    (2ULL * LONG_LONG_MAX + 1)

/* MSVC compatibility
*/
#define _I64_MIN    LONG_LONG_MIN
#define _I64_MAX    LONG_LONG_MAX
#define _UI64_MAX    ULONG_LONG_MAX

#endif    /* __GNUC__ && !__STRICT_ANSI__ */
#endif    /* !_LIMITS_H: $RCSfile: limits.h,v $: end of file */
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models


Just because it's a 64-bit compiler don't mean short, int, long are the same length on all OS types.

Use Exact-width_integer_types for a exact bit length for the data stored in an integer.
I have completely lost here.
C:
#include <stdio.h>

int main (void)
{
     unsigned int value ;
 
     scanf ("%d", &value);

     printf( "value: %d \n", value );

    return 0;
}
I'm asking to you How much big value you can store in variable
 

ArakelTheDragon

Joined Nov 18, 2016
1,366
Yep, every compiler has its own data types. Int can be "8", "16" bits depends on the compilers. Your program is probably logically allright, but some setting or tweek is missed from it.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
You might not believe me, but many here in this thread have already answered your question.

Are you more confused now? :)
Really I 'm more confused It is difficult to believe without any fact's.

I think the answer I want is not yet given. The answer is that the program can prove that this is maximum value unsigned int can store.

As said, it's depends on the compiler. so I used online compiler to check the program. https://www.tutorialspoint.com/compile_c_online.php But here also the result is same.

I'm looking answer for post #26 I was expecting answer for unsigned int not for unsigned long

How much big value you can store in variable in given program #26?
 
Last edited:

Thread Starter

anukalp

Joined Jul 28, 2018
158
Which compiler are you using?
Does your compiler support int64_t and uint64_t?
I had downloaded compiler from this website https://sourceforge.net/projects/mingw-w64/ It supports 32-bit and 64-bit Windows
) the largest value that can be stored in the variable is 4294967296 because unsigned int has 32 bits in both cases.
4294967296 That's not true
I checked it on online compiler
https://www.onlinegdb.com/online_c_compiler
Give -1 output
Answer was posted in post #27 - line 5 in the code block.
It might be problem with my compiler but I get same result on online compiler

If you know the answer then you should prove it by program
 

dl324

Joined Mar 30, 2015
18,355
If you know the answer then you should prove it by program
What does that prove when you can read it in the header file?

Code:
main(void) {
  unsigned int ui=0xFFFFFFFF;

  printf("Wordsize:  %d bits\n", __WORDSIZE);
  printf("Size of unsigned int: %d bytes\n", sizeof(unsigned int));
  printf("unsigned int = %u\n", ui);
}
Code:
Program output:

Wordsize:  32 bits
Size of unsigned int: 4 bytes
unsigned int = 4294967295
 

dl324

Joined Mar 30, 2015
18,355
Results when trying to assign a value too large:
Code:
744> cc -o mui mui.c
mui.c: In function 'main':
mui.c:13:19: warning: large integer implicitly truncated to unsigned type [-Woverflow]
   unsigned int ui=0xFFFFFFFFF;
                   ^
745> ls -l mui*
-rwxr-xr-x 1 chip chip 5696 Aug 26 07:01 mui
-rw-r--r-- 1 chip chip  424 Aug 26 06:59 mui.c
746> mui
Wordsize:  32 bits
Size of unsigned int: 4 bytes
unsigned int = 4294967295
 

MrChips

Joined Oct 2, 2009
34,854
Pay attention to the printf( ) type field.

Line 17: %d is for signed output
Line 18: should be %lu and not %ul

Type field must be %lld and %llu for 64-bit signed and 64-bit unsigned integers.
 
Pay attention to the printf( ) type field.

Line 17: %d is for signed output
Line 18: should be %lu and not %ul

Type field must be %lld and %llu for 64-bit signed and 64-bit unsigned integers.
Yes, I should have used %lu and not %ul. Thank you. The format specifier follows the form %[flags][width][.precision][length]specifier.

When you say "Line 17: %d is for signed output" you must understand that was the whole point of using both specifiers.

But, I think I will just [let] the experts, like yourself, explain it to him - sorry [for adding to the confusion with careless errors].
 
Last edited:

Thread Starter

anukalp

Joined Jul 28, 2018
158
I was making a mistake in format specifier

consider the compiler size is 32 bit long

int : (−2,147,483,648 to 2,147,483,648 ) or (o to 4,294,967,295)

unsigned int : 0 to 4,294,967,295

signed int : −2,147,483,648 to 2,147,483,648

C:
#include <stdio.h>

int main (void)
{
     unsigned int value  =  4294967295 ;

     printf( "value: %lu \n", value );

    return 0;
}
value: 4294967295

C:
#include <stdio.h>

int main (void)
{
     signed int value  =  2147483647 ;

     printf( "value: %d \n", value );

    return 0;
}
C:
#include <stdio.h>

int main (void)
{
     int value  =  4294967295 ;

     printf( "value: %lu \n", value );

    return 0;
}
value: 4294967295

I understood now difference Thanks to all of you'rs reply and help
 
Top