Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cout not printing number

Issue

I'm getting no output from a simple cout, whereas a printf will always print the number:

std::cout << variableuint8;  // prints nothing
printf("%u", variableuint8); // prints the number

I've never run into this behavior before, and while I have a work around I would like to understand it.

Context:

Yay, pointers. So it may be a little more involved than as stated. Here's the context - I don't think it should matter, by the time cout and printf get the info it's dereferenced to a simple unsigned char. This is probably more information than needed to resolve the issue, but I wanted to be complete on the off chance that it was relevant.

typedef unsigned char   UInt8;
typedef unsigned short  UInt16;

typedef struct{
   UInt8   len;
   // some other definitions. sizeof(DeviceNotification) <= 8
}DeviceNotification;

UInt8 somerandomdata[8];
DeviceNotification * notification;

notification = (DeviceNotification *) somerandomdata;

std::cout << "Len: (" << notification->len << ")\n";
printf("Len: (%u)\n", notification->len);

The random data is initialized elsewhere. For this output, the first byte in the array is 0x08:

Output:

Len: ()
Len: (8)

Environment

  • Development machine:
    • OS X 10.6.8
    • Compiler LLVM 1.7
    • Xcode 3.2.6
  • Test machine:
    • OS X 10.6.8
like image 961
Adam Davis Avatar asked Nov 09 '11 13:11

Adam Davis


People also ask

Why is cout not working in C++?

In a windowing system, the std::cout may not be implemented because there are windows and the OS doesn't know which one of your windows to output to. never ever give cout NULL. it will stop to work.

How do you cout an int?

Syntax: cout << variableOfXType; where << is the insertion operator. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator (<<). For an integer value, the X is replaced with type int.

How do you make cout not scientific notation?

You can use the function fixed in this way : std::cout << std::fixed << ... Show activity on this post. You can also use printf in c++ to print in the value without scientific notation.

Can I use cout instead of printf?

The main difference is that printf() is used to send formated string to the standard output, while cout doesn't let you do the same, if you are doing some program serious, you should be using printf(). As pointed out above, printf is a function, cout an object.


1 Answers

It's printing the char as a character.

#include <iostream>

int main() {
        unsigned char c = 0x41;
        std::cout << c << std::endl;
        return 0;
}

This prints 'A' to the stdout.

Although in that case, your code should print Len: (*), and I did have verified it prints Len: (*).


Edit: Since in character encodings like ASCII or UTF-8 your console is likely using, the character corresponding to 8 (Backspace) is not visible, it will look like it's not printed.

(In some cases it may cause the previous character (the () to disappear because it is a backspace character. In DOS it may show ◘)

like image 91
kennytm Avatar answered Oct 24 '22 07:10

kennytm