Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build string as in printf function

Tags:

c++

string

printf

printf("%d.%d.%d", year, month, day);

Can I do the same but without printing, smth like

char* date = "%d.%d.%d", year, month, day;

Or maybe some other simple ways to do that?

like image 824
Yury Pogrebnyak Avatar asked Apr 01 '12 13:04

Yury Pogrebnyak


1 Answers

In plain c there is asprintf() which will allocate memory to hold the resulting string:

#include <stdio.h>
char *date;
asprintf(&date, "%d.%d.%d", year, month, day);

(error handling omitted)

Since you have tagged C++ you probably want to use the C++ solutions.

like image 94
Daniel Roethlisberger Avatar answered Oct 20 '22 17:10

Daniel Roethlisberger