473,791 Members | 3,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4897
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**********@ma il.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.pow ernet.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******@addre ss.co.uk.invali d> wrote in message news:<c0******* ***@sparta.btin ternet.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

109
4197
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 understand the premise > > that a group might choose to entertain ONLY those questions that can be > > resolved purely by a reading or clarification of (drum roll please) The > > Standard. But how utterly boring, and what a waste of talent. It > >...
11
1413
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 say I didn't encounter any real surprises. Should I have bought a Schildt book instead? --D.
83
3115
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 reference to other C books to aid the programmer. 5. A listing of C related web sites. 6. For ease of use, an appendix of tables used in the text. 7. Make an appendix with the full ascii table. 8. Make an appendix that includes a C Programming FAQ.
72
4238
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 c?
16
4930
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 Microsoft must be installed on their servers. Now german Umlaute (ä, ü, ö) and quotes are returned incorrectly in SOAP fault responses. This can be easily verified: Implement the following in a web service method (just raises a SOAPException with a...
26
2120
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 released in 1988, it is now 2007, almost 20 years. Also, since it is so old, is there some supplemental tutorial recommend to go along with it? Thanks for your advice. Rajiv Battula
88
3802
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 invoking undefined behaviour triggered by overflow? Remember that the constants in limits.h cannot be used.
2
2705
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 -- AND NOW FOR A WORD (an IF blog):
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10201
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9023
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5424
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2910
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.