sizeof('x') in C and in C++

J

jimjim

Hello all,

I was reading the C++ FAQ and I was astonished to learn that sizeof('x') is
equal to sizeof(char) in C++, but is equal to sizeof(int) in C.

1. Why is this?
2. Can you provide a simple code fragment that can exibit the implications
of sizeof('x') being equal to sizeof(int) (I would expect it to be
sizeof(char) as in C++)?
3. Will the code break if a C application is compiled with C++ code?

TIA
 
K

Krishanu Debnath

jimjim said:
Hello all,

I was reading the C++ FAQ and I was astonished to learn that sizeof('x') is
equal to sizeof(char) in C++, but is equal to sizeof(int) in C.

1. Why is this?

Becasue character literal is of integer type in C and character type in
C++.
2. Can you provide a simple code fragment that can exibit the implications
of sizeof('x') being equal to sizeof(int) (I would expect it to be
sizeof(char) as in C++)?

No, I can't right now. But you can, by printing the value of both.
3. Will the code break if a C application is compiled with C++ code?

C and C++ are two different language. Try to refrain from mixing these
two.
 
A

Anand

jimjim said:
Hello all,

I was reading the C++ FAQ and I was astonished to learn that sizeof('x') is
equal to sizeof(char) in C++, but is equal to sizeof(int) in C.

1. Why is this?
"For historical reasons"

Lawrence Kirby shed's some light on that history in the recent thread in
this group titled:
"Why is a character constant more than size of charcter variable"
<vague-memory >
I also vageuly remember reading some where (either in clc or in csc)
that BCPL also might have had an hand in that.
<vague-memory>

<snip>
 
A

Anand

jimjim said:
Hello all,

I was reading the C++ FAQ and I was astonished to learn that sizeof('x') is
equal to sizeof(char) in C++, but is equal to sizeof(int) in C.

1. Why is this?
2. Can you provide a simple code fragment that can exibit the implications
of sizeof('x') being equal to sizeof(int) (I would expect it to be
sizeof(char) as in C++)?
3. Will the code break if a C application is compiled with C++ code?

TIA
"For historical reasons"

search for "Why is a character constant more than size of character
variable" in this group (Lawrence Kirby sheds some light on the history)

<vague-memory >
I also vaguely remember reading some where (either in clc or in csc)
that BCPL also might have had an hand in that.
<vague-memory>

<snip>
 
A

Anand

jimjim said:
Hello all,

I was reading the C++ FAQ and I was astonished to learn that sizeof('x') is
equal to sizeof(char) in C++, but is equal to sizeof(int) in C.

1. Why is this?

"For historical reasons"

search for "Why is a character constant more than size of character
variable" in this group (Lawrence Kirby sheds some light on the history)

<vague-memory >
I also vaguely remember reading some where (either in clc or in csc)
that BCPL also might have had an hand in that.
</vague-memory>
 
J

jimjim

"For historical reasons"
search for "Why is a character constant more than size of character
variable" in this group (Lawrence Kirby sheds some light on the history)
thx for the help...didnt use the right keywords to find the right thread...

I posted a question some time ago to this group about variadic functions and
I learned about integer promotions...so allow me to fire another question:

1. When there is a function prototype, e.g. void f(char x), is the char
passed to the function promoted to an int?
2. In a variadic function, a char passed to a function is definatelly
promoted to an int, right?
3. Is there any difference between invoking f( ) as f( x ) and f('x')?
4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .

TIA

P.S: I did read the FAQ but it doesnt really explain it.
 
N

Nerox

1. When there is a function prototype, e.g. void f(char x), is the char
passed to the function promoted to an int?

I think not.
2. In a variadic function, a char passed to a function is definatelly
promoted to an int, right?

Yes it does.
3. Is there any difference between invoking f( ) as f( x ) and f('x')?

f(x): you pass to f() what the x variable contains.
f('x'): you pass to f() the numeral representation of the x character.
 
N

Nerox

4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .
sizeof() is a C operator not a function.
 
J

jimjim

"For historical reasons"
search for "Why is a character constant more than size of character
variable" in this group (Lawrence Kirby sheds some light on the history)
so, its all about the integer promotion! thx for the help
 
N

Nerox

4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .

Of course you can't find it, sizeof() is a C operator not a function.
 
J

jimjim

f(x): you pass to f() what the x variable contains.
f('x'): you pass to f() the numeral representation of the x character.
sorry, I also meant to write that x is a char...

so,
1. if a prototype is provided, an one-byte copy of the char x is passed to
f( ) (unix/intel platform).
2. if a prototype is not provided, a four-byte int, which contains the value
of char x, is passed to f( ) unix/intel platform).
3. if its a variadic function, a four-byte int, which contains the value of
char x, is passed to f( ) unix/intel platform).

right?

TIA
 
M

Mike Wahler

jimjim said:
ohh, I asked because, for example, the new operator in C++ has a
prototype.

<OT>
No, it doesn't. C++ has the 'new operator' which is a keyword.
It invokes a function called 'operator new' (which does have
a prototype).
</OT>

As someone else already warned: Do *not* make assumptions about
one language based upon the other. C and C++ are two separate,
distinct languages. They share much common syntax and functionality,
but for many constructs which *seem* the same, the rules and semantics
are different.


-Mike
 
J

jimjim

No, it doesn't. C++ has the 'new operator' which is a keyword.
It invokes a function called 'operator new' (which does have
a prototype).
</OT>
oups, i messed it up completely!
 
E

Emmanuel Delahaye

jimjim wrote on 13/09/05 :
I was reading the C++ FAQ and I was astonished to learn that sizeof('x') is
equal to sizeof(char) in C++, but is equal to sizeof(int) in C.

1. Why is this?

Because C and C++ are different languages.
3. Will the code break if a C application is compiled with C++ code?

Yes. Any C code compiled with a non-C compiler produces an undefined
behaviour. Don't even try it, you are wasting your time.


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
 
J

jimjim

Emmanuel Delahaye said:
jimjim wrote on 13/09/05 :

Because C and C++ are different languages.
Would you disagree with this opinion, which was posted in a past thread
discussing the issue in question?

"More specifically the integral promotions. In K&R C it was virtually (?)
impossible to use a character value without it being promoted to int first,
so making character constant int in the first place eliminated that step.
There were and still are multi character constants such as 'abcd' or however
many will fit in an int."

Moreover, should integral read integer?

TIA
 
U

Ulrich Eckhardt

I'd like to amend that even the brackets are optional. There are two ways
to invoke sizeof, one taking a reference and the other taking a type:

sizeof some_object;// no brackets necessary
sizeof (float);// brackets necessary
ohh, I asked because, for example, the new operator in C++ has a
prototype.

As someone else already pointed out, there is an operator new which has a
prototype. Other than that, C++ also allows user-defined operator
overloads so of course you need to be able to specify a prototype (called
function declaration in C++).

Uli
 
K

Keith Thompson

Ulrich Eckhardt said:
I'd like to amend that even the brackets are optional. There are two ways
to invoke sizeof, one taking a reference and the other taking a type:

sizeof some_object;// no brackets necessary
sizeof (float);// brackets necessary

The argument to sizeof is either an expression (not a "reference") or
a type name enclosed in parentheses. BTW, in American English the
most common terms are:

'(', ')': parentheses
'[', ']': brackets
'{', '}': braces

[Remainder snipped; C++ is off-topic.]
 

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
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top