Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QDebug "breaks" outputting a char with value 0?

    General and Desktop
    3
    4
    1098
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • U
      utcenter last edited by

      Care to guess what would the output of this code be?

      @ char c = 0;
      for (int i = 0; i < 4; ++i) qDebug() << c << i << endl;@

      A hint - there is no output... as of why, it will be nice if someone briefly explains the internals of qDebug and what is the reason for this behavior...

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Just a quick thinking: c becomes the ascii character 0 which is invisible ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • U
          utcenter last edited by

          Yes, if you use std::cout or QTextStream you will get an invisible character for c, but you will also get i and endl, while with qDebug the entire chain is terminated when the 0 char is reached so an invisible character is not being output, nor any of the stuff that follows.

          1 Reply Last reply Reply Quote 0
          • D
            dbzhang800 last edited by

            First, QTextStream is used to generate a QString.
            Then, this string get output by following line.

            @
            fprintf(stderr, "%s", logMessage.toLocal8Bit().constData())
            @

            Consider that the first Char of the logMessage is '\0', so the length of the C string const char * will be zero. Though the length of the QString is not zero.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post