signed

Why 256 for a signed char is undefined in C++

一世执手 提交于 2020-01-20 07:09:47
问题 Reading the C++ Primer 5th edition book, I noticed that a signed char with a value of 256 is undefined. I decided to try that, and I saw that std::cout didn't work for that char variable. (Printed Nothing). But on C, the same thing signed char c = 256; would give a value 0 for the char c . I tried searching but didn't find anything. Can someone explain to me why is this the case in C++? Edit: I understand that 256 is 2 bytes, but why doesn't the same thing as in C, happen to C++? 回答1: Edit:

Why 256 for a signed char is undefined in C++

我们两清 提交于 2020-01-20 07:08:44
问题 Reading the C++ Primer 5th edition book, I noticed that a signed char with a value of 256 is undefined. I decided to try that, and I saw that std::cout didn't work for that char variable. (Printed Nothing). But on C, the same thing signed char c = 256; would give a value 0 for the char c . I tried searching but didn't find anything. Can someone explain to me why is this the case in C++? Edit: I understand that 256 is 2 bytes, but why doesn't the same thing as in C, happen to C++? 回答1: Edit:

C++ Converting unsigned to signed integer portability

£可爱£侵袭症+ 提交于 2020-01-17 01:21:27
问题 I know that in C the conversion of unsigned to signed integers is implementation defined, but what is it for C++? I figured someone would have asked this already, and I searched but I couldn't find it. I have a function that operates on an unsigned integer and returns a related unsigned integer. I am passing that function a signed integer by casting to unsigned similar to int num = -6; unsigned ret = func((unsigned)num); int ret_as_signed = (int)ret; . In Visual Studio that works fine, but I

How do I byte-swap a signed number in C?

自古美人都是妖i 提交于 2020-01-14 16:35:36
问题 I understand that casting from an unsigned type to a signed type of equal rank produces an implementation-defined value: C99 6.3.1.3: 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. This means I don't know how to byte-swap a signed number. For instance, suppose I am receiving two-byte, twos-complement signed values in little-endian order from a peripheral device, and

How do I byte-swap a signed number in C?

你。 提交于 2020-01-14 16:32:31
问题 I understand that casting from an unsigned type to a signed type of equal rank produces an implementation-defined value: C99 6.3.1.3: 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. This means I don't know how to byte-swap a signed number. For instance, suppose I am receiving two-byte, twos-complement signed values in little-endian order from a peripheral device, and

How do I byte-swap a signed number in C?

久未见 提交于 2020-01-14 16:32:06
问题 I understand that casting from an unsigned type to a signed type of equal rank produces an implementation-defined value: C99 6.3.1.3: 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. This means I don't know how to byte-swap a signed number. For instance, suppose I am receiving two-byte, twos-complement signed values in little-endian order from a peripheral device, and

Why C compiler cannot do signed/unsigned comparisons in an intuitive way [closed]

自作多情 提交于 2020-01-14 10:26:08
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . By "intuitive" I mean given int a = -1; unsigned int b = 3; expression (a < b) should evaluate to 1. There is a number of questions on

Conversion from char* to signed char*

爷,独闯天下 提交于 2020-01-13 08:35:49
问题 I saw a piece of valid C code I tried to compile as C++ and I got an error I can't understand. char* t; signed char* v = t; error: invalid conversion from char* to signed char* From what I learned, char and signed char are semantically identical, but are still considered as different by the compiler. I know that the error is caused by the difference between these two type, my question is: Why does this difference exists ? As far as I know char is implemented either as a signed char or as a

Conversion from char* to signed char*

早过忘川 提交于 2020-01-13 08:35:11
问题 I saw a piece of valid C code I tried to compile as C++ and I got an error I can't understand. char* t; signed char* v = t; error: invalid conversion from char* to signed char* From what I learned, char and signed char are semantically identical, but are still considered as different by the compiler. I know that the error is caused by the difference between these two type, my question is: Why does this difference exists ? As far as I know char is implemented either as a signed char or as a

What is the deal with assigning an unsigned variable to a signed value?

☆樱花仙子☆ 提交于 2020-01-11 14:12:28
问题 This code I am looking at has a lot of places where I see things like this happening: char *functionName(char *passedVariable) { unsigned char *newVariable = (char* ) passedVariable; Why is this being done? I always try to be consistent in the use of signed/unsigned, because I know that switching between the two can cause problems, but this developer doesn't seem to care. 回答1: Changing the pointer type is not really an issue, this address will still be valid. However interpreting the pointed