• §

    Jonas Altrock ew20b126@technikum-wien.at

    To overview.

    The whole source file u2_date.c.

    /* Compile with `clang -std=c99 -Wall -o u2_date u2_date.c` */
    /* Test with `../checkproject u2_date` */
  • §

    Include C-libraries for input/output.

    #include <stdio.h>
    #include <stdlib.h>
  • §

    Exercise 2: Input/Output

  • §

    Read a date in US-format (m/d/yyyy) and print it in ISO 8601 format (yyyy-mm-dd).

    Details

  • §

    Read a date in the given format. You may always assume that the entered format is correct. Print it then in the format yyyy-mm-dd. The month and the day of your output must always consist of two digits (see example).

    Example

  • §

    Input:

    8/12/2016
    

    Output:

    2016-08-12
    

    main()

  • §
    int main() {
  • §

    Define three variables, for the month, day, and year.

        int mm, dd, yyyy;
  • §

    scanf will read from the terminal until the user presses the enter key. We don’t check for any error conditions, as the exercise description said we can assume the input will always be well-formed.

        scanf("%d/%d/%d", &mm, &dd, &yyyy);
  • §

    printf takes various format codes to print our variables to the terminal. The format %0nd means to fix the output width to n numbers, with 0 as the padding digit.

        printf("%04d-%02d-%02d", yyyy, mm, dd);
  • §

    Return code 0 means everything OK, no error.

        return 0;
    }