473,320 Members | 1,933 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,320 software developers and data experts.

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

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
Nov 15 '05 #1
24 9083

jimjim wrote:
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.

TIA


--
kr******@india.ti.com

Nov 15 '05 #2
jimjim wrote:
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>
Nov 15 '05 #3
jimjim wrote:
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>
Nov 15 '05 #4
jimjim wrote:
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? <snip>


"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>
Nov 15 '05 #5
hehe, you have just repeated the apparent ;-P

thx for the input anyways...
Nov 15 '05 #6
> "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.
Nov 15 '05 #7
> 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.

Nov 15 '05 #8
> 4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .
sizeof() is a C operator not a function.

Nov 15 '05 #9
> "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
Nov 15 '05 #10
> 4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .

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

Nov 15 '05 #11
> 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
Nov 15 '05 #12
"Nerox" <ne****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .


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

ohh, I asked because, for example, the new operator in C++ has a prototype.
Nov 15 '05 #13

"jimjim" <ne*****@blueyonder.co.uk> wrote in message
news:%b*******************@text.news.blueyonder.co .uk...
"Nerox" <ne****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .


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

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
Nov 15 '05 #14
> <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>

oups, i messed it up completely!
Nov 15 '05 #15
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
Nov 15 '05 #16
jimjim wrote on 13/09/05 :
"Nerox" <ne****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .


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

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


What the hell is C++ ? We are on clc.

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

..sig under repair
Nov 15 '05 #17
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
news:mn***********************@YOURBRAnoos.fr...
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.

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
Nov 15 '05 #18
jimjim wrote:
"Nerox" <ne****@gmail.com> wrote:
4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .
Of course you can't find it, sizeof() is a C operator not a function.


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

Nov 15 '05 #19
Ulrich Eckhardt <do******@knuut.de> writes:
jimjim wrote:
"Nerox" <ne****@gmail.com> wrote:
4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .

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


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.]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #20
In article <Ka*****************@text.news.blueyonder.co.uk> "jimjim" <ne*****@blueyonder.co.uk> writes:
....
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?


Not entirely. In the context of:

char c;
void f(char p, ...);

in the call f(c, c) the first argument is passed as a char, the
second as an int.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #21
On Tue, 13 Sep 2005 23:19:26 +0200, Ulrich Eckhardt
<do******@knuut.de> wrote in comp.lang.c:
jimjim wrote:
"Nerox" <ne****@gmail.com> wrote:
4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .

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


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


Not quite accurate. The first one should be:

sizeof some_expression;

....as, for example, sizeof 'a'. No parentheses required. And no
object required, either.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #22
jimjim wrote:

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?
No, it is passed as a char.
2. In a variadic function, a char passed to a function is definatelly
promoted to an int, right?
Not definitely. Char parameters that are explicitly designated in the
declaration are passed as char. If the variadic part of the parameter
list contains a char, then it is promoted to int.
3. Is there any difference between invoking f( ) as f( x ) and f('x')?
Yes, it is completely different. The first passes the value of the
variable x and the second the ordinal representation of the character 'x'.
4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .


I would be surprised if you did; sizeof is an operator, rather than a
function.

-- Denis
Nov 15 '05 #23
Emmanuel Delahaye wrote:
jimjim wrote on 13/09/05 :
"Nerox" <ne****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
4. Unfortunatelly, I cannot find sizeof( )'s prototype :-( .

Of course you can't find it, sizeof() is a C operator not a function.
ohh, I asked because, for example, the new operator in C++ has a
prototype.


What the hell is C++ ?


What planet are you living on?
We are on clc.


Is that anywhere near Earth? ;)

The OP wasn't asking about the new operator, they were trying to
explain
their reasoning for thinking that sizeof() needs a prototype.

If someone learning French comes up to you and explains why they
misunderstood a 'faux amis', would you tell them to piss off because
you don't speak English, or would you politely explain that, despite
the identical spelling, the languages are distinct and the same word
should be treated in context?

--
Peter

Nov 15 '05 #24
jimjim wrote:
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."
This opinion seems accurate to me.
Moreover, should integral read integer?


The word "integral" has many meanings, one of which is:
"Expressed or expressible as or in terms of integers."
Essentially the above quote is using "integral" as an adjectival form of
the noun "integer".

--
Simon.
Nov 15 '05 #25

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

Similar topics

3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
9
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have...
7
by: dam_fool_2003 | last post by:
#include<stdio.h> int main(void) { unsigned int a=20,b=50, c = sizeof b+a; printf("%d\n",c); return 0; } out put: 24
42
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
by: junky_fellow | last post by:
Consider the following piece of code: #include <stddef.h> int main (void) { int i, j=1; char c; printf("\nsize =%lu\n", sizeof(i+j));
90
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
32
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return...
5
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.