5

Normally you can print a string in C like this..

printf("No record with name %s found\n", inputString); 

But I wanted to make a string out of it, how I can do it? I am looking for something like this..

char *str = ("No record with name %s found\n", inputString); 

I hope it is clear what I am looking for...

2
  • 3
    Do yourself a favor & go buy "The C Programming Language" by Kernigham & Ritchie. Commented Nov 20, 2009 at 16:48
  • eskimo.com/~scs/cclass/cclass.html Commented Nov 20, 2009 at 17:32

5 Answers 5

30

One option would be to use sprintf, which works just like printf but takes as its first parameter a pointer to the buffer into which it should place the resulting string.

It is preferable to use snprintf, which takes an additional parameter containing the length of the buffer to prevent buffer overruns. For example:

char buffer[1024]; snprintf(buffer, 1024, "No record with name %s found\n", inputString); 
Sign up to request clarification or add additional context in comments.

Comments

10

You're looking for the sprintf family of functions. Their general format is:

char output[80]; sprintf(output, "No record with name %s found\n", inputString); 

However, sprintf by itself is extremely dangerous. It is prone to something called buffer overruns. What this means it that sprintf has no idea how big the output string you provide it is, so it will willingly write more data to it than is available. For example, this will compile cleanly, but will overwrite valid memory—and there is no way to let sprintf know that it's doing anything wrong:

char output[10]; sprintf(output, "%s", "This string is too long"); 

The solution is to use a function as snprintf, which takes a length parameter:

char output[10]; snprintf(output, sizeof output, "%s", "This string is too long, but will be truncated"); 

or, if you're on a Windows system, to use the _sntprintf variants and friends, which protect against overflowing of either the input or output strings.

2 Comments

As it's a homework question, I'd just to point out that sizeof output only gives the number of elements in a char array - the usual practice is to use: sizeof array / sizeof array[0], which works without being dependent upon the size of various types. Plus, it works for char arrays too. :)
Good point. Even that, though, can fail in some really common cases, such as passing array to a function--at which point, sizeof array is 4 or 8 on any modern system, regardless of the number and size of the elements therein. The real solution is to use std::vector or something similar and avoid the whole mess entirely.
7

Since this is homework (thanks for tagging it as such) I'll suggest you to look closely at the ...printf() family of functions.

I'm sure you'll find the solution :)

2 Comments

Well, it looks like others alredy deprived you of the pleasure of discovery :)
+1 anyway for giving the right kind of answer to a homework question.
3

Look into sprintf (see below).

int n = sprintf(str, "No record with name %s found\n", inputString); 

Comments

3

Use

sprintf(str, "No record with name %s found\n", inputString); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.