473,624 Members | 2,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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@localhos t 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@localhos t 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 2343
VJ
arnuld wrote:
>
------------------------------------------------------------------------------
[arnuld@localhos t 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@localhos t 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@localhos t 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@localhos t 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@localhos t 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@localhos t 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@localhos t 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@localhos t 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
3136
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 had done myself a disservice by having not attempted and completed the exercises. I intend to rectify that. My current routine is to read a chapter and then attempt every exercise problem, and I find that this process is leading to a greater...
0
1746
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 opposite as an exercise which is writing it as a "declaration without definition". please check whether i am right or wrong:
0
1749
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 // printing the sizes of different types
2
5331
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 -------------- /* Stroustrup 3e, section 4.11, exercise 5 STATEMENT:
16
2361
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 the types: unsigned char
11
1826
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 for names of months and an array of numbers for number of days. 2.) using an array of structures. each structure holds the name of the
6
3037
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 table of the name sof months o fyear and the number of days in each month. write out that table. Do this twice:
14
2464
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: Read a sequence of words from the input. use "quit" as the word
27
2436
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 STATEMENT: Read a sequence of words from the input. use "quit" as the word to terminate the input. Print the words in the order they were entered. don't print a word twice.modify the programme to sort the
0
8246
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8631
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
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
8490
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
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
5570
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
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1489
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.