Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6834981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:10:40+00:00 2026-05-26T23:10:40+00:00

I have a buffer which I receive through a serial port. When I receive

  • 0

I have a buffer which I receive through a serial port. When I receive a certain character, I know a full line has arrived, and I want to print it with printf method. But each line has a different length value, and when I just go with:

printf("%s", buffer);

I’m printing the line plus additional chars belonging to the former line (if it was longer than the current one).

I read here that it is possible, at least in C++, to tell how much chars you want to read given a %s, but it has no examples and I don’t know how to do it in C. Any help?

I think I have three solutions:

  • printing char by char with a for loop
  • using the termination character
  • or using .*

QUESTION IS: Which one is faster? Because I’m working on a microchip PIC and I want it to happen as fast as possible

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:10:41+00:00Added an answer on May 26, 2026 at 11:10 pm

    The string you have is not null-terminated, so, printf (and any other C string function) cannot determine its length, thus it will continue to write the characters it finds there until it stumbles upon a null character that happens to be there.

    To solve your problem you can either:

    • use fwrite over stdout:

      fwrite(buffer, buffer_length, 1, stdout);
      

      This works because fwrite is not thought for printing just strings, but any kind of data, so it doesn’t look for a terminating null character, but accepts the length of the data to be written as a parameter;

    • null-terminate your buffer manually before printing:

      buffer[buffer_length]=0;
      printf("%s", buffer); /* or, slightly more efficient: fputs(buffer, stdout); */
      

      This could be a better idea if you have to do any other string processing over buffer, that will now be null-terminated and so manageable by normal C string processing functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to double buffer a custom control which contains buttons. I have tried
I have an ugly piece of Serial Port code which is very unstable. void
I have a string which has been sent using udp. Now I want to
I have a circular, statically allocated buffer in C, which I'm using as a
I'm using emacs and I have written a script which uses current-buffer. However the
I have a large set of lines, which I render from a vertex buffer
I want to have an application which parses various RSS feeds and send the
Say you have a buffer of size N which must be set to definite
I have an image in my server which I want to send to my
I have a Custom DirectShow Video Renderer Filter which has some extended features over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.