473,609 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

tryick use of sizeof again[Explaination Wanted]

char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl;// output: 'a'

why not 'b'?
thanks

Sep 14 '07 #1
72 3009
On Sep 14, 11:16 am, goacr...@gmail. com wrote:
char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl;// output: 'a'

why not 'b'
Who knows the vagarities of C++? Why not ask in comp.lang.c++ instead
of here? We don't deal with C++ here, only C
Sep 14 '07 #2
go******@gmail. com wrote:
char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl;// output: 'a'

why not 'b'?
thanks
sizeof expressions are NOT evaluated. Only the type is important
to get the size of it.
Sep 14 '07 #3
"Lew Pitcher" <lp******@teksa vvy.coma écrit dans le message de news:
11************* *********@w3g20 00...legro ups.com...
On Sep 14, 11:16 am, goacr...@gmail. com wrote:
>char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl ;// output: 'a'

why not 'b'

Who knows the vagarities of C++? Why not ask in comp.lang.c++ instead
of here? We don't deal with C++ here, only C
Why be unnecessarily obtuse when the issue is not C++ specific.

sizeof expr computes the size in bytes of the object denoted by expr without
producing code for evaluating that expression. Therefore ch is not
incremented.

--
Chqrlie.
Sep 14 '07 #4
jacob navia wrote:
go******@gmail. com wrote:
>char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl ;// output: 'a'

why not 'b'?
thanks

sizeof expressions are NOT evaluated. Only the type is important
to get the size of it.

indeed. Consider sizeof to be

size_t sizeof(type)
and not
size_t sizeof(int)

Sep 14 '07 #5
On Sep 14, 6:19 pm, "Charlie Gordon" <n...@chqrlie.o rgwrote:
Why be unnecessarily obtuse when the issue is not C++ specific.
How do you know it is not C++ specific? For example, after

char c - '0';
c += sizeof ('0');

what is the value stored in c? There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.

Sep 14 '07 #6
go******@gmail. com writes:
char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl;// output: 'a'

why not 'b'?
If you're going to post code here, please make sure it's C, not C++.
It's also helpful to post a (small) complete program rather than an
isolated snippet.

'cout', '<<', and 'endl' are specific to C++.

Your underlying question happens, in this particular case, to be
equally valid in both languages, but there are subtle differences
between C and C++ that even the C experts here may not be aware of.

Just this once, I'll show you a C version of your program.

#include <stdio.h>
int main(void)
{
char ch = 'a';
int v = sizeof ++ch;
printf("%c\n", ch);
return 0;
}

Next time, please either post C in the first place, or post to
comp.lang.c++. If you're having trouble knowing the difference, you
need to learn one language or the other (perhaps eventually both).
A newsgroup is not a good place to do that, but
alt.comp.lang.l earn.c-c++ might be more appropriate for your purposes
than comp.lang.c.

Several people have already given you the answer to your question --
which is unfortunate, since the question looks very much like
homework. An expression like 'sizeof ++ch' isn't something that you'd
write if your goal is to accomplish something. It was almost
certainly written by someone trying to make a specific point.

If you want us to do your homework for you, please post your
instructor's e-mail address so we can submit our solutions directly.
We're willing to provide hints if you've done some of the work
yourself and you're stuck on something, but we won't (usually) do it
for you.

--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 14 '07 #7
christian.bau wrote:
On Sep 14, 6:19 pm, "Charlie Gordon" <n...@chqrlie.o rgwrote:
>Why be unnecessarily obtuse when the issue is not C++ specific.

How do you know it is not C++ specific? For example, after

char c - '0';
c += sizeof ('0');

what is the value stored in c? There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.
This is not the case for the problem at hand. Both C and C++
do not evaluate the expression of a sizeof.

Sep 14 '07 #8


sizeof is a handy way of ensuring that an expression is legal without
actually evaluating the expression. For instance, you could have code
something like the following:

typedef ... SomeType;

void Func(SomeType const *p)
{
sizeof (p->val); /* Ensure that SomeType has a member called
val */
}

Martin

Sep 14 '07 #9
There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.

There's no subtle differences between C and C++ in this respect. Or
even differences for that matter.

Martin
Sep 14 '07 #10

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

Similar topics

10
1557
by: goacross | last post by:
i found something tricky this morning. char *p="abc"; 1. char m=1;// m='b' 2. char n=sizeof('h'); // n=1; I guess the reason of 1is,
0
8095
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
8588
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...
1
8236
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,...
0
7030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6068
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
5526
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1690
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1407
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.