GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Mar-2005, 21:22
karthikeyansen karthikeyansen is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Location: INDIA
Posts: 22
karthikeyansen is on a distinguished road
Lightbulb

Data types and Hardware dependencies


Hi,

How do one determine the size of integer variable. Although there is a thumb rule that 32 bit processor allocates integer variable as 4 bytes (Similarly 16 bit processor, size of int is 2 bytes). But how exactly is the calculation arrived at in terms of the processor and the data type? Then why is it benchmarked only on integer variable like 4 bytes and referenced for other data types from integer.

Thanks in advance,
--Karthikeyan
  #2  
Old 04-Mar-2005, 08:59
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by karthikeyansen
Hi,

How do one determine the size of integer variable. Although there is a thumb rule that 32 bit processor allocates integer variable as 4 bytes (Similarly 16 bit processor, size of int is 2 bytes). But how exactly is the calculation arrived at in terms of the processor and the data type? Then why is it benchmarked only on integer variable like 4 bytes and referenced for other data types from integer.

Thanks in advance,
--Karthikeyan

There are four integer data types (eight if you count signed and unsigned as separate types):

char
short int
int
long int

According to the C standard:
Quote:
An object declared as type char is large enough to store any member of the basic execution character set.

An int object has the natural size suggested by the architecture of the execution environment.

The standard also requires the following:

sizeof(char) is less than or equal to sizeof(short int)
sizeof(short int) is less than or equal to sizeof(int)
sizeof(int) is less than or equal to sizeof(long int)


As has been mentioned, "old" C compilers (like Turbo C for DOS) typically had sizeof(int) equal to 2. There are people still running Turbo C 2.0 on Windows XP.

For my AthlonXP processor on Windows XP, I find that:

sizeof(char) = 1
sizeof(short) = 2
sizeof(int) = 4
sizeof(long) = 4

For an Athlon64 processor with 64-bit Linux you might expect that ints would be eight bytes, but I find that.

sizeof(char) = 1
sizeof(short) = 4
sizeof(int) = 4
sizeof(long) = 8

Note that compilers can decide which size to use for an int, so merely knowing the hardware doesn't tell you what the size of an int is. The bottom line is that as a programmer, you should never, ever make an assumption as to the size of any particular object (except in cases where you have to). That is, keep system dependencies and implementation dependencies out of your programs as much as possible (and be sure to document any dependencies so that if you or anyone else compiles the program on a different system the danger points will be known).

Of course you can find out what the sizes are on your system by spmething like this:
CPP / C++ / C Code:
#include <stdio.h>

int main()
{

  printf("sizeof(char)   = %d\n", sizeof(char));
  printf("sizeof(short)  = %d\n", sizeof(short));
  printf("sizeof(int)    = %d\n", sizeof(int));
  printf("sizeof(long)   = %d\n", sizeof(long));
  printf("sizeof(float)  = %d\n", sizeof(float));
  printf("sizeof(double) = %d\n", sizeof(double));

  return 0;
}


Regards,

Dave
  #3  
Old 03-Apr-2005, 21:58
karthikeyansen karthikeyansen is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Location: INDIA
Posts: 22
karthikeyansen is on a distinguished road
Thumbs up

Quote:
Originally Posted by davekw7x
There are four integer data types (eight if you count signed and unsigned as separate types):

char
short int
int
long int

According to the C standard:


The standard also requires the following:

sizeof(char) is less than or equal to sizeof(short int)
sizeof(short int) is less than or equal to sizeof(int)
sizeof(int) is less than or equal to sizeof(long int)


As has been mentioned, "old" C compilers (like Turbo C for DOS) typically had sizeof(int) equal to 2. There are people still running Turbo C 2.0 on Windows XP.

For my AthlonXP processor on Windows XP, I find that:

sizeof(char) = 1
sizeof(short) = 2
sizeof(int) = 4
sizeof(long) = 4

For an Athlon64 processor with 64-bit Linux you might expect that ints would be eight bytes, but I find that.

sizeof(char) = 1
sizeof(short) = 4
sizeof(int) = 4
sizeof(long) = 8

Note that compilers can decide which size to use for an int, so merely knowing the hardware doesn't tell you what the size of an int is. The bottom line is that as a programmer, you should never, ever make an assumption as to the size of any particular object (except in cases where you have to). That is, keep system dependencies and implementation dependencies out of your programs as much as possible (and be sure to document any dependencies so that if you or anyone else compiles the program on a different system the danger points will be known).

Of course you can find out what the sizes are on your system by spmething like this:
CPP / C++ / C Code:
#include <stdio.h>

int main()
{

  printf("sizeof(char)   = %d\n", sizeof(char));
  printf("sizeof(short)  = %d\n", sizeof(short));
  printf("sizeof(int)    = %d\n", sizeof(int));
  printf("sizeof(long)   = %d\n", sizeof(long));
  printf("sizeof(float)  = %d\n", sizeof(float));
  printf("sizeof(double) = %d\n", sizeof(double));

  return 0;
}


Regards,

Dave

Thanks for your reply,Dave. Your reply was really eye opener.

Knowledge is power !!!!

Karthikeyan.India
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:00.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.