Stroustrup 5.9 exercise 6

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&) {};

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?
 
V

VJ

arnuld said:
------------------------------------------------------------------------------
[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)
 
A

arnuld

VJ said:
arnuld said:
------------------------------------------------------------------------------
[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
 
A

Alf P. Steinbach

* 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

Alf P. Steinbach

* arnuld:
VJ said:
arnuld said:
------------------------------------------------------------------------------
[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

arnuld

void f(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
 
A

Alf P. Steinbach

* 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

arnuld

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.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top