C: Assigning string variable using precision specifiers like printf -


i want assign string variable using precision specifiers printf uses , without using ton of loops. code passed in date command line in form of yyyymmdd. print date in mm/dd/yyyy format, following:

char *date = argv[2]; printf("%.2s/%.2s/%.4s", &date[4], &date[6], date); 

so passing '20130725' command line print '07/25/2013'

however, not work if try:

char *formatted_date = ("%.2s/%.2s/%.4s", &date[4], &date[6], date); printf("%s\n", formatted_date); 

passing '20130725' command line print '20130725' back.

how assign variable in way similar this, or not possible in c?

you can't @ initialization time, can use sprintf(3):

char formatted_date[11]; // mm/dd/yyyy plus null terminator sprintf(formatted_date, "%.2s/%.2s/%.4s", &date[4], &date[6], date); printf("%s\n", formatted_date); 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -