Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 10 months ago.
Why does he get only 3 characters when I want four? Serial, Buffer, gets
Code:
include the mbed library with this snippet
#include "mbed.h" DigitalOut myled(LED1); Serial pc(USBTX, USBRX); int main() { char c; char buffer[128]; pc.gets(buffer, 4); //here I write that I want 4 characters; but I only can enter 3 and get 3 when I write 4 pc.printf("I got '%s'\n", buffer); }
Question:
Why does it gives me the characters back after I enter 3 characters? In the code was writen that I want enter 4 characters and get back 4 characters. When I write 100. I can enter 99 before I get 99 back. Whats the problem?
1 Answer
7 years, 10 months ago.
It's allocating one character to be the null character at the end of the string so if you want four(4) characters use gets(buffer, 5).