The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
C programming
Discuss C programming in the C Programming forum on Dev Shed. C programming C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 19th, 2012, 01:32 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 1
Time spent in forums: 12 m 54 sec
Reputation Power: 0
|
|
C programming
i have seen that size of integer (int) in windows is 2 bytes but 4 in linux terminal....
can any one give reason for why that happens.....
and why size of float and char remain same in both os...
rply fast...
|

September 19th, 2012, 04:41 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
I don't imagine you Googled, did you? Windows started as a 16 bit application on top of 16 bit DOS, hence a 'word' was 2 bytes. It then got 'promoted' to 32 bit, so a 'word' now was 4 bytes, but there was a whole lot of legacy code that required support, so the value of 'int' has drifted around during the interval. Linux has always been (at least) 32 bit, so a wee bit less confusion, though the transition to 64 bit has introduced some issues. If you access the macros in limits.h you can find out exactly what size each data type is.
|

September 19th, 2012, 04:51 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Quote: | Originally Posted by vikas.boundless i have seen that size of integer (int) in windows is 2 bytes but 4 in linux terminal....
can any one give reason for why that happens.....
and why size of float and char remain same in both os...
rply fast... |
Lemme guess, you're using Turbo-C (an ancient DOS compiler) on a windows machine (only compiler I know that still uses 16-bit ints). If you use a more modern C compiler (such as Visual C++ or gcc or whatever) on the very same machine, you'll see that size of integer is 4.
The size of integer is compiler-dependent per the C standards. There is a lower limit on how many minimum bits are needed for an int (standard says 16 bits minimum), but it doesn't say what the maximum is. So it can be 32 bits, 64 bits, 80 bits, 128 bits or whatever, it is left up to the compiler vendor to decide what it should be.
Also, by C standards, sizeof(char) = 1 always, immaterial of whether a char is 8-bit, 16-bit or whatever.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

September 19th, 2012, 05:09 AM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 62
  
Time spent in forums: 1 Day 1 h 9 m 54 sec
Reputation Power: 2
|
|
Quote: | Originally Posted by mitakeet I don't imagine you Googled, did you? Windows started as a 16 bit application on top of 16 bit DOS, hence a 'word' was 2 bytes. It then got 'promoted' to 32 bit, so a 'word' now was 4 bytes, but there was a whole lot of legacy code that required support, so the value of 'int' has drifted around during the interval. Linux has always been (at least) 32 bit, so a wee bit less confusion, though the transition to 64 bit has introduced some issues. If you access the macros in limits.h you can find out exactly what size each data type is. |
Linux did indeed begin life on the Sinclair QL, which, according to Sir Clive Sinclair, was a 32-bit 'serious' computer. Although it only had an 8-bit data bus, it was internally 32-bit. Some of the upgrades made it pack a punch though, such as the Super Gold Card.
Regards,
Shaun.
|

September 20th, 2012, 04:27 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by mitakeet If you access the macros in limits.h you can find out exactly what size each data type is. | Or just use sizeof(<datatype>).
|

September 20th, 2012, 04:31 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by Scorpions4ever The size of integer is compiler-dependent per the C standards. | It is, but also for the sake of inter-operability, most compilers comply with a specific ABI which is architecture and/or OS dependent.
|

September 20th, 2012, 04:45 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by vikas.boundless and why size of float and char remain same in both os... |
The size of float and double are determined by the representation and precision required. Most commonly IEEE 754 is used - not least because that is the representation used by most hardware floating point units including that on the x86.
The size of a char is intended to be the size of a character in the execution environment, bit in practice is the size of the smallest separately addressable memory location, which on most processors is 8 bits. The official definition of char would cause problems in Unicode execution environments where depending on the encoding a character could be variable in width. Even in environments such as Windows where the Unicode encoding is 16bit, making a char that size would make 8bit data unaddressable without introducing a new data type name. C# overcomes this by having a 16 bit char type and an 8 bit byte type.
Quote: | Originally Posted by vikas.boundless rply fast... | A bit pushy. You are lucky that such rudeness has been mostly ignored. Often that would result in getting no useful answer at all! Not every one will be in the same time-zone as you; 24 hours is reasonable, and no one has to answer your question.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|