1

I was wondering if there's a way to store multiple variable data in a string. I am trying to store the date, month and year that I am taking from user as an input to store in a single string/array.

scanf("%d/%d/%d",&getDate.dd,&getDate.mm,&getDate.yyyy);

Assuming that the value entered above is a valid input, How can I store getDate.dd, getDate.mm, getDate.yyyy in a single string/array in a DD-MM-YYYY format?

4
  • if you want a string, store it in a string. What is the question? Commented Jun 9, 2020 at 11:18
  • Oh, and if you're asking what format specifier to use, better to re-read a C book. Commented Jun 9, 2020 at 11:19
  • Does this answer your question? How can I store multiple variable values into a single array or list? Commented Jun 9, 2020 at 11:19
  • 1
    @Peter The link you gave is for a Java solution. Commented Jun 9, 2020 at 11:23

1 Answer 1

0

You can use the sprintf function to write data to a character string:

//...
char dataString[11]; // Enough space for DD-MM-YYYY plus the required nul-terminator
sprintf(dateString, "%02d-%02d-%04d", getDate.dd, getDate.mm, getDate.yyyy);

The %02d format specifies that 2 digits should be printed, adding a leading zero if the value is < 10.

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.