Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sizeof 1

Status
Not open for further replies.

mikeol

Programmer
Apr 22, 2003
8
IE
Just a quick question...
As far as I know, the size of a character in C is 1 byte.
Can somebody explain why the size of a constant character is 4 bytes? ie:

char c;
sizeof(c) = 1
sizeof('A') = 4
c = 'A';
sizeof(c) = 1

'A' is acharacter, how come the sizeof 'A' is not 4?

- Thanks
 
'A' is simply a representation of the decimal number 65, which is an integer (65 is the value of 'A' in the ASCII table). Assuming you're on 32-bit architecture, the size of an integer is 32-bits (or four bytes).

If you cast the parameter as a char, you will get a size of one:
Code:
int s = sizeof( (char) 'A' );
 
> As far as I know, the size of a character in C is 1 byte.
That is part of the definition of the language. The smallest addressable quantity of memory is called a char, and that size is set as being 1.

Not that you're likely to come across such cases unless you're really into programming embedded systems, DSP processors or obscure machines, but some machines have 16 or 32 bit chars, and their size is 1 also.

Every variable, structure, array or block of memory you allocate will be some multiple of this size.

> sizeof('A') = 4
'A' is an integral constant, so it basically the same as saying sizeof(int).

However, watch out if you move to C++, sizeof('A') is 1.




--
 
Status
Not open for further replies.

Similar threads

Replies
2
Views
114
  • Locked
  • Question
Replies
6
Views
142
  • Locked
  • Question
sizeof 1
Replies
8
Views
147

Part and Inventory Search

Sponsor

Back
Top