size of int and char

R

rafal_

What is the size of int on 64-bit computers ?
Can char have minimal size different from signed char ?
 
E

Eric Sosman

What is the size of int on 64-bit computers ?

sizeof(int). All that can be said with certainty is
that this value is at least ceil(16/CHAR_BIT); it might
be greater.
Can char have minimal size different from signed char ?

No. All of sizeof(char), sizeof(signed char), and
sizeof(unsigned char) are equal to 1.
 
M

Martin Ambuhl

What is the size of int on 64-bit computers ?

Whatever the implementation uses, as long as it includes at least the
range the standard requires for ints (-32767 through +32767). The only
answers are sizeof(int) or sizeof(int)*CHAR_BIT, depending on what your
question actually is.
Can char have minimal size different from signed char ?

sizeof(char) = sizeof(unsigned char) = sizeof(signed char) = 1
by definition.
 
A

Army1987

What is the size of int on 64-bit computers ? sizeof(int)

Can char have minimal size different from signed char ?
No. They both have size 1, by definition.
 
K

Keith Thompson

I meant if sizeof(int) and size of registers are equal.

What's a "register"?

The C standard (which defines the C language, which is what we discuss
here) doesn't say anthing about "registers". (The "register" keyword
is merely a hint that access to a variable should be quick; it also
means you can't take its address.) The standard also has no concept
of a "64-bit system". For that matter, even in general there's no
univerally agreed definition of what a "64-bit system" is.

Within the parameters set by the standard, the sizes of the
fundamental types are decided by the implementers (compiler authors),
in any way they choose.

I've worked on "64-bit systems" with 32-bit ints, and on other "64-bit
systems" with 64-bit ints.

There is a drawback to making int 64 bits. Assuming 8-bit characters,
it leaves a gap in the integer type system. There's only one type
bigger than char and smaller than int, namely short, which means that
either there won't be a 16-bit type, or there won't be a 32-bit type.
(I'm ignoring the unsigned variants; they're required to be the same
size as the corresponding signed types.) I think that's part of the
reason that many 64-bit implementations have kept int at 32 bits
rather than 64. Another consideration is compatibility with 32-bit
systems; well-written software shouldn't care, but not all software is
well-written.

But the decision is entirely up to the implementer. int has to be at
least as wide as char and short, no wider than long (and long long, if
it exists; it's required in C99), and at least 16 bits.
 
P

Peter Nilsson

I meant if sizeof(int) and size of registers are equal.

The C language doesn't specify the 'size' of hardware
registers, so it's difficult to know what you mean by size
of registers. I presume you're not talking about the size
of register qualified objects.

In any case, the answer remains the same from the point
of view of a C program. The size of an int is precisely
sizeof(int) bytes.

A C byte needs to be at least 8 bits. The minimum required
range of an int means it must have at least 16 bits. Beyond
that, an implementation can pick and choose what sizes it
wants. That size is influenced by the architecture, but it
needn't be a slave to it.
 
P

Peter Nilsson

Keith Thompson said:
...
The C standard (which defines the C language, which is
what we discuss here) doesn't say anthing about "registers".

5.1.2.3p12 (Example 4) may be non-normative, but it clearly
talks about registers as something other than an abstract
storage class.
(The "register" keyword is merely a hint that access to a
variable should be quick; it also means you can't take its
address.)

Was that keyword and storage class randomly named?
 
M

Mark McIntyre

I meant if sizeof(int) and size of registers are equal.

The answer is the same. The size of an int is by definition
sizeof(int). If you want to know how many bits that is, you need to
read your compiler documentation since it is not specified by the C
standard.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
K

Keith Thompson

Peter Nilsson said:
5.1.2.3p12 (Example 4) may be non-normative, but it clearly
talks about registers as something other than an abstract
storage class.

You're right, I wasn't aware of that example. On the other hand, the
wording there specifically says that the use of registers doesn't
affect the semantics of the code.
Was that keyword and storage class randomly named?

Certainly not; the original intent was that a variable declared with
the "register" keyword should, if possible, be stored in a register.
But the current definition has no trace of that meaning, other than
the name.
 
S

Stephen Sprunk

sid said:
size of int is 4 bytes on my AMD athlon 64 bit machine

With the particular implementation you're testing, perhaps. Others running
on the same CPU may be 2 or 8 bytes. It has little to do with the CPU; it's
all about the particular ABI you're targeting...

S
 
K

Keith Thompson

sid said:
size of int is 4 bytes on my AMD athlon 64 bit machine

sizeof(int) could easily be 8 on the same machine with a different
compiler or operating system, or even with different options to the
same compiler.

See the other responses in this thread for a detailed explanation.
 
A

Army1987

Peter Nilsson said:
5.1.2.3p12 (Example 4) may be non-normative, but it clearly
talks about registers as something other than an abstract
storage class.


Was that keyword and storage class randomly named?

It is irrelevant here. You can also have
struct Verylarge {
long integer;
long double real;
void *pointer;
void (*function)();
};
register struct Verylarge a;
even if it won't fit in a register. (And you can also have register
arrays, but since you can't take the address of them (not even
implicitly) the only thing you'll be able to do with them is to
measure their size with sizeof.)
 
C

cr88192

Keith Thompson said:
sizeof(int) could easily be 8 on the same machine with a different
compiler or operating system, or even with different options to the
same compiler.

See the other responses in this thread for a detailed explanation.

'could be', yes, but 'is', probably not.

that is why I, personally, find this thread annoying.


now, on x86-64, will anyone make sizeof(int)==8?...
I would doubt it.

64-bit operations are more expensive than 32 bit ones (at least in terms of
more often requiring a REX prefix, ...).

now, on a different arch? maybe, but also likely not.


and, if a compiler writer did do this?
well, then, a good deal of existing apps would break.

usually, when targeting a particular arch, we make a few assumptions, such
as the sizes of various types, ...

and, people will not change these, lest they risk the resultant crap storm.

and what of 'sizeof(int)==2'? well, those targeting something like dos will
tend to know at least this much. "oh wow, I am targetting dos, I feel like
malloc'ing a 1MB buffer", errm, no...

the compiler has the right and moral obligation to ram its foot up the dev's
backend...


after all, all this is part of why we 'port' apps.

a good deal of apps, regularly make certain assumptions, regardless of
whatever the spec may happen to say.


now, if we were talking about sizeof(long), well then, this one does vary in
this case...


theory is one thing, practice is another...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,248
Latest member
MagdalenaB

Latest Threads

Top