QDebug "breaks" outputting a char with value 0?



  • 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...


  • Lifetime Qt Champion

    Hi,

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



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.