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 135127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:44:15+00:00 2026-05-11T06:44:15+00:00

I am playing with the printf and the idea to write a my_printf(…) that

  • 0

I am playing with the printf and the idea to write a my_printf(…) that calls the normal printf and a sprintf that sends the result to a special function. (I was thinking about sprintf since that behaves just like printf on most platforms).

My idea was to write a small macro that did this:

 #define my_printf(X, Y...) do{ printf(X, ## Y); \     char* data = malloc(strlen(X)*sizeof(char)); \     sprintf(data, X, ## Y); \     other_print(data);\     free(data);}while(0) 

But since sprintf can expand the string to a much bigger size than X, this method breaks almost directly.

And just to add a number do the malloc seems to be the wrong way to attack the problem, since then I would just move the problem into the future and a day when I want print a big expression…

Does anyone has a better idea on how to attack this problem? Or how do I know how big the sprintf result will be?

Thanks Johan


Update: I forgot that printf returns how many chars it prints, and since I already is calling printf in the macro it was a very easy thing to add a int that saves the number.

 #define buf_printf(X, Y...) do{ int len = printf(X, ## Y); \     char* data = malloc((len+1)*sizeof(char)); \     sprintf(data, X, ## Y); \     other_print(data);\     free(data);}while(0) 

Update: I was thinking about this and maybe to use a normal function that looks a lot like what ephemient has suggested is a good idea. The key there seems to be the v-version of the different printf functions (vprintf, vsprintf and vsnprintf). Thanks for pointing that out.

Thanks again Johan

  • 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. 2026-05-11T06:44:16+00:00Added an answer on May 11, 2026 at 6:44 am

    The best way to do this is with varargs. Create a function with the same prototype as printf() and use the varargs functions to pass data to sprintf to populate the desired buffer, the also pass that buffer to printf(‘%s’) before returning.

    A lot of the early implementations had a 4K limit on the lowest level printf() call but I would opt for more than that. You probably need to just set an upper limit and stick to it.

    One trick we used in a logging system was to write the data using printf() to a /dev/null handle. Since printf() returns the number of characters written, we then used that to allocate a buffer. But that wasn’t very efficient since it involved calling a printf()-type function twice.

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

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If it's two-way, it's not really a 'hash'. It's encryption… May 11, 2026 at 11:57 am
  • added an answer Yep -- the C++ standard leaves this stuff up to… May 11, 2026 at 11:57 am
  • added an answer Have a look at the Maven Guide for Hibernate Core… May 11, 2026 at 11:57 am

Related Questions

I am playing with the new stuff of C#3.0 and I have this code
I am currently playing around with the HybridSessionBuilder class found on Jeffrey Palermo's blog
I am currently playing around with the Asp.Net mvc framework and loving it compared
I am playing with Google App Engine and Python and I cannot list the
I am playing around with ASP.NET MVC for the first time, so I apologize
Hallo i am currently playing around with castle projects ActiveRecord and the remoting facility.
I've just started playing with Django and am loosely following the tutorial with my
I am playing with Microsoft's TreeView control and I am trying to force a
I am playing with a CAD application using MFC. I was thinking it would
I am playing around with MVC and have started setting up an existing site

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.