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

20
  • 3
    The value returned is -127 because overflow occured.Welcome to stackOverflow :) Commented Oct 12, 2016 at 6:51
  • 1
    I think the output would be the -128 Commented Oct 12, 2016 at 6:55
  • 1
    ideone.com/RBGM3v Commented Oct 12, 2016 at 7:08
  • 2
    Overflow in signed integer types results in undefined behaviour. See for example stackoverflow.com/questions/3948479/… Commented Oct 12, 2016 at 7: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. Commented Oct 12, 2016 at 7:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

13 Comments

"See C11 §5.2.4.2.1 1", what is this?
@chux, do you mean -128 is the result of two's complement? How can we use two's complement to get this?
@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 "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, why isn't this choice made for the type int like I have pointed above?
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.