0

I want to know that can an signed integer accept higher value than its maximum value either negative or positive.In pset3 I am giving significantly large value as an argument to argv[1], that's okay it is a string it can accept any length of string but can 32 bit integer like needle & haystack[i] store that value & produce the output.

1

No, an int variable, whether it's signed or unsigned, cannot store values that are out-of-range and trying to assign a value that is out-of-range to an int variable would typically result in a warning unless you have an option like -Werror passed in your compilation command in which case it would result in an error.

Assigning an out-of-range value to an int variable causes the value to wrap around. For example, if the minimum value that an int could store was -10 and the maximum value that it could store was 9 and you tried to store a value like 13 in it, it would become -7 (this also applies for trying to store a value that is lower the minimum limit). This is resulted in from the fact that an int has a fixed size. If an int was 4 bits long and the value was 6 bits for example, typically the left-most two bits of the value would be ignored.

And a string still cannot accept a value of any length. You are still limited by the amount of available memory on your computer as well as the size of your string (which is not specified by you in case the strings in argv are involved).

10
  • please elaborate the last line, If an int was 4 bits long and the value was 6 bits for example, typically the left-most two bits of the value would be ignored. give an example in this context
    – user299731
    Jan 14 '15 at 12:22
  • @user299731 if we have, for example, an unsigned int variable and it is 4 bits long, this means that the maximum value that this int variable can store is 1111(bin) or 15(dec). If you tried to store a value that is more than 4 bits long, only the first 4 bits will be stored and the rest will be ignored. For example, 17(dec) is 10001(bin) and that's obviously 5 bits long. In this case, the left-most bit will be ignored and only the first 4 will be stored which are 0001(bin) or 1(dec).
    – kzidane
    Jan 14 '15 at 12:35
  • so it'll store the values as 1 as your explaination. this is convincing but what will happen with signed integers
    – user299731
    Jan 14 '15 at 12:38
  • @user299731 signed integers have the left most bit as a sign bit. It is used to determine whether the value is negative (in case it's 1) or non-negative (in case it's 0). The same sense will apply except that the maximum value an int would be able to store in this case would be 0111(bin) or 7(dec) and the least value would be 1111 or -8(dec), so trying to store a value like 10(dec) or 1010(bin) would result in storing the value -6 instead. You may read about two's complement for more info about how we would get -6 here!
    – kzidane
    Jan 14 '15 at 12:44
  • what will happen if we want to store 26 in signed 4-bit integer, since it has one extra bit. btw i know 2's complement.
    – user299731
    Jan 14 '15 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .