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

  • SEARCH
  • Home
  • 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 573881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:46:17+00:00 2026-05-13T13:46:17+00:00

How can I print a non-null-terminated string using printf, assuming that I know the

  • 0

How can I print a non-null-terminated string using printf, assuming that I know the length of the string at runtime?

  • 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-13T13:46:18+00:00Added an answer on May 13, 2026 at 1:46 pm
    printf("%.*s", length, string);
    

    Use together with other args:

    printf("integer=%d, string=%.*s, number=%f", integer, length, string, number);
    //                         ^^^^                       ^^^^^^^^^^^^^^
    

    In C you could specify the maximum length to output with the %.123s format. This means the output length is at most 123 chars. The 123 could be replaced by *, so that the length will be taken from the argument of printf instead of hard-coded.

    Note that this assumes the string does not contain any interior null bytes (\0), as %.123s only constrains the maximum length not the exact length, and strings are still treated as null-terminated.

    If you want to print a non-null-terminated string with interior null, you cannot use a single printf. Use fwrite instead:

    fwrite(string, 1, length, stdout);
    

    See @M.S.Dousti’s answer for detailed explanation.

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

Sidebar

Related Questions

I'm trying to create a CSV file using php. How can I print non
How can I print a plain list of all files that were part of
I know I can redirect awk's print output to another file from within a
As a simple example, I want to write a CLI script which can print
Can i print out a url /admin/manage/products/add of a certain view in a template?
How can I print a message to the error console, preferably including a variable?
How can I elegantly print the date in RFC822 format in Perl?
Can a Java Applet able to print out text/html easily to standard printer driver(s)
How can I get line numbers to print in Visual Studio 2005 when printing
In python you can use a tuple in a formatted print statement and the

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.