return types in C language

why does both int and float depends on the machine you are using

Page 1 of 1

4 Replies - 2893 Views - Last Post: 01 September 2009 - 01:15 PM Rate Topic: -----

#1 hd_pulse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 09

return types in C language

Posted 31 August 2009 - 11:27 AM

why does both int and float depends on the machine you are using????
;16 bit ints, which lie between -32768 to +32767 are common as are 32 bits ints.A float is typically a 32 bit quantity with atleast six significant digits.
Is This A Good Question/Topic? 0
  • +

Replies To: return types in C language

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: return types in C language

Posted 31 August 2009 - 12:09 PM

The standard does not define the size of any of its data types (I think that it does mention minimums for some). It is not so much the machine you are using but the compiler settings you are using. So the compiler does tend to generate code for the current platform you are on, but usually there are ways to get it to compile for other platforms.

The compiler determines the size of the datatypes and each tends to do things in a little different way.

The original idea was that int would be the word size of the computer you are on (the register size) -- but that idea has gotten a little muddled.

In general:

int -- 16, 32, 64 bytes
short -- 16
long -- 32
long long -- 64 (not all compilers see this as a valid type).

float -- 32bits (pretty standard)
double -- 64 bits (pretty standard)
long double -- 64 - 128 bits (not terribly standard)

For example for this little program:
#include <stdio.h>

#define GetSize(type) printf("size of " #type ": %d bits\n", sizeof(type)*8)

int main() {
	GetSize(int);
	GetSize(short);
	GetSize(long);
	GetSize(long long);
	GetSize(float);
	GetSize(double);
	GetSize(long double);
	return 0;
}


On this laptop with Borland 5.5 compiler I get these values:

size of int: 32 bits
size of short: 16 bits
size of long: 32 bits
size of long long: 64 bits
size of float: 32 bits
size of double: 64 bits
size of long double: 80 bits

Same computer but with MingGW I get:

size of int: 32 bits
size of short: 16 bits
size of long: 32 bits
size of long long: 64 bits
size of float: 32 bits
size of double: 64 bits
size of long double: 96 bits


So one can't always depend upon the sizes of the data types. Some compiler have headers that define specific sizes like "int16, int32, int64" etc.
Was This Post Helpful? 0
  • +
  • -

#3 hd_pulse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 09

Re: return types in C language

Posted 01 September 2009 - 08:18 AM

View PostNickDMax, on 31 Aug, 2009 - 11:09 AM, said:

It is not so much the machine you are using but the compiler settings you are using.



"Different CPUs support different integral data types. Typically, hardware will support both signed and unsigned types but only a small, fixed set of widths."

as quoted from wikipaedia.
If you agree to the above quote please post the reason for this????


regards
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: return types in C language

Posted 01 September 2009 - 12:44 PM

what is there not to agree with -- sounds true enough.

An PC running an x86 style chip has a set of registers that the CPU uses to preform operations. These operations include moving to/from memory, Boolean operations, arithmetic operations, etc..

My computer has a 64bit processor -- so its register size is 64bits -- however I can access just half of the register giving me a 32bit register, which again can be halved giving me a 16bit register, each 16bit register is split into a high byte and low byte so I have 8bit registers as well.

rax -> eax -> ax -> ah, al

The CPU can treat any of these as signed or unsigned integers.

The original idea in C was that the integer type would be the "word size" -- i.e. the register size of the CPU. So on my computer an int would have been 64bits -- but this idea did not stick (mostly for practical purposes -- 64bit integers are really big) So the typical model is to use 32bit ints rather than 64bit -- BUT that model is not guaranteed to be the default nor to hold for all platforms.

BTW on this computer (different from the one I used the other day)

Visual C++ x64
size of int: 32 bits
size of short: 16 bits
size of long: 32 bits
size of long long: 64 bits
size of float: 32 bits
size of double: 64 bits
size of long double: 64 bits

So it would seem that the widest variation is in the "long double" type and the integers are pretty standard.
Was This Post Helpful? 0
  • +
  • -

#5 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: return types in C language

Posted 01 September 2009 - 01:15 PM

the C standard specifies minimum sizes for numeric types, e.g.
short int - signed integer minimum range -32768 to 32767
unsigned short int - unsigned integer minimum range 0 to 65535
int signed integer minimum range -32768 to 32767
unsigned int unsigned integer minimum range 0 to 65535
long int signed integer minimum range -2147483648 to 2147483647
unsigned long int - unsigned integer minimum range 0 to 4294967295

depending upon machine architecture and compiler these can vary so long as the minium size is satisfied, e.g. an int can be 16bits, 32 bits, 64 bits, etc. When one meets a new system the first thing is to use the standard header file <limits.h> to check the storage allocated to the various types. If transfering binary data between systems (e.g. using files or TCP or UDP packets) this becomes crtical in that one may have to do some conversion. In addition machines can be little endian (least significant byte first) or big endian (most significant byte first) so one may have to do some byte swapping to get data in the correct format. Some compilers allow you to specify the endianness of the generated code to assit with porting software. There are similar problems with floating point numbers.

This post has been edited by horace: 01 September 2009 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1