Printing out relative date from current time Here is some code of mine that prints out the relative date according to the current time:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
int main (int argc, char *argv[])
{
  int i = 1;
  struct tm date = {0};
  char relativeDays[80];

  char* temp = strtok(argv[1], "/");
  while(temp != 0)
  {
    switch(i++)
    {
                case 1:
                        date.tm_mon  = atoi(temp) - 1;
                        break;
                case 2:
                        date.tm_mday = atoi(temp);
                        break;
                case 3:
                        date.tm_year = atoi(temp) - 1900;
    }
    temp=strtok(NULL, "/");
  }
  i = (int) difftime(time(NULL), mktime(&amp;date))/86400;
  sprintf(relativeDays, "%d", abs(i));
  if (i &gt; 0) printf("%s\n", strcat(relativeDays, " days ago."));
  else if (i &lt; 0) printf("%s\n", strcat(relativeDays, " days from now."));
  else printf("Today\n");
  return 0;
}




Sample input and output:




$ ./date 12/8/2013
2 days ago.
$ ./date 12/10/2013
Today
$ ./date 12/24/2013
13 days from now.



Any thoughts on how to improve the code, specifically how to make it shorter?
I've commented your code: 

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;

int main(int argc, char *argv[])
{
    int i = 1;
    struct tm date = {0};
    char relativeDays[80];


Check For Proper Input

    if(argc != 2)
    {
        printf("Two arguments required!\n");
        return -1;
    }

    char *temp = strtok(argv[1], "/");


temp will be NULL if empty.  Also, add a break after case 3: for consistency.  If you think you'll need to handle unexpected cases, use default at the end.

    while(temp != NULL)
    {
        switch(i++)
        {
            case 1:
                date.tm_mon  = atoi(temp) - 1;
                break;
            case 2:
                date.tm_mday = atoi(temp);
                break;
            case 3:
                date.tm_year = atoi(temp) - 1900;
                break;
            default:
        }


Be consistent with spacing.

        temp = strtok(NULL, "/");
    }


Add comments so people know what your intentions are.  Why are you dividing by 86400?

    i = (int)difftime(time(NULL), mktime(&amp;date)) / 86400;
    sprintf(relativeDays, "%d", abs(i));


Use spaces to make everything more readable.

    if(i &gt; 0)
        printf("%s\n", strcat(relativeDays, " days ago."));
    else if(i &lt; 0)
        printf("%s\n", strcat(relativeDays, " days from now."));
    else
        printf("Today\n");

    return 0;
}


Edit:

This while block can be changed to the following. Though it doesn't change the operation, it may improve the aesthetics. 

while(temp != NULL)
{
    int num = atoi(temp);    
    switch(i++)
    {
        case 1:
            date.tm_mon = num - 1;
            break;
        case 2:
            date.tm_mday = num;
            break;
        case 3:
            date.tm_year = num - 1900;
            break;
        default:
            printf("Unexpected input. Blowing up now!\n");
            break;
    }

    temp = strtok(NULL, "/");
}