Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 19-02-2009, 01:19 PM   #1 (permalink)
In The Zone
 
ruturaj3's Avatar
 
Join Date: Feb 2007
Location: Mumbai
Posts: 214
Default Integer range in C


HI,

In C int is by default, signed short int so it uses 2 bytes(I m talking abt 16 bit compiler). and its range is -32768 to 32767.

And char uses 1 byte.

char ch = 1300, printf("%d",ch); prints 20.
coz, it takes only 8 bits.

1300 = 0101 0001 0100 in binary.

so when storing 1300 in char, it takes only 8 bits ie 0001 0100 wich is +20 (+ coz its signed bit is 0) in decimal.


So int a = 32767, printf("%d",a); prints output correctly ie 32767.

32772 = 1000 0000 0000 0100 in binary.
so if i tak 16 bits and since signed bit is 1 so - sign. so it should print - 4 na.

but int a = 32772, printf("%d",a); prints -32764 why ??? I m not getting tis point only.
ruturaj3 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 19-02-2009, 02:31 PM   #2 (permalink)
Canon 1000D
 
astroutkarsh's Avatar
 
Join Date: Apr 2007
Location: Pune
Posts: 80
Default Re: Integer range in C

Its Range Problem.
You have added 5 in 32767 for 32772.
As Range Limit 32768 ==> -32768, 32769 ==> -32767..... 32772 ==> -32764.

At Binary Level try Addition of Two Binary Numbers with 16 bit Limitation.
Binary of 32767 + Binary of 5 (111111111111111 + 101)
__________________
Canon 1000D + 18-55 + 50mm f/1.8 + 55-250 IS :-)
astroutkarsh is offline  
Old 19-02-2009, 06:16 PM   #3 (permalink)
Apprentice
 
Join Date: Feb 2007
Posts: 98
Default Re: Integer range in C

you have crossed the bit level possible representation in binary mode, form here on each compiler reacts differently..........and if you are talking with respect to turbo c++....the above explanation is true.
Jayanth.M.P is offline  
Old 24-02-2009, 09:01 PM   #4 (permalink)
Right Off the Assembly Line
 
Join Date: Dec 2006
Posts: 9
Default Re: Integer range in C

32772 = 1000 0000 0000 0100

Since negative numbers are stored in 2's complement form

Therefore, 1's complement of 1000 0000 0000 0100 = 0111 1111 1111 1011
2's complement of 1000 0000 0000 0100 = 0111 1111 1111 1100

which is 32764 in decimal.

Since sign bit is set, therefore answer is -32764

And yes, since integer size in C is platform dependent, so answer may be different on different platforms. The answer is same for all 16-bit compilers (not limited to only Turbo C++ 3.0)
grvpuri is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need HTPC configuration ! (Low range / Mid range) blackbird PC Components / Configurations 19 02-04-2009 04:36 PM
Best mobile in the range of 10k to 15K sagarkumar Buying Advice 38 12-02-2009 05:04 PM
Mid Range Motherboard (Under 7k) static_x Hardware Q&A 8 21-11-2008 09:10 PM
Python: How to check integer ? The Unknown Programming 3 27-01-2008 04:29 PM
Best phone in the range 12k khin007 Mobiles and Tablets 30 05-12-2006 01:58 AM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:07 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2