3

I've never come across such variable types in python(even though I am still a beginner in python and have just started C).

I've come across variable types in C and am confused at the point where the range of values are given like this. I've made the following deductions:

  • The storage size is the amount of space that the specific type of variable can hold. But regarding the value range I have a lot of doubts:

  • I think the value range is the integer upto which we can enter into the specific variable:

Example:

I think for signed char the storage size is 1 byte and we could not enter variable1 = 128, but I was wrong when I compiled the simple program;

#include <stdio.h>
int main(void)
{
  signed char variable1;
  variable1 = 128;
  printf("the value is: %d", variable1);
  return 0;
}  
The output of the compiler(using dev C++) was :  
  -127  

So I thought It would start from the minimum once it crossed it's range and produce an error after 255 and I was right. The same was true for the type short int from type -32,768 to 32,767. But, this failed for the type int -2,147,483,648 to 2,147,483,647. It continued to accent.

Therefore, my whole perspective about the value ranges went to nought. It might be something more complex than I had imagined. So what is the value range of variables, and how can we interpret all of these weird results?

EDIT1: I edited to upload my experiments:

experiments

BumbleBee
  • 87
  • 2
  • 16
  • The article you link to is hopelessly out of date and incorrect - the table of int sizes and ranges is particularly bad. You should probably avoid that web site in general and use a more reliable reference. – Paul R Oct 12 '16 at 06:50
  • 3
    The value returned is -127 because overflow occured.Welcome to `stackOverflow` :) – Gurupad Mamadapur Oct 12 '16 at 06:51
  • 1
    I think the output would be the -128 – Bhavdip Sagar Oct 12 '16 at 06:55
  • 1
    http://ideone.com/RBGM3v – Abhineet Oct 12 '16 at 07:08
  • 2
    Overflow in signed integer types results in undefined behaviour. See for example http://stackoverflow.com/questions/3948479/integer-overflow-and-undefined-behavior – Klas Lindbäck Oct 12 '16 at 07:16
  • 1
    Unlike Python, C assumes that you understand computers. The maximum value of a signed byte will be the same in your C program as it is in your CPU's instruction set. It is extremely likely that the CPU is using two's complement format, meaning that one byte can have values from -128 to +127. To fully understand these things, you need to first study binary number formats. Which as it turns out, is mandatory pre-requisite knowledge before studying programming. – Lundin Oct 12 '16 at 07:43
  • @Paul R, I have Horton's fourth edition of c as a main reference, but the table somehow matches. The only difference is the int is also sub-divided into short and long int. Is that wrong? – BumbleBee Oct 12 '16 at 08:01
  • @GurupadMamadapur, the compiler doesn't show an overflow error as it does when I enter(128*2) – BumbleBee Oct 12 '16 at 08:02
  • @BumbleBee: there are lots of bad books out there (Kantekar's books are by far the worst, but there are many other bad books too) - I suggest you pick a book from [this list](http://stackoverflow.com/q/562303/253056). In the mean time you may find [this Wikipedia page](https://en.wikipedia.org/wiki/C_data_types) useful. – Paul R Oct 12 '16 at 08:04
  • @Abhineet, I frankly didn't understand this(sorry) but is that the output on some kind of an online compiler or something. Dev C++ doesn't really show that – BumbleBee Oct 12 '16 at 08:04
  • @BumbleBee it's not a compiler error, I never said it was. It is an undefined behaviour occurring due to the overflow. – Gurupad Mamadapur Oct 12 '16 at 08:07
  • @BumbleBee - Yeah, IdeOne is one of the online IDE. The basic point is, you are experiencing Undefined Behavior which of course can't be defined as per C standards. – Abhineet Oct 12 '16 at 08:07
  • @Lundin, Binary number format? I'm aware of binary number system which comes as a included course(basic level) in high school mathematics. Is this what you meant? and Is this knowledge sufficient? If not then how can I learn more about it? – BumbleBee Oct 12 '16 at 08:11
  • @Abhineet, it's something like infinity? It's not there yet it's not false? – BumbleBee Oct 12 '16 at 08:12
  • @BumbleBee [Two's complement](https://en.wikipedia.org/wiki/Two%27s_complement). – Lundin Oct 12 '16 at 08:16
  • 1
    @KlasLindbäck There is no integer overflow in the example. This is pretty obvious, as the only modification is an implicit conversion. – 2501 Oct 12 '16 at 08:22
  • See my edits in solution – Abhineet Oct 12 '16 at 09:53
  • @2501 (checking...) You are correct. The behaviour is implementation defined, not undefined. I stand corrected. http://stackoverflow.com/a/18294422/646887 – Klas Lindbäck Oct 12 '16 at 11:49
  • I'm not sure now, I don't know what the actual problem is, undefined or implementation defined, (I don't know what latter means though I have a subtle idea of what former is.) – BumbleBee Oct 12 '16 at 12:32
  • @GurupadMamadapur Disagree with [overflow occured](http://stackoverflow.com/questions/39992128/range-of-values-in-c/40001084#comment67262392_39992128). There is no math to overflow. The issue is conversion as detailed in my answer. Of course OF and conversion are related concerns. The is no UB, just ID behavior. – chux - Reinstate Monica Oct 12 '16 at 14:31

2 Answers2

2

"Range of values in C" is a broad topic. Just talking about integer ranges, a 1 bit integer field may have a narrow range of 0 to 1 or (-1 to 0). intmax_t/uintmax_t deals with numbers in the 64-bit range or more. The range limitations and conversion amongst the many integer types in C is the tricky bit.


To focus on OP's example:
In the below, 128 is an integer constant with the value of 128 and type of int.

signed char variable1;
variable1 = 128;

signed char has a range is SCHAR_MIN to SCHAR_MAX. This is typically -128 to 127 and could be as small as -127 to 127. (See C11 §5.2.4.2.1 1)

The assignment needs to convert an int to signed char and the following applies per the assumed signed char range: (My emphasis)

When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged. C11dr §6.3.1.3 1

Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised. C11dr §6.3.1.3 3

A typical implementation defined result is a 2's complement wrap-around, so a value of -128 is common. OP's platform did something different and gave it the value of -127. It is implementation-defined behavior. @2501


Not sure that "The output of the compiler(using dev C++) was : -127" implies OP was correctly using a C compiler. OP should insure that.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    "See C11 §5.2.4.2.1 1", what is this? – BumbleBee Oct 13 '16 at 01:55
  • @chux, do you mean -128 is the result of two's complement? How can we use two's complement to get this? – BumbleBee Oct 16 '16 at 03:14
  • @chux, in the type of int entering a value of 2147438648 does not bring out the result -2147438648, I am not understanding this point – BumbleBee Oct 16 '16 at 03:42
  • @BumbleBee "mean -128 is the result of two's complement?" --> yes. _We_ do not use 2's complement to get this - it is the other way around. We use `SCHAR_MIN --> -128` to deduce the implementation has made that choice to use 8-bit 2's complement - a very common choice. – chux - Reinstate Monica Oct 16 '16 at 05:17
  • @chux, why isn't this choice made for the type int like I have pointed above? – BumbleBee Oct 16 '16 at 05:21
  • @BumbleBee "the type of int entering a value of 2147438648 does not bring out the result -2147438648" --> I do not see 2147438648 in your post or comments. Note that `2147438648` is outside the range, by 1, of a 32-bit `int`. What type do you think `2147438648` is? – chux - Reinstate Monica Oct 16 '16 at 05:22
  • @chux, it is definitely an integer, yes, but it is also a signed integer, which according to my 'beginner'(small) understanding would range to positive from 2147438647 and negetive to 4294877294, but there is an error(found experimentally) once 4294877294 is assigned to a variable. – BumbleBee Oct 16 '16 at 05:53
  • @chux, in dr. math's forum it has been pointed out that for signed char, value from 0 to 127 would be positive and beginning from 128 to 255 would be negetive, which would explain my question and the value of 2's complement agrees that 128= -128=(10000000)base2, but the same does not imply for (signed)ints? I'm really confused in this(for 2-3 stuck on the same page). – BumbleBee Oct 16 '16 at 05:55
  • @BumbleBee, "it is definitely an integer" does not clearly express a type like `char`, `int`, `short`, `long` or `long long`, etc. When `int` is 32-bit, `2147438648` is not type `int`. It is `long` or `long long`. A 32-bit `int` ( I did not say "integer") ranges from -(2³¹) to (2³¹ - 1). 2147438648 is outside that range. IAC, post/comment about the code that uses 2147438648 as it is missing. Other forum sites are best discussed there. "for signed char, value from ... 128 to 255 would be negative" is unclear. Post code that shows what you are commenting. – chux - Reinstate Monica Oct 16 '16 at 06:14
  • @chux, it is a long long int (2147438648) but in the same code in my question above why would not the code produce any errors or negate our value to -2147438647 if we replace "signed char" with "int" and the value of variable1 with 2147438648? – BumbleBee Oct 16 '16 at 08:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125841/discussion-between-chux-and-bumblebee). – chux - Reinstate Monica Oct 16 '16 at 15:16
  • @chux, how do we obtain the range of -3.403E38 on screen, doesn't seem that FLT_MIN does it. – BumbleBee Oct 25 '16 at 01:40
  • `printf("%e\n", -FLT_MAX);` Aside from _maybe_ 0.0, every FP value a negative counterpart. – chux - Reinstate Monica Oct 25 '16 at 01:47
0

What I think that, In ones' complement encoding (which is not very common nowadays, but in the olden days, it was), there were separate values for +0 and -0, and so the range for an 8-bit quantity is -127 to +127.

But in the processors that having two's complement representation (which is rather common), the minimum value will almost certainly be -128 in both languages C and C++. The reason why you only -127 is guaranteed, is because it allows a non two's complement representation to be supported.

That is because of the way two's complement encoding works: 0 is treated as a "positive" number (signed bit off), so, therefore, the number of available positive values is reduced by one.

You can find the range value that state into the char class as constants.That probably return the exact correct range that supported by the system.

Thanks

Bhavdip Sagar
  • 1,951
  • 15
  • 27