473,320 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Stroustrup 5.9 exercise 6

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
7 2317
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
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
* 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
* 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
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
* 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I...
0
by: arnuld | last post by:
Stroustrup has a table in section 4.9 of declarations and definitions. he asks to write a similar table but in opposite sense: char ch; // declaration with definition he asks to do the...
0
by: arnuld | last post by:
this programme runs without any trouble. it took 45 minutes of typing. i posted it here so that people can save their precious time: // Stroustrup special edition // chapter 4 exercise 2 //...
2
by: arnuld | last post by:
MAX and MIN values of CHAR could not be displayed. Why ? BTW, any advice on improvement ? (please remember i have covered chapter 4 only) ------------- PROGRAMME -------------- /*...
16
by: arnuld | last post by:
i did what i could do at Best to solve this exercise and this i what i have come up with: ----------- PROGRAMME -------------- /* Stroustrup 5.9, exercise 3 STATEMENT: Use typedef to define...
11
by: arnuld | last post by:
/* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a table of the name sof months o fyear and the number of days in each month. write out that table. Do this twice: 1.) using ar array of char...
6
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a...
14
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT:...
27
by: arnuld | last post by:
it works fine without any trouble. i want to have advice on improving the code from any angle like readability, maintenance etc: ---------- PROGRAMME ------------ /* Stroustrup, 5.9, exercise 11...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.