By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,378 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

Stroustrup 5.9 exercise 6

P: n/a
problem: define functions F(char), g(char&) & h(const char&). call
them with arguments 'a', 49, 3300, c, uc & sc where c is a char, uc is
unsigned char & sc is signed char. whihc calls are legal? which calls
cause the compiler to to introduce a temporary variable?

solution: this is the code
-----------------------------------------------------------
#include <iostream>

void f(char) {};
void g(char&){};
void h(const char&) {};

int main() {
char c;
unsigned char uc;
signed char sc;

f(c);
f(uc);
f(sc);
f('a');
f(49);
f(3300);
}
-----------------------------------------------------------

& the error is:

------------------------------------------------------------------------------
[arnuld@localhost cpp]$ g++ 05_ex-06.cc

05_ex-06.cc: In function 'int main()':
05_ex-06.cc:20: warning: overflow in implicit constant conversion

[arnuld@localhost cpp]$
---------------------------------------------------------------------------------

"f" accepts "char" & i know 'char' belongs to 'integeral types' as
'char' will be converted to an 'int'. it means 'f(49)' will be called
with whatever character 49 presents in ASCII table. am i right?

2nd, the error belongs to 'f(3300)' as 3300 is larger than one byte &
compiler expected an 'int' not larger than the size of 'char'. right?

Nov 8 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
VJ
arnuld wrote:
>
------------------------------------------------------------------------------
[arnuld@localhost cpp]$ g++ 05_ex-06.cc

05_ex-06.cc: In function 'int main()':
05_ex-06.cc:20: warning: overflow in implicit constant conversion

[arnuld@localhost cpp]$
I do not get this warning
---------------------------------------------------------------------------------

"f" accepts "char" & i know 'char' belongs to 'integeral types' as
'char' will be converted to an 'int'. it means 'f(49)' will be called
with whatever character 49 presents in ASCII table. am i right?

2nd, the error belongs to 'f(3300)' as 3300 is larger than one byte &
compiler expected an 'int' not larger than the size of 'char'. right?
You can treat char as 8-bit integer value, and since 3300 is an int
larger then 256, it will be implicitly converted to char (I think it is
platform dependent whether char is signed char or unsigned char)
Nov 8 '06 #2

P: n/a
VJ wrote:
arnuld wrote:

------------------------------------------------------------------------------
[arnuld@localhost cpp]$ g++ 05_ex-06.cc

05_ex-06.cc: In function 'int main()':
05_ex-06.cc:20: warning: overflow in implicit constant conversion

[arnuld@localhost cpp]$

I do not get this warning
:-|

You can treat char as 8-bit integer value, and since 3300 is an int
larger then 256, it will be implicitly converted to char
in my case it is not converting.
I think it is platform dependent whether char is signed char or unsigned char
yes you are right. Stroustrup says the same. (see page 74, section 4.3)

thanks anyway

Nov 8 '06 #3

P: n/a
* arnuld:
problem: define functions F(char), g(char&) & h(const char&). call
them with arguments 'a', 49, 3300, c, uc & sc where c is a char, uc is
unsigned char & sc is signed char. whihc calls are legal? which calls
cause the compiler to to introduce a temporary variable?

solution: this is the code
-----------------------------------------------------------
#include <iostream>

void f(char) {};
void g(char&){};
void h(const char&) {};
Semicolons. I'm not sure whether they're just superfluous or
syntactically incorrect.

int main() {
char c;
unsigned char uc;
signed char sc;

f(c);
f(uc);
f(sc);
f('a');
f(49);
f(3300);
}
-----------------------------------------------------------

& the error is:

------------------------------------------------------------------------------
[arnuld@localhost cpp]$ g++ 05_ex-06.cc

05_ex-06.cc: In function 'int main()':
05_ex-06.cc:20: warning: overflow in implicit constant conversion

[arnuld@localhost cpp]$
---------------------------------------------------------------------------------
No, at the technical level this is just a warning (although such a
warning typically indicates that the programmer has intended something
else than the code actually expresses). Apart from possibly the
semicolons, the code above is technically valid, all calls "legal". If
you do the same for g and h, however, you'll then have calls that the
compiler should not accept.

"f" accepts "char" & i know 'char' belongs to 'integeral types' as
'char' will be converted to an 'int'. it means 'f(49)' will be called
with whatever character 49 presents in ASCII table. am i right?
In some coded character set, yes. If that's ASCII or some ASCII
extension on your computer, it will be ASCII.

2nd, the error belongs to 'f(3300)' as 3300 is larger than one byte &
compiler expected an 'int' not larger than the size of 'char'. right?
No, although in practice that will often signify a /logical error/, that
the program does something else than intended.

The result depends on the signedness of 'char' for your implementation
and compiler switches.

Conversion of an integral value v of any type to unsigned type T is
defined by the standard; the result is v mod 2^n where n is the number
of value representation bits in T. Conversion to signed T is
implementation defined if the value v is not representable as a T.

---

To answer Stroustrup's question about which calls are "legal", all you
have to do is to try the same with g and h, assuming a reasonably
standard-conforming compiler. But of course it's a good idea to also
understand why. To answer Stroustrup's question about which calls
introduce a temporary variable, you have to analyze things, essentially,
answering the question "why would a temporary variable be /necessary/".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 8 '06 #4

P: n/a
* arnuld:
VJ wrote:
>arnuld wrote:
>>------------------------------------------------------------------------------
[arnuld@localhost cpp]$ g++ 05_ex-06.cc

05_ex-06.cc: In function 'int main()':
05_ex-06.cc:20: warning: overflow in implicit constant conversion

[arnuld@localhost cpp]$
I do not get this warning

:-|

>You can treat char as 8-bit integer value, and since 3300 is an int
larger then 256, it will be implicitly converted to char

in my case it is not converting.
It is.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 8 '06 #5

P: n/a
void f(char) {};
void g(char&){};
void h(const char&) {};

Semicolons. I'm not sure whether they're just superfluous or
syntactically incorrect.
i have removed them but code works fine with them. quite starange.

No, at the technical level this is just a warning (although such a
warning typically indicates that the programmer has intended something
else than the code actually expresses). Apart from possibly the
semicolons, the code above is technically valid, all calls "legal". If
you do the same for g and h, however, you'll then have calls that the
compiler should not accept.
I did same with g & h & i put the problems i faced in a seperate
thread.

Conversion of an integral value v of any type to unsigned type T is
defined by the standard; the result is v mod 2^n where n is the number
of value representation bits in T. Conversion to signed T is
implementation defined if the value v is not representable as a T.
out of my head.

To answer Stroustrup's question about which calls are "legal", all you
have to do is to try the same with g and h, assuming a reasonably
standard-conforming compiler. But of course it's a good idea to also
understand why. To answer Stroustrup's question about which calls
introduce a temporary variable, you have to analyze things, essentially,
answering the question "why would a temporary variable be /necessary/".
i think "temp" will be necessary in case of "h" only. i know that since
i read section 5.5 BUT still i am not able to understand why Stroustrup
brought in the slightly-complex concept of "temp" for "const". section
5.5 does not make it clear enough.

Alf, tell me one thing, i am still going through inner turmoil. after
all of this converstaion, do you really think i am talented/qualified
enough to do C++ & authors like Stroustrup?

thanks

Nov 8 '06 #6

P: n/a
* arnuld:
>
Alf, tell me one thing, i am still going through inner turmoil. after
all of this converstaion, do you really think i am talented/qualified
enough to do C++ & authors like Stroustrup?
Nobody can say that in advance, but you're on the right track by (1)
taking it slowly / not expecting to master it all in some days, (2)
doing the exercises, and (3) asking about what you don't understand.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 8 '06 #7

P: n/a
Nobody can say that in advance, but you're on the right track by (1)
taking it slowly / not expecting to master it all in some days, (2)
doing the exercises, and (3) asking about what you don't understand.
i learnt one thing here at /comp.lang.c++/: "dont try to learn all of
C++ at once, learn it a little at a time. so 2 days ago i decided to
learn /procedural c++ paradigm/ first & not to touch OO paradigm before
that.

i mentioned it anyway.

Nov 8 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.