printf and cout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Heathfield

    #16
    Re: printf and cout

    Bernhard Schauer said:
    >><snip>
    >>>Actually they don't behave differently at all. In each case the int
    >>>value 400 is being interpreted as an unsigned char.
    >><snip>
    >>>
    >>Code from laikon:
    >>>int c = 400;
    >>>printf("%c ", c);
    >>>cout << int(char(400));
    >>>
    >>Where exactly do you see unsigned characters? (char != unsigned char)
    >>
    >The %c format expects (and thus treats the corresponding argument as) an
    >unsigned char.
    >
    Ok. But I hope char(400) does not convert to an unsigned char?
    Actually, char(400) converts to a syntax error.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Chris Dollin

      #17
      Re: printf and cout

      Richard Heathfield wrote:
      Bernhard Schauer said:
      >
      >>><snip>
      >>>>Actually they don't behave differently at all. In each case the int
      >>>>value 400 is being interpreted as an unsigned char.
      >>><snip>
      >>>>
      >>>Code from laikon:
      >>>>int c = 400;
      >>>>printf("%c" , c);
      >>>>cout << int(char(400));
      >>>>
      >>>Where exactly do you see unsigned characters? (char != unsigned char)
      >>>
      >>The %c format expects (and thus treats the corresponding argument as) an
      >>unsigned char.
      >>
      >Ok. But I hope char(400) does not convert to an unsigned char?
      >
      Actually, char(400) converts to a syntax error.
      But the code it appears in is, atopically, C++, and so how it behaves
      in C doesn't much matter ...

      The C equivalentish code, `(char) 400`, doesn't convert to unsigned
      char, but to char -- which may be an unsigned type, or not, at the
      implementor's whim, perhaps with an eye on performance implications
      (and mobs with torches and pitchforks).

      --
      "What would that matter, if it made a good book?" /Gaudy Night/

      Hewlett-Packard Limited registered office: Cain Road, Bracknell,
      registered no: 690597 England Berks RG12 1HN

      Comment

      • Bernhard Schauer

        #18
        Re: printf and cout

        Richard Heathfield wrote:
        Actually, char(400) converts to a syntax error.
        Sorry. Mixed up C/C++ (again). Meant (char)400 as Chris pointed out.

        --
        Bernhard Schauer
        schauer_at_crux y_dot_net

        Comment

        • Mark McIntyre

          #19
          Re: printf and cout

          Richard Heathfield wrote:
          laikon said:
          >
          >Hello, everyone:
          >>
          >this is about overflow in C and C++.
          >
          The behaviour of a C program on overflow is undefined.
          >
          >int c = 400;
          >printf("%c", c);
          >>
          >it print ? on screen,
          >
          Not necessarily. C doesn't guarantee you a screen, and it says very little
          about the character set supplied by your implementation.
          >
          >and ascii of '?' is 63.
          >
          This is irrelevant, I think. If by some chance you'd seen the character
          with code point 144 (which is, of course, *not* an ASCII character), I'd
          have been able to explain it in concrete terms. I am a little puzzled by
          the 63, actually.
          Its not printing 63, its just printing "?" to mean "I don't know how to
          represent a value outside the characterset this screen uses".

          Comment

          • pete

            #20
            Re: printf and cout

            santosh wrote:
            The %c format expects
            (and thus treats the corresponding argument as) an unsigned char.
            No standard library functions are described
            as taking an argument lower ranking than int.

            --
            pete

            Comment

            • Kenneth Brody

              #21
              Re: printf and cout

              Richard Heathfield wrote:
              >
              jacob navia said:
              [...]
              That's what I am talking about. Obviously your example
              will work.

              printf("%c",123 45678);

              will not!
              >
              It is certainly true that it is not guaranteed to work. It is not
              guaranteed *not* to work, however. On systems with CHAR_BIT >= 24, it
              /will/ work (although heaven knows what character will be printed!).
              Well, it depends on what you mean by "work". According to 7.19.6.1p8,
              on the part about the "%c" format:

              If no l length modifier is present, the int argument is converted
              to an unsigned char, and the resulting character is written.

              Is it possible to "fail" when converting an int to unsigned char? (Of
              course you can lose bits, but can it "fail"?)

              --
              +-------------------------+--------------------+-----------------------+
              | Kenneth J. Brody | www.hvcomputer.com | #include |
              | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
              +-------------------------+--------------------+-----------------------+
              Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


              Comment

              • Keith Thompson

                #22
                Re: printf and cout

                jacob navia <jacob@nospam.c omwrites:
                [...]
                Well OK
                >
                "%c" format specifier expects an int.
                Correct.
                GREAT!
                >
                I am tired of this discussion.
                Funny that you happen to get "tired" of it just at the moment when you
                realize you made a mistake.
                You are right, I was wrong
                Yes, I was right. Yes, you were wrong. But it's not such a
                huge deal. We all make mistakes. I make mistakes all the time,
                and I'm grateful when people point them out. And I usually accept
                corrections without sarcasm.
                and when everybody uses "%c" to put a character they are
                totally wrong.
                And now you're being ridiculous. This:

                char c = 'x';
                printf("%c", c);

                is perfectly valid, because the char value is promoted to int,
                which is what printf expects for "%c". (Except in the obscure
                case that plain char is unsigned and sizeof(int) == 1, but I'm
                willing to ignore that (other than this brief mention for the sake
                of completeness).) But of course you know that.

                Pointing out an error is not a personal attack. Learn that and
                you might be taken a lot more seriously around here.

                --
                Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                Nokia
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • pete

                  #23
                  Re: printf and cout

                  Keith Thompson wrote:
                  >
                  jacob navia <jacob@nospam.c omwrites:
                  [...]
                  Well OK

                  "%c" format specifier expects an int.
                  >
                  Correct.
                  >
                  GREAT!

                  I am tired of this discussion.
                  >
                  Funny that you happen to get "tired" of it just at the moment when you
                  realize you made a mistake.
                  >
                  You are right, I was wrong
                  >
                  Yes, I was right. Yes, you were wrong. But it's not such a
                  huge deal. We all make mistakes. I make mistakes all the time,
                  and I'm grateful when people point them out. And I usually accept
                  corrections without sarcasm.
                  >
                  and when everybody uses "%c" to put a character they are
                  totally wrong.
                  >
                  And now you're being ridiculous. This:
                  >
                  char c = 'x';
                  printf("%c", c);
                  >
                  is perfectly valid, because the char value is promoted to int,
                  which is what printf expects for "%c". (Except in the obscure
                  case that plain char is unsigned and sizeof(int) == 1, but I'm
                  willing to ignore that (other than this brief mention for the sake
                  of completeness).) But of course you know that.
                  >
                  Pointing out an error is not a personal attack. Learn that and
                  you might be taken a lot more seriously around here.
                  This will output the same thing
                  even though the int argument is negative:

                  printf("%c", 'x' - 1 - (unsigned char)-1);

                  except in that obscure case that you mentioned.

                  --
                  pete

                  Comment

                  • pete

                    #24
                    [ot]Re: printf and cout

                    Philip Potter wrote:
                    I especially can't understand it when
                    the correction comes from someone as calm and civil as Keith.
                    I can almost understand.

                    One of the things that I admire about Richard Heathfield
                    is his ability to make trolls come unglued.
                    Troll that are not bothered by anyone else.
                    Mostly I've seen this happen in comp.programmin g.
                    He does it "calm and civil"
                    with politeness and with with a finess
                    that is very difficult to describe or duplicate.

                    --
                    pete

                    Comment

                    • Richard Heathfield

                      #25
                      Re: [ot]Re: printf and cout

                      pete said:
                      Philip Potter wrote:
                      >
                      >I especially can't understand it when
                      >the correction comes from someone as calm and civil as Keith.
                      >
                      I can almost understand.
                      >
                      One of the things that I admire about Richard Heathfield
                      is his ability to make trolls come unglued.
                      Troll that are not bothered by anyone else.
                      Mostly I've seen this happen in comp.programmin g.
                      He does it "calm and civil"
                      with politeness and with with a finess
                      that is very difficult to describe or duplicate.
                      I wish I could claim that it's deliberate (because then I could simply stop
                      doing it) - a sort of "meta-trolling", or trolling the trolls, pushing
                      their buttons to make them react in their oh-so-predictable way.

                      But it isn't deliberate. The trigger would appear to be some facet or other
                      of my writing style, which many non-trolls do seem to find helpful and
                      clear, but which appears to drive the trolls to distraction.

                      When the trolls start providing useful articles about C here (i.e. when
                      hell freezes over, or perhaps a few months afterwards), I'll give some
                      thought to discovering ways in which I might change my style to avoid
                      "ungluing" them, as you so aptly put it. In the meantime, I'll continue
                      providing such help as I can to those who seek it in the way that seems
                      best to me, and if that means that a few bozos continue to clog up the
                      newsgroup with puerile nonsense, well, that's what killfiles are for,
                      right?

                      Some people don't killfile the trolls, because they find them amusing.
                      Obviously that's up to them. Nevertheless, I would invite such people to
                      consider the (fairly simple) challenge of writing a C program to mimic an
                      article by any of the current collection of comp.lang.c trolls. A few
                      stock phrases will be sufficient, I think. Slightly more challenging (but
                      really not *very* much more): generate random but characteristic troll
                      *exchanges*. You know the sort of thing:

                      T1: Oh. The. Irony.
                      T2: Yes, you're right.
                      T1: Too true.
                      T3: You're both so right.

                      etc.

                      To implement such a program in ISO C should take - oh, ten minutes, tops.

                      Once you've written such a program and run it a few times, you'll soon have
                      the shape of the solution space mapped out, after which it really ceases
                      to be particularly funny - at which point you may well decide to use a
                      killfile after all.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • Richard

                        #26
                        Re: [ot]Re: printf and cout

                        pete <pfiland@mindsp ring.comwrites:
                        Philip Potter wrote:
                        >
                        >I especially can't understand it when
                        >the correction comes from someone as calm and civil as Keith.
                        >
                        I can almost understand.
                        >
                        One of the things that I admire about Richard Heathfield
                        is his ability to make trolls come unglued.
                        Troll that are not bothered by anyone else.
                        Mostly I've seen this happen in comp.programmin g.
                        He does it "calm and civil"
                        with politeness and with with a finess
                        that is very difficult to describe or duplicate.
                        RH's skills in terms of C are without question. However he is pedantic
                        to the point of silliness. And that combined with his rather nasty
                        tendency to pick at people and wage warfare on one or two alternative,
                        but equally valuable, posters make him an object for ridicule rather
                        than admiration for most professional programmers who are used to
                        dealing with this type of technical ogre.

                        Comment

                        • pete

                          #27
                          Re: [ot]Re: printf and cout

                          Richard wrote:
                          >
                          pete <pfiland@mindsp ring.comwrites:
                          One of the things that I admire about Richard Heathfield
                          is his ability to make trolls come unglued.
                          RH's skills in terms of C are without question.
                          And on top of that,
                          he's one of the only seven posters to this newsgroup
                          who use the phrase "begging the question" correctly.

                          --
                          pete

                          Comment

                          • Philip Potter

                            #28
                            Re: [ot]Re: printf and cout

                            pete wrote:
                            Richard wrote:
                            >pete <pfiland@mindsp ring.comwrites:
                            >
                            >>One of the things that I admire about Richard Heathfield
                            >>is his ability to make trolls come unglued.
                            >
                            >RH's skills in terms of C are without question.
                            >
                            And on top of that,
                            he's one of the only seven posters to this newsgroup
                            who use the phrase "begging the question" correctly.
                            >
                            Which begs the question: who are the others? ;)

                            Phil

                            Comment

                            • David Thompson

                              #29
                              Re: printf and cout

                              On Fri, 28 Mar 2008 07:08:48 -0500, pete <pfiland@mindsp ring.com>
                              wrote:
                              santosh wrote:
                              >
                              The %c format expects
                              (and thus treats the corresponding argument as) an unsigned char.
                              >
                              No standard library functions are described
                              as taking an argument lower ranking than int.
                              Some take implementation-defined types which _could_ be lower.

                              size_t: *alloc, qsort/bsearch, fread/write, *snprintf/*swprintf,
                              several mem*/str*/wcs*, etc.

                              time_t: difftime

                              (va_list could be smaller than int, but all va_* are not actual
                              functions; jmp_buf must be an array type)

                              - formerly david.thompson1 || achar(64) || worldnet.att.ne t

                              Comment

                              Working...