0

So I want to do something like printf("%s", '\t'*3); Im just wondering if there is a way to print something like that without looping

user1686896
  • 483
  • 1
  • 5
  • 9

2 Answers2

0

printf("%s%s%s", "\t", "\t", "\t"); ? Just kidding. But since you have tagged this as C++, and since I don't know what higher-level problem you are trying to solve here, have you considered just using the appropriate std::string constructor?

std::string s(3, '\t');

You can even use it with printf if you really insist...

printf("%s", std::string(3, '\t').c_str());

But why not just use std::cout?

std::cout << std::string(3, '\t');

About the "without looping" part... both printf and std::string may have loops in their implementations, of course. But that should not bother you.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
  • `printf("%s", std::string(3, '\t').c_str());` will not work. The string object is destructed after the call to `c_str()`. The pointer returned will point to invalid memory. – NickC Mar 28 '14 at 19:59
  • @NickC: How so? The object is only destructed after `printf` returns. – Christian Hackl Mar 28 '14 at 20:06
  • The string object is an R-Value that hasn't been captured by a variable therefor the objects destructor can be called after the call to `c_str()` has returned. The value return from `c_str()` is only a `const char*` not a string reference. The lifetime of the string object has ended prior to the printf call starting. I have seen this burn more than a few developers. Sometimes the destructor isn't called in debug builds and only in release builds. – NickC Mar 28 '14 at 20:21
  • The string object will be destructed at the end of the *full* expression, and that includes the printf. In other words, the lifetime of the temporary string object is extended. There are a lot of past Stackoverflow questions and answers on this, for example http://stackoverflow.com/questions/10006891/stdstringc-str-and-temporaries – Christian Hackl Mar 28 '14 at 20:29
  • Thanks for enlightening me. Now you have me thinking what conditions I had that caused me to modify code in the past. I know that I have seen VC++ release builds fail to retrieve a string contents when passed in as an argument like this. I wish I could look at the code again to see what was going wrong. Know of any VC++ optimization setting that didn't follow the standard? – NickC Mar 28 '14 at 20:53
  • @NickC: You're welcome. I guess with pre-standard (i.e. pre-1998) versions of VC++, the problem you describe here may indeed have occurred. I don't know of any optimization settings in current VC versions which might cause such behaviour... – Christian Hackl Mar 28 '14 at 22:03
0

assuming that you ment runtime loops and are allowed to use boost, you could create the strings at compile time using templates:

#include <iostream>
#include <boost/mpl/char.hpp>
#include <boost/mpl/string.hpp>

using namespace boost;

template <unsigned int C, int R>
struct repchrn
{
    typedef typename mpl::push_back<typename repchrn<C, R - 1>::string, mpl::char_<C>>::type string;
    static const char* getString() { return mpl::c_str<string>::value; }
};
template <unsigned int C>
struct repchrn<C, 0>
{
    typedef mpl::string<> string;
    static const char* getString() { return mpl::c_str<string>::value; }
};


int main() {
    printf("%s", repchrn<'a', 5>::getString());

    /* or */ std::cout << std::endl;

    std::cout << repchrn<'a', 5>::getString();
}
wonko realtime
  • 545
  • 9
  • 26