473,735 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

On what does size of data types depend?

Hi all,

I am using gcc compiler in linux.I compiled a small program
int main()
{
printf("char : %d\n",sizeof(ch ar));
printf("unsigne d char : %d\n",sizeof(un signed char));
printf("short : %d\n",sizeof(sh ort));
printf("unsigne d short : %d\n",sizeof(un signed short));
printf("int : %d\n",sizeof(in t));
printf("unsigne d int : %d\n",sizeof(un signed int));
printf("long : %d\n",sizeof(lo ng));
printf("unsigne d long : %d\n",sizeof(un signed long));
printf("long long : %d\n",sizeof(lo ng long));
printf("unsigne d long long : %d\n",sizeof(un signed long
long));
}

Result was

char : 1
unsigned char : 1
short : 2
unsigned short : 2
int : 4
unsigned int : 4
long : 4
unsigned long : 4
long long : 8
unsigned long long : 8
What i want to know is what will be the effect if i use int in
place of long in applications running on linux and also on what factors
does the size of datatypes depend.

Thanks
Sunil.

Nov 15 '05 #1
35 5461
In general, the code you right should not depend upon the size of a
type on a platform.

The size of a type depends upon the processor architecture and the
compiler vendor. For example, a 16 bit MCU migth consider an int to be
2 bytes and a long to be 4 bytes.

On a 64 bit processor, a compiler vendor may decide to use 8 bytes to
represent an int.

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
System Design with Sequence Diagrams in PDF and Word EMF

Nov 15 '05 #2
Sunil wrote:
Hi all,

I am using gcc compiler in linux.I compiled a small program
int main()
{
printf("char : %d\n",sizeof(ch ar));
printf("unsigne d char : %d\n",sizeof(un signed char));
printf("short : %d\n",sizeof(sh ort));
printf("unsigne d short : %d\n",sizeof(un signed short));
printf("int : %d\n",sizeof(in t));
printf("unsigne d int : %d\n",sizeof(un signed int));
printf("long : %d\n",sizeof(lo ng));
printf("unsigne d long : %d\n",sizeof(un signed long));
printf("long long : %d\n",sizeof(lo ng long));
printf("unsigne d long long : %d\n",sizeof(un signed long
long));
}
On some systems I have used, the output would claim
that all the types are of size zero. Hint: What type of
value does "%d" expect, and what type of value does sizeof
produce?

You've also neglected to #include <stdio.h> and to
return a value from main() -- the latter is all right in
C99 but not in C90, and is doubtful practice in any case.
You're using gcc, which can produce a lot of helpful
diagnostics if you ask for them: ask for them.
Result was

char : 1
unsigned char : 1
short : 2
unsigned short : 2
int : 4
unsigned int : 4
long : 4
unsigned long : 4
long long : 8
unsigned long long : 8

What i want to know is what will be the effect if i use int in
place of long in applications running on linux and also on what factors
does the size of datatypes depend.


On your system and using your compiler with your choice
of compile-time options, `long' and `int' appear to have the
same size. This suggests that they probably also have the
same range, but you'd need to display the actual values from
<limits.h> to be completely certain.

But even if they have the same size, the same range, and
the same representation, they remain distinct types. You can
prove this to yourself with a small test program:

int func(void);
long (*fptr)(void) = func;

You may wonder why "A difference that makes no difference"
is, in this case, truly a difference. The answer is that the
fact that `int' and `long' look the same on your machine under
current conditions does not mean that they look the same on all
machines, or even on your machine with different compiler flags.
If you intend never to move your program to another machine,
never to upgrade your current machine, never to move to 64-bit
Linux, and never to change compilers, then you can ignore the
distinction between `int' and `long' (except in situations like
that I've shown above, where the compiler is required to be
more scrupulous than you).

On the other hand, if you think that your current system
and all its software might not be the very last one you ever
want to use, you should not pretend that `int' and `long' are
the same thing. Three useful references:

The Tenth Commandment
http://www.lysator.liu.se/c/ten-commandments.html

FAQ Question 1.1
http://www.eskimo.com/~scs/C-faq/top.html

IAQ Question 1.1
http://www.plethora.net/~seebs/faqs/c-iaq.html
(be sure to read the footnotes)

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #3

Sunil wrote:
What i want to know is what will be the effect if i use int in
place of long in applications running on linux and also on what factors
does the size of datatypes depend.


The size depends on the implementation. Incidentally, the size is
measured in units of the space occupied by a char, which is not
guaranteed to take 8 bits of space, though it often does.

In most cases, the exact amount of space something takes should not
concern the programmer: use char when talking about characters,
unsigned char when talking about raw memory, short or unsigned short
when space savings is important, signed or unsigned char for even more
savings, long and unsigned long when dealing with large integers, long
long and unsigned long long when the integers may be really long, and
int and unsigned int when you want to use whatever is the most
`natural' integer representation in the implementation (i.e. int or
unsigned int ought to be the one you use unless there is reason to
deviate). Use the signed types if you think of them as integers, use
the unsigned types if you treat them as bit patterns, or need to use
the extra range on the large positive end, or the logic of the program
is such that blindly converting negative numbers to large positive
integers is the `right thing' to do!

The C standard does guarantee some minimum range of values for each of
these types: look at those, and decide when you want space savings
versus when your integers may become large in magnitude in interpreting
the last paragraph. But don't apply the rules blindly ... experience
teaches you what is likely to be the best data type. C99 also lets you
more fine grained control over integral types: look at them. In rare
cases, bitfields might also be useful.

Do not gratuitously put in `linux' dependencies: and not all linux
platforms will have the exact same behaviour anyway. Even if they are
the same size, int and long are not interchangeable , a program will
often become `incorrect' if you change ints to longs without changing
anything else. Evn though this makes the behaviour undefined, in
practice, on current implementations , it is not likely to create a
difference except in warnings from the compiler. But why take the
chance?

Note that sizeof gives you the space occupied in memory: and it is
possible for an implementation to not effectively use all the space, so
use the macros like CHAR_MAX if you need the exact ranges. It may also
not use the same representation in memory for different types of the
same size (For example, there is no bar to using little endian for ints
and big endian for the longs, as long as the compiler takes care to do
the bit manipulations properly; no implementation I know of does that
yet). The implementation can further require different alignments for
the two types (thus, it might decide on a 2 byte alignment for ints and
4 byte alignment for longs: planning on using 16 bit bus operations for
ints and 32 bit operations for longs; again I know of no implementation
that does that).

In short, you are asking questions which you `should' not be. C is
trying to provide a level of abstraction: a division of labour between
the programmer and the implementation. The programmer describes what
he/she wants and some low level things like whether space or range is
more important, the implementation takes care of the hardware and makes
the program run according to the specification. The standard provides
the language for unambiguous communication. Your questions are
crossing the border, violating one of the raisons de etre of high level
langauges.

Sure, there are occasions when you need to know your precise hardware
and how your implementation maps your code to it. The phrasing of your
question seems to suggest you are not in that situation, though.

Nov 15 '05 #4
Thanks for good info.

Nov 15 '05 #5
"Sunil" <su***********@ gmail.com> writes:
Hi all,

I am using gcc compiler in linux.I compiled a small program
int main()
{
printf("char : %d\n",sizeof(ch ar));
printf("unsigne d char : %d\n",sizeof(un signed char));
printf("short : %d\n",sizeof(sh ort));


<SNIP>

This brings to mind something that I have wondered about.

I often see advice elsewhere, and in other peoples programs,
suggesting hiding all C "fundamenta l" types behind typedefs such as

typedef char CHAR;
typedef int INT32;
typedef unsigned int UINT32;
typedef char* PCHAR;

The theory is that application code which always uses these typedefs
will be more likely to run on multiple systems (provided the typedefs
are changed of course).

I used to do this. Then I found out that C99 defined things like
"uint32_t", so I started using these versions instead. But after
following this group for a while I now find even these ugly and don't
use them unless unavoidable.

What do people here think is best?

--

John Devereux
Nov 15 '05 #6
John Devereux <jd******@THISd evereux.me.uk> writes:
"Sunil" <su***********@ gmail.com> writes:
Hi all,

I am using gcc compiler in linux.I compiled a small program
int main()
{
printf("char : %d\n",sizeof(ch ar));
printf("unsigne d char : %d\n",sizeof(un signed char));
printf("short : %d\n",sizeof(sh ort));


<SNIP>

This brings to mind something that I have wondered about.

I often see advice elsewhere, and in other peoples programs,
suggesting hiding all C "fundamenta l" types behind typedefs such as

typedef char CHAR;
typedef int INT32;
typedef unsigned int UINT32;
typedef char* PCHAR;

The theory is that application code which always uses these typedefs
will be more likely to run on multiple systems (provided the typedefs
are changed of course).

I used to do this. Then I found out that C99 defined things like
"uint32_t", so I started using these versions instead. But after
following this group for a while I now find even these ugly and don't
use them unless unavoidable.

What do people here think is best?


Depends on whether the code really needs to depend on the size of its
variables.

--
Lowell Gilbert, embedded/networking software engineer
http://be-well.ilk.org/~lowell/
Nov 15 '05 #7

John Devereux wrote:
I used to do this. Then I found out that C99 defined things like
"uint32_t", so I started using these versions instead. But after
following this group for a while I now find even these ugly and don't
use them unless unavoidable.

What do people here think is best?


Good coding style can rarely be encapsulated into simple rules! I
suggest that these C99 features be used in favour of pragmas (like `I
want this thing to be really fast') or making unwarranted assumptions
like unsigned int can hold 32 bits. If I can get away by just using
long instead of int, though, I do it in preference to using the more
precise specifications.

The idea is that even though we can avoid them, we often do not write a
strictly conforming code because a fast algorithm using bit
manipulations might be available if we made assumptions about the exact
number of bits, and conditioning everything on limits.h may be
unnecessary for the project at hand. Or, we may be tempted to use
compiler flags to guarantee speed or space savings. If the C99
integral type definitions solve the problem (by, at worst, detecting
problem at compile time), use them in preference to silent breakage
when code is ported.

Most often, I find no particular reason to use them, and I do not.

Nov 15 '05 #8
Lowell Gilbert <lg******@be-well.ilk.org> writes:
John Devereux <jd******@THISd evereux.me.uk> writes:
This brings to mind something that I have wondered about.

I often see advice elsewhere, and in other peoples programs,
suggesting hiding all C "fundamenta l" types behind typedefs such as

typedef char CHAR;
typedef int INT32;
typedef unsigned int UINT32;
typedef char* PCHAR;

The theory is that application code which always uses these typedefs
will be more likely to run on multiple systems (provided the typedefs
are changed of course).

I used to do this. Then I found out that C99 defined things like
"uint32_t", so I started using these versions instead. But after
following this group for a while I now find even these ugly and don't
use them unless unavoidable.

What do people here think is best?


Depends on whether the code really needs to depend on the size of its
variables.


What I am asking is whether one should habitually use them *just in
case* something breaks when run on another platform. I have seen
programs where int, long etc. are *never used* except in a "types.h"
header.

--

John Devereux
Nov 15 '05 #9


Skarmander wrote On 10/05/05 10:30,:
Eric Sosman wrote:
Sunil wrote:
printf("char : %d\n",sizeof(ch ar));
[...]


On some systems I have used, the output would claim
that all the types are of size zero. Hint: What type of
value does "%d" expect, and what type of value does sizeof
produce?

<snip>
What exactly *is* the format specifier for size_t in C90? C99 has "%zu",
but is (say) "%lu" guaranteed to work?


The usual C90 way is

printf ("size = %lu\n", (unsigned long)sizeof(Typ e));

This works because size_t must be an unsigned integer type,
C90 has only four such types, and unsigned long can handle
all the values of any of the four.

In C99 the number of unsigned integer types is much
larger, and varies from one implementation to another. The
widest unsigned integer type is uintmax_t, so one could
write (using another C99-invented length modifier)

printf ("size = %ju\n", (uintmax_t)size of(Type));

I do not know for sure why the committee decided to
invent the "z" width modifier, but two motivations seem
plausible:

- That silly cast is a pain, and since other length
modifiers were already being invented it was easy
to introduce a new one for size_t.

- On small machines size_t might be as narrow as 16
bits, while uintmax_t must be at least 64 bits.
Working with 64-bit values might require a multi-
precision software library that would otherwise
not be needed. The "z" modifier lets one avoid
using uintmax_t, and might allow the implementation
to exclude the unnecessary library (recall systems
that tried to omit software floating-point support
when they thought the program wouldn't use it.)

--
Er*********@sun .com

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
17775
by: Shawn Berg | last post by:
Is the Integer data type deprecated? I am not sure what Integer data type to use in some of my classes. According to the URL below, if I am reading it correctly, it seems as though I should be using the Short, Integer, and Long data types. Is this correct? Or should I instead be using the Int16, 32, and 64 data types in my code? Whats the best practice here? http://www.ondotnet.com/pub/a/dotnet/2001/07/30/vb7.html?page=2
9
4646
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
13
3689
by: Shailesh Humbad | last post by:
I wrote a short page as a quick reference to c++ integer data types. Any feedback welcome: http://www.somacon.com/blog/page11.php
7
1557
by: Abhishek Saksena | last post by:
Hi all, Is there is any strong reason not to use standard C++ native datatypes. The only reason I can imagine is protability issuse on different machines.But did C++ not gurantee portaibilty of standard native datatypes ?
1
2406
by: Neil Zanella | last post by:
Hello all, In C and C++ a primitive data type is represented by a minimum number of bits as defined by the corresponding standard. For instance an int is assumed to be at least 16 bits on all platform where in practice an int is 16 bits on the arcane 16 bit machines and 32 bits on the most commonly used 32 bit machines. However, my C# reference says that an int in C# is always 32 bits wide. It also specifies a specific number of bits...
6
3406
by: RaulAbHK | last post by:
Dear all, I guess this is a basic question with an easy answer but I am a beginner and I would much apreciate your feedbacks. Let's say I have a library with some functionality to perform some image processing on jpeg images. One of functions in the library is similar to this: myfunction_effect (&out_instance, &mysettings, I8 *buf_in, I32
4
2459
by: Abdul Samad | last post by:
banfa just said that it is because so the compiler may not assign the same memory to two variables but my question is WHY DOES COPILER ASSIGNMS MEMORY WHEN THERE IS NO NEED OF IT? as the structure has no data types or member functions so why the memory needed to be assignem to structured variable with no data types? for what purpose? Regards
15
8441
by: Madhur | last post by:
Hi All, I would like you help me in creating an array of data types. I am interested in look at the the data type which looks like this Array a={int,float,char,int*..............................}, so that a should return me int and a should return me
2
2392
by: zeeshan708 | last post by:
Is it the compiler that determines the size of the data types (e.g an int is of 2 bytes or of 4 bytes ) or does it depends on the computer's hardware that shall determine the size of the data types.
0
8964
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8786
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9201
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6049
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4564
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2740
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2190
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.