confused with range of data type

Thread Starter

anukalp

Joined Jul 28, 2018
158
I'm solid confused with range of data type in C. I have 64 bit computer so int data type can store 64 bit value

64 bits: maximum representable value = 18,446,744,073,709,551,615

https://en.wikipedia.org/wiki/Integer_overflow

C:
#include <stdio.h>

int main (void)
{
    int value  =  18446744073709551615;
  
    printf ("%d", value);
  
    return 0;
}
When I run program on 64 bit computer. It gives two warning

Code:
warning: integer constant is so large that it is unsigned
Warning: overflow in implicit constant conversion [-Woverflow]
Result : -1

Program should be print this value = 18446744073709551615 but I don't understand why it print -1.

How maximum value int data type can be hold in memory location ?
 

nsaspook

Joined Aug 27, 2009
13,309
gcc -Wconversion -Wsign-conversion -Werror -Wall -Wextra -pedantic -o signwarn signwarn.c
signwarn.c: In function ‘main’:
signwarn.c:8:19: error: integer constant is so large that it is unsigned [-Werror]
int value = 18446744073709551615;
^~~~~~~~~~~~~~~~~~~~
signwarn.c:8:19: error: overflow in conversion from ‘__int128’ to ‘int’ changes value from ‘18446744073709551615’ to ‘-1’ [-Werror=overflow]
 
Last edited:

bogosort

Joined Sep 24, 2011
696
Result : -1

Program should be print this value = 18446744073709551615 but I don't understand why it print -1.

How maximum value int data type can be hold in memory location ?
You assumed that int has 8-byte storage, but clearly it does not. Try this:
Code:
    printf( "sizeof(short): %lu\n"
            "sizeof(int): %lu\n"
            "sizeof(long): %lu\n"
            "sizeof(long long): %lu\n",
            sizeof(short), sizeof(int), sizeof(long),
            sizeof(long long) );
The results will be in units of bytes. The C standard only guarantees the following:

\( \small{\text{sizeof(short) \le sizeof(int) \le sizeof(long) \le sizeof(long long)}}\)

Note that, regardless of size, in two's complement encoding the most-significant bit of signed types is a sign bit. This means that half of the possible encodings are negative values. To get the full positive range use an unsigned type.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158

ArakelTheDragon

Joined Nov 18, 2016
1,362
Oct 1777777777777777777777
Bin 1111111111111111111111111111111111111111111111111111111111111111
Dec -1
Hex FFFFFFFFFFFFFFFF

Is the proper representation.
 

dl324

Joined Mar 30, 2015
16,937
I'm solid confused with range of data type in C. I have 64 bit computer so int data type can store 64 bit value
Check the definitions in your header files.

This is from a 32 bit ARM:
Code:
/* Unsigned.  */
typedef unsigned char           uint8_t;
typedef unsigned short int      uint16_t;
#ifndef __uint32_t_defined
typedef unsigned int            uint32_t;
# define __uint32_t_defined
#endif
#if __WORDSIZE == 64
typedef unsigned long int       uint64_t;
#else
__extension__
typedef unsigned long long int  uint64_t;
#endif
On a 64 bit ARM architecture, I'd have to use unsigned long int to get 8 bytes.
 

Ian Rogers

Joined Dec 12, 2012
1,136
If you google printf() you will see whats needed..

d or I signed int ( 16 bits )
u unsigned int ( 16 bits )
ld ( long signed . 32 bits)
lu ( long unsigned . 32 bits )
lld ( long long signed . 64 bits )
llu ( long long unsigned . 64 bits )

You choose...
 

Picbuster

Joined Dec 2, 2013
1,047
I'm solid confused with range of data type in C. I have 64 bit computer so int data type can store 64 bit value

64 bits: maximum representable value = 18,446,744,073,709,551,615

https://en.wikipedia.org/wiki/Integer_overflow

C:
#include <stdio.h>

int main (void)
{
    int value  =  18446744073709551615;
 
    printf ("%d", value);
 
    return 0;
}
When I run program on 64 bit computer. It gives two warning

Code:
warning: integer constant is so large that it is unsigned
Warning: overflow in implicit constant conversion [-Woverflow]
Result : -1

Program should be print this value = 18446744073709551615 but I don't understand why it print -1.

How maximum value int data type can be hold in memory location ?
Your computer is 64 bits but what about your compiler?

Picbuster
 

nsaspook

Joined Aug 27, 2009
13,309
Is this reason not being printed to the correct value.
You can use a GCC compiler __int128 variable to hold the constant (that's limited to a 64-bit value), side-step the 64 bit sign conversion in a 64-bit int and then print value.

# cat signwarn.c
C:
#include <stdint.h>
#include <stdio.h>

int main(void) {
__int128 value = 18446744073709551615;

printf ("\r\n%llu\r\n", (long long int) value);
return 0;
}
# gcc -Wall -Wextra -o signwarn signwarn.c
signwarn.c: In function ‘main’:
signwarn.c:5:24: warning: integer constant is so large that it is unsigned
__int128 value = 18446744073709551615;
^~~~~~~~~~~~~~~~~~~~
# ./signwarn

18446744073709551615
 
Last edited:

bogosort

Joined Sep 24, 2011
696
On x64, using gcc:
C:
#include <stdio.h>

int main (void)
{
  unsigned long value  =  18446744073709551615UL; // note the 'UL' suffix

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

  return 0;
}
value: 18446744073709551615
 

nsaspook

Joined Aug 27, 2009
13,309
I had downloaded compiler from this website https://sourceforge.net/projects/mingw-w64/ It supports 32-bit and 64-bit Windows

@nsaspook , @bogosort I have run both of your program on my computer It print value 4294967295

I wonder why does it print 4294967295 on my computer
My version __int128 won't work in gcc -m32 32-bit mode but this is what I get with the @bogosort code.
# gcc -Wall -Wextra -m32 -o signwarn signwarn.c
signwarn.c: In function ‘main’:
signwarn.c:9:27: warning: conversion from ‘long long unsigned int’ to ‘long unsigned int’ changes value from ‘18446744073709551615’ to ‘4294967295’ [-Woverflow]
unsigned long value = 18446744073709551615UL; // note the 'UL' suffix
^~~~~~~~~~~~~~~~~~~~~~
# ./signwarn
value: 4294967295
# gcc -Wall -Wextra -m64 -o signwarn signwarn.c
# ./signwarn
value: 18446744073709551615
Seems like your compiler was in 32-bit mode even if it runs on 64-bit windows.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
Seems like your compiler was in 32-bit mode even if it runs on 64-bit windows.
I think you're right

C:
#include <stdio.h>

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

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

    return 0;
}
This program print value: 4294967295

my original question was How maximum value int data type can be hold in memory location ?

consider three case

int value : How maximum value it can be hold in memory location ?

unsigned int value : How maximum value it can be hold in memory location ?

signed int value : How maximum value it can be hold in memory location ?
 

nsaspook

Joined Aug 27, 2009
13,309
I think you're right

C:
#include <stdio.h>

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

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

    return 0;
}
This program print value: 4294967295

my original question was How maximum value int data type can be hold in memory location ?

consider three case

int value : How maximum value it can be hold in memory location ?

unsigned int value : How maximum value it can be hold in memory location ?

signed int value : How maximum value it can be hold in memory location ?
All the information you need is in: https://en.wikibooks.org/wiki/C_Programming/stdint.h
 
Top