Stroustrup 5.9 exercise 6

Discussion in 'C++' started by arnuld, Nov 8, 2006.

  1. arnuld

    arnuld Guest

    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?
    arnuld, Nov 8, 2006
    #1
    1. Advertising

  2. arnuld

    VJ Guest

    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)
    VJ, Nov 8, 2006
    #2
    1. Advertising

  3. arnuld

    arnuld Guest

    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
    arnuld, Nov 8, 2006
    #3
  4. * 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?
    Alf P. Steinbach, Nov 8, 2006
    #4
  5. * 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?
    Alf P. Steinbach, Nov 8, 2006
    #5
  6. arnuld

    arnuld Guest

    > > 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
    arnuld, Nov 8, 2006
    #6
  7. * 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?
    Alf P. Steinbach, Nov 8, 2006
    #7
  8. arnuld

    arnuld Guest

    > 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.
    arnuld, Nov 8, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. arnuld
    Replies:
    5
    Views:
    340
    Alf P. Steinbach
    Nov 8, 2006
  2. arnuld
    Replies:
    2
    Views:
    291
    arnuld
    Nov 8, 2006
  3. arnuld
    Replies:
    6
    Views:
    382
    arnuld
    Nov 8, 2006
  4. arnuld

    Stroustrup 5.9 exercise 9

    arnuld, Nov 8, 2006, in forum: C++
    Replies:
    5
    Views:
    385
    Bernd Strieder
    Nov 9, 2006
  5. arnuld

    Stroustrup 5.9 exercise 11

    arnuld, Nov 9, 2006, in forum: C++
    Replies:
    1
    Views:
    355
    Gary Wessle
    Nov 9, 2006
Loading...

Share This Page