By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,534 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

problem in K&R answer book

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Prem Mallappa wrote:

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.

--
pete
Nov 14 '05 #2

P: n/a
pete wrote:
Prem Mallappa wrote:

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.

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..

Nov 14 '05 #3

P: n/a
Prem Mallappa wrote:
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;
}


--
Martin Ambuhl
Nov 14 '05 #4

P: n/a
Prem Mallappa wrote:

pete wrote:
Prem Mallappa wrote:

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.


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

--
pete
Nov 14 '05 #5

P: n/a
Prem Mallappa wrote:

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.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #6

P: n/a
Prem Mallappa <pr***********@hotpop.com> wrote in message news:<c0*************@ID-203908.news.uni-berlin.de>...
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, ki**********@mail.ru
Nov 14 '05 #7

P: n/a
Oleg Melnikov wrote:

Prem Mallappa <pr***********@hotpop.com> wrote in message news:<c0*************@ID-203908.news.uni-berlin.de>...
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


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

--
pete
Nov 14 '05 #8

P: n/a
Oleg Melnikov wrote:
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.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #9

P: n/a
Richard Heathfield <do******@address.co.uk.invalid> wrote in message news:<c0**********@sparta.btinternet.com>...
Oleg Melnikov wrote:
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.


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'.]

--
Peter
Nov 14 '05 #10

P: n/a
In <63**************************@posting.google.com > ai***@acay.com.au (Peter Nilsson) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.