473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problem in K&R answer book

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
10 4864
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

109
by: MSG | last post by:
Michel Bardiaux <michel.bardiaux@peaktime.be> wrote in message news:<G4idnfgZ0ZfCWbrdRVn2jQ@giganews.com>... > Mark Shelor wrote: > > > > > OK, Sidney, I am considering it. I can certainly...
11
by: Daniel Haude | last post by:
....and I must say I don't regret it. But having lurked (and occasionally posted) for years in this group, having read and understood the C FAQ, and having perused the Standard quite a bit I must...
83
by: newby2c | last post by:
My personal K&R (3rd edition) wish list: 1. That a 3rd edition is actually published! 2. Make any and all corrections from earlier editions. 3. Update to comply with the c99 Standard. 4. A...
72
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
26
by: rajiv.battula | last post by:
Hey everyone, I am a Java programmer, somewhere between novice to advanced. I wanted to know if it is still recommended to read "The C Programming Language" 2nd Edition? This edition was...
88
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.