0

I have these variables:

char* name = "bobsux";    
int score = 100;
char* scoreHash = "f899139df5e1059396431415e770c6dd";

Printing these using format specifiers and printf, returns this:

printf("name=%s&score=%d&score_hash=%s", name, score, scoreHash);
=> name=bobsux&score=100&score_hash=f899139df5e1059396431415e770c6dd

How can I make a variable that returns the same? Failed attempt:

char* scoreData = ("name=%s&score=%d&score_hash=%s", name, score, scoreHash);
=> f899139df5e1059396431415e770c6dd

I want the variable to be like the printf return, so that I can send scoreData to a web server:

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, scoreData);

1 Answer 1

5

You use snprintf() to format the text into a character array:

char scoreData[128];

snprintf(scoreData, sizeof scoreData, "name=%s&score=%d&score_hash=%s\n",
         name, score, scoreHash);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.