473,661 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9168

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
> 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 #8
"Nerox" <ne****@gmail.c om> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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 #9

"jimjim" <ne*****@blueyo nder.co.uk> wrote in message
news:%b******** ***********@tex t.news.blueyond er.co.uk...
"Nerox" <ne****@gmail.c om> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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 #10

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

Similar topics

3
3547
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 know it returns two bytes in UniCode. Please help... For ANSI: In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator yields the number of bytes in the object representation of its
2
2456
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
9222
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 "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal too. But is is not: #include <dirent.h>
9
3009
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 identical precedence and right-to-left parsing, so why isn't the above equivalent to printf ("%d\n", (int)sizeof ((char)(char)2));
7
1925
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
2389
by: Christopher C. Stacy | last post by:
Some people say sizeof(type) and other say sizeof(variable). Why?
8
2529
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
8399
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
2571
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 the size occupied by that datatype in memory in bytes. Thanks :) Abhishek Srivastava
5
2888
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 before runtime ? I also have: { void check_foo_size(void);
0
8432
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
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8758
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
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,...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
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...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1743
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.