Search code examples
cdateformat-specifiers

What format specifier do I use in C for dates?


This is my code (I know using %d is wrong but I'm not sure what I am supposed to use):

#include <stdio.h>
#include <stdlib.h>
int main()
{
char charactername[] = "Ruby";
int age =18;
printf("Once upon a time there was girl named %s\n",charactername);
printf("%s was %d years old\n",charactername,age);

age =19;
int birthday = 22/07/2003;

printf("on %d she was born\n",birthday);
printf("On 22/07/2022 she will become %d",age);

return 0;
}

This is what the terminal gives me:

Once upon a time there was girl named Ruby

Ruby was 18 years old

on 0 she was born

On 22/07/2022 she will become 19


Solution

  • There is no "date" type built in to C. You can use strings for arbitray text; something like:

    const char *birthday = "22/07/2003";
    

    which you can print with a %s in the printf format

    printf("on %s she was born\n",birthday);