11 years, 1 month ago.

cout Where do the characters go?

I have made a little program that prints to a terminal program (YAT). I use printf for that. Also have a char LCD that i can send to by printf. Now I tried to do it by cout. The program compiles successfully. It prints "gebruik de toetsen 1-8 voor LED 1-4 aan en uit te zetten\n". But nowhere i can see "qwertyuiop".

So where has de "qwertyuiop" been send to????

[Edit: I thought that it might have to do with namespaces. So I tried: std::cout. It compiles too, but no "qwertyuiop" print is found by me.]

#include "mbed.h"
#include "TextLCD.h"
#include <iostream>

Serial pc(USBTX, USBRX); // tx, rx
PwmOut L1(LED1);
PwmOut L2(LED2);
PwmOut L3(LED3);
PwmOut L4(LED4);

TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7

float brightness = 0.0;

int main() {
    pc.printf("gebruik de toetsen 1-8 voor LED 1-4 aan en uit te zetten\n");
    cout << " qwertyuiop";

2 Answers

11 years, 1 month ago.

I could help myself! The "web" learns: http://stackoverflow.com/questions/14858262/stdcout-wont-print?rq=1

Adding: std::endl now also prints "qwertyuiop".

So cout goes to the terminal, just like pc.printf.

I notice that this is not the complete solution, because some parts are double written when I repeatedly start the program. Also unreadable characters appear. I think the buffer is not flushed well, and is partly overwritten. Possibly because pc.prinf and cout do not know from each other that they go to the same buffer?

#include "mbed.h"
#include "TextLCD.h"
#include <iostream>

Serial pc(USBTX, USBRX); // tx, rx
PwmOut L1(LED1);
PwmOut L2(LED2);
PwmOut L3(LED3);
PwmOut L4(LED4);

TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7

float brightness = 0.0;

int main() {
    pc.printf("gebruik de toetsen 1-8 voor LED 1-4 aan en uit te zetten\n");
    std::cout << " qwertyuiop" << std::endl;

Accepted Answer
11 years, 1 month ago.

Something else to take into account: Cout (with iostream) requires a ton of resources. Sticking to printf is generally a better idea. If you click on build details with that code it probably uses something like 25% of your ram memory, while with printf it uses 1%.

When I started with mbed I had doubts because it was all cpp. What was going on under the hood was all hidden for me. Now the internals are opened my opinion has changed and I am exited to (learn to) use mbed with cpp. I know that cpp's iostream takes more space then printf. I just wanted to test if and how it works. I like to learn cpp because it has very powerfull libraries. I think using cpp a program (system) can get a more solid structure. My idea is that In the end cpp may well pay out.

posted by Geert Hospers 11 Mar 2013

Although everything I write on the mbed is OO C++, this compiles to be (almost) as efficient as C, but the extra memory and processor cycles used by the stl libraries like iostream and string will never be worth it. I miss a few things but printf, in my opinion, has a much better interface then cout.

std::cout << "This hex value: " << std::setfill('0') << std::setw(2) << std::hex << value << " is from register A of " << device_id << std::endl;

printf("This hex value: %02X is from register A of device: %d\r\n", value, device_id);
posted by Chris Pepper 11 Mar 2013