473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

size of an int

8
i am newbie in c ,i want to know on what parameters the size of int or char or float is decided , i mean does it depend on compiler only or "compiler and OS" or combination of all above and processor or RAM we are using

like...If we are using a 32 bit processor and OS is 16 bit , then will the size of int be 16 bits ?
Dec 7 '08 #1
20 2737
Banfa
9,065 Expert Mod 8TB
The size of a char is always 1 byte, the C standard guarantees that. The number of bits in a byte is platform dependent.

The size of int should be the size that is most efficient for the platform to process (16 bits on a 16 bit processor etc) but that is not always the case.

For this post the platform should be just the processor but the compiler can have an effect too.
Dec 7 '08 #2
Arepi
62
Hi!
You can take the size of en variable from softwer whit operator sizeof. This give the size of actual variable in bytes.
use is simple:

Expand|Select|Wrap|Line Numbers
  1. int a,b;
  2. a=sizeof b;
Dec 7 '08 #3
Ganon11
3,652 Expert 2GB
With, of course, parentheses around the argument list:

Expand|Select|Wrap|Line Numbers
  1. int a, b;
  2. a = sizeof(b);
Dec 8 '08 #4
Banfa
9,065 Expert Mod 8TB
Actually the parentheses are only required if the argument is a type rather than an expression so

Expand|Select|Wrap|Line Numbers
  1. int a, b;
  2. a = sizeof b;       // OK
  3. a = sizeof(b);      // OK
  4. a = sizeof(int);    // OK
  5. a = sizeof int;     // NOT OK
  6.  
Personally when using the sizeof operator I try to take the size of an expression rather than a type, because it requires a little less maintenance.
Dec 8 '08 #5
newb16
687 512MB
@axneer
It depends only on compiler used ( and its settings ) - old borland C have 16-bit int wherever you run it, and you can as well compile some program with dos extender and run it with 32-bit int on 16-bit dos.
Dec 8 '08 #6
Ganon11
3,652 Expert 2GB
@Banfa
What!? Absurd. I didn't know C could look like Perl.
Dec 8 '08 #7
Banfa
9,065 Expert Mod 8TB
Remember sizeof is an operator, it is actually more absurd that it must have parentheses in 1 case, I mean you would write -b not -(b) normally wouldn't you?
Dec 8 '08 #8
JosAH
11,448 Expert 8TB
@Ganon11
No it isn't absurd; the parentheses disambuigate several matters; have a look:

Expand|Select|Wrap|Line Numbers
  1. typedef int t;
  2.  
  3. int main() {
  4.     t u;
  5.     char t= 42;
  6.     double d= sizeof t * 3.14159;
  7.     double e= sizeof (u * 3.14159);
  8.     double f= sizeof (u) * 3.14159;
  9.     printf("%f\n", d);
  10.     printf("%f\n", e);
  11.     printf("%f\n", f);
  12.     return 0;
  13. }
  14.  
Play with the parentheses and see what happens.

kind regards,

Jos
Dec 8 '08 #9
donbock
2,426 Expert 2GB
Integer types in Standard C have the following characteristics:
The width of 'char' is at least 8 bits;
the width of 'short' is at least 16 bits;
the width of 'long' is at least 32 bits;
the width of 'long long' is at least 64 bits;
the width of 'int' is greater than or equal to that of 'short' and less than or equal to that of 'long'.

An implementation is expected to select a size for 'int' that has the best performance on that platform and that also meets the preceding width constraints.

Refer to <limits.h> for these details for your particular implementation. This header specifies the number of bits in a 'char' (which is also the number of bits in a "storage unit", see below). Other than that, this header specifies the largest and smallest values that can be stored in each integral type rather than the width of the type in bits. Different implementations may use different encoding schemes for integers, so the same width in bits might corresponds to a different range of values.

The sizeof operator returns the size of a type or data object (in "storage units"). By definition, one storage unit is the amount of memory used to hold a 'char'; that is, sizeof(char) is always one. We like to think that sizeof returns a value in bytes, but that is only typically true. For example, an inefficient compiler implementation might store all integer types (including char) in 64-bit blocks of memory. If so, then sizeof any and all integer types would be one.
Dec 8 '08 #10
Banfa
9,065 Expert Mod 8TB
Which C standard are you talking about Don because what you have said is what I understand for C++ but for C89 my understanding is that

char is 1 byte which has CHAR_BIT (defined in limits.h) number of bits that can be <8 (I have heard of definite cases of 7 (and 9) although I can't put my fingers on the platform names)

and that

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

(I realise I haven't mentioned long long)
Dec 8 '08 #11
Arepi
62
@Ganon11
Let me to quote from "Brian W. Kernighan. Dennis M. Ritchie :The C programming language"

url:<URL Removed>
On site 111:

C provides a compile-time unary operator called sizeof that can be used to compute the size
of any object. The expressions
sizeof object
and
sizeof (type name)
Dec 8 '08 #12
donbock
2,426 Expert 2GB
My comments were intended to apply to C99. Refer to paragraph 5.2.4.2.1 (Sizes of integer types) of the Standard.

Expand|Select|Wrap|Line Numbers
  1. ... Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.
  2.  
  3. CHAR_BIT        8
  4. SCHAR_MIN    -127    // -(2^7 - 1)
  5. SCHAR_MAX    +127    // 2^7 - 1
  6. UCHAR_MAX    255    // 2^8 - 1
  7. SHRT_MIN    -32767    // -(2^15 - 1)
  8. SHRT_MAX    +32767    // 2^15 - 1
  9. USHRT_MAX    65535    // 2^16 - 1
  10. LONG_MIN    ...    // -(2^31 - 1)
  11. LONG_MAX    ...    // 2^31 - 1
  12. ULONG_MAX    ...    // 2^32 - 1
  13. LLONG_MIN    ...    // -(2^63 - 1)
  14. LLONG_MAX    ...    // 2^63 - 1
  15. ULLONG_MAX    ...    // 2^64 - 1
  16.  
These CHAR_BIT constraint permits support for a 9-bit char, but appears to prohibit a 7-bit char. The other constraints refer to minimum and maximum values, not number of bits. I can't imagine any integer encoding scheme that satisfies these constraints that wouldn't require at least 16 bits for short, 32 bits for long, or 64 bits for long long.

I know C89 also had minimum values for these parameters, but I don't know what they were. I don't have a copy of C89 nearby.

Regarding the sizeof hierarchy, I agree that every implementation I've ever seen matches the sequence you listed. Notice that here we are talking about a hierarcy of storage allocation, not a hierarchy of maximum values. Suppose a processor provided support for 8-bit, 16-bit, and 32-bit accesses -- but suppose its operation was much faster if all accesses were aligned on 32-bit boundaries. I think the Standard would permit an implementation to allocate 32 bits for 8- and 16-bit variables, even though many of those bits would be unused padding. If so, sizeof would return the same value for char, short, and long. I can't quote chapter and verse from the Standard for this one. We know sizeof a struct includes all pad bytes.
Dec 8 '08 #13
Banfa
9,065 Expert Mod 8TB
It is possible that the representation of a short or int or long includes bits or bytes not used to hold the actual value (bits used as padding or bits used for trap values).

However the story for char (or unsigned char at least) is different since the standard expressly dictates that using this type the CPU can contiguously access all bytes of memory. That precludes use of padding bytes in a char type to align the type to an efficient memory boundary for the processor.
Dec 8 '08 #14
donbock
2,426 Expert 2GB
@Banfa
Good point. You're right.
Dec 8 '08 #15
AmberJain
884 Expert 512MB
Hello,
@Arepi
The reply #12 contains a download link to the book "The C programming language". Now that link might violate the copyrights for the book. Admins/mods ahould consider removing that link.

Also, I could not find report button for this thread and so I replied in this thread.

Thanks....
AmbrNewlearner
Dec 8 '08 #16
boxfish
469 Expert 256MB
The report button is the little red and white triangle with the black line in the top right corner.
Dec 9 '08 #17
axneer
8
The author of my book is saying
"on a 16 bit OS like MS-DOS or windows 3.1 an integer occupies 2 bytes,In case of 32 bit Os like windows 95/98/Me/NT the size of int is 4 bytes"

also he has given a table like

data type range
signed char (-128 to 127)
unsigned char (0 to 255 )
signed int (-32768 to 32767)
like so on------

and said note at the bottom "the sizes and ranges of int,short and long are OS dependent.Sizes in figures are for 16 bit OS"

is he wrong or partially correct?
Dec 9 '08 #18
newb16
687 512MB
@axneer
He is correct about ranges for 16- and 8-bit values. But sizes for builtin types are defined by compiler, not OS. If he did not explained it as 'sizes of values accepted by OS API functions' it is wrong - e.g. 32bit program under dos extender under dos.
Dec 9 '08 #19
Banfa
9,065 Expert Mod 8TB
@ambrnewlearner
Good catch but by quoting it you doubled the amount of clean-up I had to do :D
Dec 9 '08 #20
AmberJain
884 Expert 512MB
Hello,

The report button is the little red and white triangle with the black line in the top right corner.
Oh ok...I get it now. ty.

Good catch but by quoting it you doubled the amount of clean-up I had to do :D
OOPS..I almost forgot that. ;)

AmbrNewlearner
Dec 9 '08 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Marti | last post by:
Dear everyone, Its my understanding that IE6 now uses a default text-size of "Small" rather than IE5's "Medium". Since I have used relative font-sizes (usually in ems) on all my sites, I am...
25
by: Matthias | last post by:
Hi, I am just reading that book by Scott Meyers. In Item 4 Meyers suggests to always use empty() instead of size() when probing for emptyness of STL containers. His reasoning is that size()...
9
by: Dr John Stockton | last post by:
Assuming default set-ups and considering all reasonable browsers, whatever that may mean, what should an author expect that his readers in general will see (with visual browsers) for a page with...
2
by: Kums | last post by:
What is the maximum permissible size of a database? Is there any limitation. What is the maximum # of tablespace's allowed in a database? Thanks for your response.
4
by: tshad | last post by:
I am having trouble with links in my DataGrid. I have Links all over my page set to smaller and they are consistant all over the page in both Mozilla and IE, except for the DataGrid. Here is a...
27
by: Deephay | last post by:
Greetings all, I have a program that used the realloc() function to change the allocated size of a buffer, the program works with some arguments, but with some other arguments, it will show me...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
7
by: carterweb | last post by:
This is how I do it now. 1. Determine the dimensions of the rectangle. 2. Set a my font size to a fixed maximum size. 3. Apply the font my string and measure the string using the graphics...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
27
by: 1001 Webs | last post by:
I am trying to make my style sheet as compatible as possible and I'm getting a bit confused here. I've read that the best size for font-size would be 76.1%; due to shortcomings in the way both...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.