G
goacross
char ch='a';
int v=sizeof ++ch;
cout<<ch<<endl;// output: 'a'
why not 'b'?
thanks
int v=sizeof ++ch;
cout<<ch<<endl;// output: 'a'
why not 'b'?
thanks
char ch='a';
int v=sizeof ++ch;
cout<<ch<<endl;// output: 'a'
why not 'b'
char ch='a';
int v=sizeof ++ch;
cout<<ch<<endl;// output: 'a'
why not 'b'?
thanks
Lew Pitcher said: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
jacob said:sizeof expressions are NOT evaluated. Only the type is important
to get the size of it.
Why be unnecessarily obtuse when the issue is not C++ specific.
christian.bau said: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.
char ch='a';
int v=sizeof ++ch;
cout<<ch<<endl;// output: 'a'
why not 'b'?
christian.bau said: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.
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.
jacob navia said:This is not the case for the problem at hand. Both C and C++
do not evaluate the expression of a sizeof.
As Keith pointed out, VLAs muddy the waters.There's no subtle differences between C and C++ in this respect. Or
even differences for that matter.
christian.bau said: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.
Martin Wells said:There's no subtle differences between C and C++ in this respect. Or
even differences for that matter.
Charlie Gordon said:Wrong!
But you snipped the code and attribution.
sizeof('1') evaluates differently in C and C++. In C it is equivalent to
sizeof(int), while in C++ it evaluates like sizeof(char). On most
architectures, this will yield a different value.
Keith Thompson said:I would argue that that's not a difference in the behavior of the
sizeof operator. The operator behaves exactly the same in both
languages, yielding the size in bytes of its operand. The meaning of
the operand just happens to be different.
But it does illustrate the point that there are subtle differences
between the two languages. Asking about C++ code here in comp.lang.c
can sometimes lead to confusion; it will inevitably lead to arguments
like this one.
Charlie Gordon said:How can you demonstrate the true nature of 'h' without resorting to the
sizeof operator ?
The very reason it is an int is quite obscure, multibyte character constants
are not defined in any standard way, it would be foolish to use them.
I know. But isn't it more helpful to explain these subtle differences than
to brutally bash OPs for trespassing without cause ?
That's beside the point, which is that (as far as I know) sizeof
behaves identically in C and C++.
Not all of us *know* the subtle differences.
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.
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.