problem in K&R answer book

P

Prem Mallappa

Hi everyone
this is the program given in answer book to K&R

====================================
#include <stdio.h>

int main()
{
printf (" signed char = %d", -(char) ((unsigned char) ~0 >> 1));
printf (" signed char = %d", (char) ((unsigned char) ~0 >> 1));
return 0;
}

==================================


This is to print the maximum value we can store in a signed char..
the output of this is
-127
127
but we can store -128 as the value in a signed char and also in <limits.h>

# define SCHAR_MIN (-128)

why is it given in that way in the most famous K&R answerbook
am i wrong or the program is wrong..
plz help me
regards
prem
 
P

pete

Prem said:
Hi everyone
this is the program given in answer book to K&R

====================================
#include <stdio.h>

int main()
{
printf (" signed char = %d", -(char) ((unsigned char) ~0 >> 1));
printf (" signed char = %d", (char) ((unsigned char) ~0 >> 1));
return 0;
}

==================================

This is to print the maximum value we can store in a signed char..
the output of this is
-127
127
but we can store -128 as the value in a signed char and also in <limits.h>

# define SCHAR_MIN (-128)

why is it given in that way in the most famous K&R answerbook
am i wrong or the program is wrong..
plz help me

-127 is an allowable value for SCHAR_MIN.
I don't know which K&R problem you are refering to.
 
P

Prem Mallappa

pete said:
-127 is an allowable value for SCHAR_MIN.
I don't know which K&R problem you are refering to.


Excercise 2-1 of C programming Language : by Kernighan and Ritchie...
-- Write a program to determine the ranges of Char, short, int.. both signed
and unsigned, by printing appropriate values from standard headers and by
direct computation..
 
M

Martin Ambuhl

Prem said:
Hi everyone
this is the program given in answer book to K&R

====================================
#include <stdio.h>

int main()
{
printf (" signed char = %d", -(char) ((unsigned char) ~0 >> 1));
printf (" signed char = %d", (char) ((unsigned char) ~0 >> 1));
return 0;
}

==================================


This is to print the maximum value we can store in a signed char..
the output of this is
-127
127
but we can store -128 as the value in a signed char and also in <limits.h>

# define SCHAR_MIN (-128)

why is it given in that way in the most famous K&R answerbook
am i wrong or the program is wrong..

(1) It's right for some machines; wrong for others
(2) It is wrong in assuming that (char) is the same as (signed char)
(3) It is wrong in not terminating the last line of output with a
line-termination character ('\n').

Try this:
#include <stdio.h>
#include <limits.h>

int main()
{
printf(" %d\n",
(signed char) ((unsigned char) 1 << (CHAR_BIT - 1)));
printf(" signed char = %d",
-(signed char) ((unsigned char) ~0 >> 1));
printf(" signed char = %d\n",
(signed char) ((unsigned char) ~0 >> 1));
return 0;
}
 
P

pete

Prem said:
Excercise 2-1 of C programming Language : by Kernighan and Ritchie...
-- Write a program to determine the ranges of Char, short, int..
both signed and unsigned,
by printing appropriate values from standard headers and by
direct computation..

unsigned limits are always ((unsigned type)-1)

UCHAR_MAX is ((unsigned char)-1)
ULONG_MAX is ((long unsigned)-1)

There are no portable ways to get the signed limits,
without LIMITS.H
 
J

Joe Wright

Prem said:
Hi everyone
this is the program given in answer book to K&R

====================================
#include <stdio.h>

int main()
{
printf (" signed char = %d", -(char) ((unsigned char) ~0 >> 1));
printf (" signed char = %d", (char) ((unsigned char) ~0 >> 1));
return 0;
}

==================================

This is to print the maximum value we can store in a signed char..
the output of this is
-127
127
but we can store -128 as the value in a signed char and also in <limits.h>

# define SCHAR_MIN (-128)

why is it given in that way in the most famous K&R answerbook
am i wrong or the program is wrong..
plz help me
regards
prem

The answer book is not K&R and not the Standard. Anybody can make a
mistake. The above program seems contrived. Consider that unsigned char
has no sign bit, ~0 is all ones and that '>> 1' shifts in zeros on the
left. If we consider the case CHAR_BIT 8 then '(unsigned char)~0' yields
11111111 (255) and '>> 1' yields '01111111'. This value is 127. The
contrived example above then casts (converts) the value to char
(presumably signed) and in the second case, negates it with '-'. Two's
complement negation is *one's complement plus one* so it is now
'10000001' with value -127. If we subtract 1 from this we get '10000000'
which is obviously -128.
 
O

Oleg Melnikov

Prem Mallappa said:
Hi everyone
this is the program given in answer book to K&R

====================================
#include <stdio.h>

int main()
{
printf (" signed char = %d", -(char) ((unsigned char) ~0 >> 1));
printf (" signed char = %d", (char) ((unsigned char) ~0 >> 1));
return 0;
}

==================================


This is to print the maximum value we can store in a signed char..
the output of this is
-127
127
but we can store -128 as the value in a signed char and also in <limits.h>

# define SCHAR_MIN (-128)

why is it given in that way in the most famous K&R answerbook
am i wrong or the program is wrong..
plz help me
regards
prem
Perhaps, this prog makes you quite sure
#include <stdio.h>
void main(void)
{
signed char n= (signed char)128;
int i= (int)n;
printf("%d", i);
}
'minchar' -128 does really exist.
It's just binary 10...0 with only sign bit setted.
It's maxsignedchar+1 - to negative. '255' is '-1'.
We could take it from -a+a=0 with overflow. Then it's obvious
that minsignedchar=-128 - for plus required another 10...0 =128 in
unsigned char. That is idea for new algorithm. I'll be waiting for
your prog (e-mail). Long long time ago I fixed bug of Landau.
Thank you for the question - it's of great educat importance.
Sorry for my bad English. Melnikov Oleg, (e-mail address removed)
 
P

pete

Oleg said:
Perhaps, this prog makes you quite sure

I don't have the stamina to thoroughly critique this program.
#include <stdio.h>
void main(void)
{
signed char n= (signed char)128;
int i= (int)n;
printf("%d", i);
}
'minchar' -128 does really exist.
It's just binary 10...0 with only sign bit setted.
It's maxsignedchar+1 - to negative. '255' is '-1'.
We could take it from -a+a=0 with overflow. Then it's obvious
that minsignedchar=-128 - for plus required another 10...0 =128 in
unsigned char.

Some machines have SCHAR_MIN -127
 
R

Richard Heathfield

Oleg said:
Perhaps, this prog makes you quite sure
#include <stdio.h>
void main(void)

int main(void)
{
signed char n= (signed char)128;

Since SCHAR_MAX needn't exceed 127, assigning 128 can introduce overflow,
and thus undefined behaviour.
 
P

Peter Nilsson

Richard Heathfield said:
int main(void)


Since SCHAR_MAX needn't exceed 127, assigning 128 can introduce overflow,
and thus undefined behaviour.

Integer conversion is specifically implementation defined in C90 when
the value cannot be represented in the target type. Only C99 allows
the potential for an implementation defined signal to be raised if the
value cannot be represented.

[I have no idea why C99 added this 'feature'.]
 
D

Dan Pop

In said:
Integer conversion is specifically implementation defined in C90 when
the value cannot be represented in the target type. Only C99 allows
the potential for an implementation defined signal to be raised if the
value cannot be represented.

[I have no idea why C99 added this 'feature'.]

To fix something that wasn't broken in the first place. And to break
perfectly correct and portable C90 code that relied on this feature.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Fibonacci 0
K&R 1-24 15
K&R p.97 8
K$R xrcise 1-13 (histogram) 4
1-17 in K&R book 9
clc-wiki answer to K+R exercise 2-7 6
How to build a char[][] in k&r dialect. 7
Questions about K&R (Kernighan and Ritchi) 57

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top