473,789 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof 'a' returns 4

Hello

I've got a simple question regarding sizeof operator.
why sizeof 'a' return 4?
(Note the argument is without parenthesis)

If anybody is so kind to answer ...
TIA

Jun 18 '06 #1
29 2703
ln****@yahoo.co m wrote:
Hello

I've got a simple question regarding sizeof operator.
why sizeof 'a' return 4?
(Note the argument is without parenthesis)

If anybody is so kind to answer ...


check out the following page, it's a FAQ problem: -)

http://www.c-faq.com/charstring/sizeofchar.html

Xicheng

Jun 18 '06 #2
Excellent, thank you very much.
Xicheng Jia wrote:
ln****@yahoo.co m wrote:
Hello

I've got a simple question regarding sizeof operator.
why sizeof 'a' return 4?
(Note the argument is without parenthesis)

If anybody is so kind to answer ...


check out the following page, it's a FAQ problem: -)

http://www.c-faq.com/charstring/sizeofchar.html

Xicheng


Jun 18 '06 #3
Check out in this FAQ page:
http://www.c-faq.com/charstring/sizeofchar.html
ln****@yahoo.co m wrote:
Hello

I've got a simple question regarding sizeof operator.
why sizeof 'a' return 4?
(Note the argument is without parenthesis)

If anybody is so kind to answer ...
TIA


Jun 18 '06 #4
ln****@yahoo.co m said:
Hello

I've got a simple question regarding sizeof operator.
why sizeof 'a' return 4?
'a' is an int. The size of an int is implementation-dependent. I guess on
your system it's 4, yes?
(Note the argument is without parenthesis)


Good. No point splashing them everywhere for no reason.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 18 '06 #5
posted:
Hello

I've got a simple question regarding sizeof operator.
why sizeof 'a' return 4?
(Note the argument is without parenthesis)

If anybody is so kind to answer ...
TIA

typedef T double;
unsigned SizeOf( T const t )
{
const char * const current = (const char*)&t;

const char * const next = (const char*)(&t + 1);

return next - current;
}


--

Frederick Gotham
Jun 18 '06 #6
Frederick Gotham said:
posted:
Hello

I've got a simple question regarding sizeof operator.
why sizeof 'a' return 4?
(Note the argument is without parenthesis)

If anybody is so kind to answer ...
TIA

typedef T double;


Syntax error.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 18 '06 #7
Richard Heathfield wrote:
ln****@yahoo.co m said:
why sizeof 'a' return 4?

'a' is an int. The size of an int is implementation-dependent. I guess on
your system it's 4, yes?


But you seemed a little hesitant? If it's implementation-dependent, the
following equations are always true. It is true after my test :)

char c = 'c';
sizeof c == 1; /*by definition :)*/
sizeof 'c' == 4;
(Note the argument is without parenthesis)

Good. No point splashing them everywhere for no reason.


A pair of parentheses is required when type names applied to sizeof
operator. Is it because parentheses occur around type names in type
conventions?

lovecreatesbeau ty

Jun 18 '06 #8
lovecreatesbeau ty said:
Richard Heathfield wrote:
ln****@yahoo.co m said:
> why sizeof 'a' return 4? 'a' is an int. The size of an int is implementation-dependent. I guess on
your system it's 4, yes?


But you seemed a little hesitant?


Well, it might not be 4. It might simply be that you had a bug in your
program. Or maybe you /do/ have a bug in your program, but ints are still
four bytes wide on your system. Who knows? Insufficient information
available.
If it's implementation-dependent, the
following equations are always true.


....when using the version of your compiler that you are currently using,
with the same settings, on the same operating system on the same hardware,
yes.
> (Note the argument is without parenthesis)

Good. No point splashing them everywhere for no reason.


A pair of parentheses is required when type names applied to sizeof
operator.


I so rarely need to know that, because I almost never use sizeof to find the
size of a type. I'm far more interested in the size of an /object/.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 18 '06 #9
Richard Heathfield posted:
Frederick Gotham said:
posted:
Hello

I've got a simple question regarding sizeof operator.
why sizeof 'a' return 4?
(Note the argument is without parenthesis)

If anybody is so kind to answer ...
TIA

typedef T double;


Syntax error.

So as not to clutter the newsgroup, I would request in future that, if
you choose to scrutanise and correct my code, that you correct the error
rather than simply indicate that an error is present -- it will be of
benefit to us all.
typedef double T;
Another thing, and this is your own perogative of course, maybe you'd
like to be more "social" about how you offer your counsel? It's pretty
clear that my error was more a question of "getting things muddled up"
rather than having a deficit of proficiency. When I myself correct such
silly errors, I am usually more sociable about it -- even one little
comment:

You've got them the wrong way around, should be:
typedef double T;


--

Frederick Gotham
Jun 18 '06 #10

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

Similar topics

3
3672
by: Charlie | last post by:
My Google Groups search on this one showed the endless debates and I did my best to look through them to see if my question was answered and didn't find it. So, here it goes.... From the GG search, I have pretty much gathered and understand why using sizeof(pointer to char) returns 4 in my program. Is there a way to get it to return the actual amount of characters? the basic idea is that I am initializing a char and passing it into a...
12
13592
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!"; int size1 = sizeof(std::string); int size2 = sizeof(someString); and printed out the values of size1 and size2. size1 and size2 always
2
4272
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
83
15629
by: rahul8143 | last post by:
hello, what is difference between sizeof("abcd") and strlen("abcd")? why both functions gives different output when applied to same string "abcd". I tried following example for that. #include <stdio.h> #include <string.h> void main() { char *str1="abcd";
11
2175
by: Ben C | last post by:
Why does this compile with no warnings, what, if anything, does it mean, and why does the program print out 1? #include <stdio.h> int main(void) { printf("%d\n", sizeof(int())); return 0; }
90
8495
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
40
3663
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long int) ? Same question for the corresponding unsigned types.
1
2316
by: svlsr2000 | last post by:
Here is a small program which i saw a couple of years ago. #include<stdio.h> int main() { char x; if(sizeof(int)==sizeof('a')) printf("\n C compiler\n"); else
15
1538
by: somenath | last post by:
Hi All, I am not able to understand the behavior of the bellow mentioned program. #include<stdio.h> int main(void) { printf("\n Size = %d \n",sizeof 1<0); return 0;
7
1972
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. The original post at gnu.gcc.help can be found at this link http://groups.google.com/group/gnu.gcc.help/browse_thread/thread/ece55cbbd9c36270/2148a6c1ac6119e1?lnk=st&q=#2148a6c1ac6119e1 Here's the question: class base {
0
9657
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
9502
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10400
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
9974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7523
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
5544
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4084
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3688
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2902
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.