Question:
Computers - C-language question?
2012-06-24 03:45:25 UTC
In a C-language program, suppose that ch is declared to be an 8-bit signed char. If ch = 127, then the value of ch++ will be:

a) 128
b) 0
c) -128
d) -127
e) Nan

Could someone please explain this to me. Cheers.
Seven answers:
2012-06-24 03:52:50 UTC
I assume that "ch"'s type is "signed char", which has a minimum guaranteed range of -127 - +127. "ch++" will yield a value of 127. "ch + 1" will yield a value of 128, though its type will be int. The value of "ch++, ch" is undefined.



edit:

@Wertle Woo: Both you and peteams are wrong. It's strange how some people have difficulty accepting that certain aspects of C are undefined. C was designed to be a portable language. Let's go through the assumptions that peteams has made.



1) He's assuming "signed char" has a range of -128 - +127. It may also be -127 - +127 or anything larger.



2) Pretend we accept his assumption that "signed char" has the range listed above. His next assumption is that "signed char"'s internal representation uses two's complement.



Both of these assumptions may be false on any given standard C implementation. People need to stop assuming that the output you get with your compiler will be the same from implementation to implementation. Signed integer overflows have undefined behaviour. Read the C11 standard and you might learn something.



edit: I should point out that both of those assumptions I've listed fairly safe to make for practical purposes; most modern implementations use 8-bit chars with two's complement for the signed representation. The point is that C doesn't support either of those assumptions and since it's a C question it's an invalid answer. It has undefined behaviour, so leave it at that.



Keep in mind that it's possible to store -127 - +128 in a signed 8-bit char, so a) is also a possible answer as far as C is concerned. The fact is, it's a stupid question. Write "f) Undefined" on your homework and circle it.



edit: But then again, keep in mind that it asks for the value of "ch++" which is obviously +127. This question is stupid on multiple levels. In summary:



signed char ch = 127;



Value of "ch++" = 127.

Value of "ch++, ch" or "++ch" = Undefined.



So depending on how your question is actually worded, one of the above two answers.



edit: Consider the following question.



What colour are oranges?

a) Green.

b) Blue.

c) Purple.



The answers provided are all wrong. Figure out what the right answer is and explain why it's right. It's better to correct a wrong answer before more people start believing it's a right one, than it is to allow it to continue being wrong. By choosing the latter, you'll only make it harder for people who will try to correct this mistake in the future.



@Jonathan:

> Note also that ch++ is the same as ch+=1

Not quite.

t = ch++;

t = ch += 1;

Different values will be stored in t in each scenario. "ch++" yields ch, then stores ch + 1 in ch as a side effect. "ch += 1" yields ch + 1 and stores that value in ch. Since the question specifically asks for the value of "ch++" (rather than the value of ch after "ch++"), it ought to be +127.
peteams
2012-06-24 04:10:33 UTC
c, negative 128



Assuming a 2's compliment representation of numbers then and 8-bit integer, which is what a signed char will be, can represent -128 to +127. Incrementing +127 yields -128 and decrementing -128 yields +127.



2's complement works by changing the sign of the unit weight of the most significant digit position. Normally the weights would be 1, 2, 4,...32, 64, 128; for 2's complement the most significant weight becomes -128.
Jonathan
2012-06-25 00:50:09 UTC
It is a good thing your problem specified "signed char" because it is implementation-defined whether or not a char is signed or unsigned. In most implementations you will encounter (c) as the answer because they are two's complement machines, without padding bits. It is, however, implementation defined whether or not sign and magnitude, two's complement, or one's complement is used. And it is possible for the case indicated here to be a special "trap representation" in the standard (sign bit 1 and all other bits zero) for the sign and magnitude or two's complement cases. See section 6.2.6.2 of the C99 standard, for example. Note also that ch++ is the same as ch+=1, in that 'ch' is evaluated only once not twice as it would be in ch=ch+1.



The key thing to note here is that the result of an unsigned arithmetic operation is always defined, but the result of a signed operation may be undefined and in fact is either implementation-defined or else an implementation-defined signal is raised if the value cannot be represented in the type. In this case, the value of 128 (the result of 127+1) cannot be represented in 'ch' so the standard leaves it as an implementation detail as to what happens. Normally, the code generated does the obvious and carries on. But it could do anything it wanted, including randomizing the value using a radioactive source as a generator, if it wanted to do so and it would still be compliant.



You can't rely upon the behavior as a portable one. But for a given compiler and architecture it targets, you probably could rely on what you discover about it to remain in future editions of that same compiler with the same target.



To give another example, sizeof(signed char) == 1 and this is portable. But "1" here means "one byte" and a byte is NOT portable. A byte must be addressable and it must be large enough to hold any of the basic character set used at run-time. But the number of bits is implementation defined. Suppose you are using unicode (the 16-bit variety of it) as the basic character set used at run-time? Then 'signed char' would have to be large enough to hold any of the unicode characters. Which is a lot larger than 8 bits in size. (Most implementations would instead define the unicode character set as a "wide character". But this doesn't mean they have to do so.) Suppose this were on an old PDP-10, from DEC? That is a 36 bit machine and it would store (5) 7-bit characters in its addressable word. Either a C compiler would have to waste the entire 36 bits for a single "signed char" (in keeping with addressability) or else it would have to fabricate a pseudo-addressing scheme that allowed addressable access to any of the five 7-bit characters within a 36-bit word. The implementation I saw used the latter method and continued packing characters. But here, a "byte" would now mean 7 bits. Not 8. And the range would be correspondingly less.



In common practice with C, though, adding 1 to 1111111 produces 10000000 (binary.) Since that fits in 8 bits and most practical C compilers use 'signed char' of 8 bits in size, that is what gets stored back. But since the upper most bit is set and since in signed values that is interpreted as a negative value, the answer is usually going to be -128, or (c).
jessica
2016-07-22 06:51:29 UTC
Earlier than u be trained C++, u will have to be aware of the C lang. If u recognize C lang, then learning any comp lang will turns into very handy. B`cos C is the mummy language of computers. Headquartered on C many other lang r developed. So u learning from C lang & then go 4 different lang.
Wertle Woo
2012-06-24 07:03:44 UTC
Disregard everyone except peteams. He is right. Everyone else is a moron who has no business answering a question they don't know the answer to. The answer is -128.



EDIT: Okay, listen to others if you want to get the question wrong. Not my problem.
Gardner
2012-06-24 03:48:30 UTC
ch++ means add 1 then evaluate the value of the variable. So in this case it would be a, 128.
Parikshit Singh
2012-06-24 03:50:19 UTC
you can try search on google

or go to Wikipedia site

i think it may help you

sorry for this answer i cannot explain this to you.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...