Placeholder in C

Discussion in 'Mac Programming' started by cybrscot, Jan 2, 2011.

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    In the code below I was told that if I wanted to ensure that a single digit day in a date was formatted as say....09, rather than 9, ex. 9/2/09 instead of 9/2/9, that I should change my placeholder from %d to %02d. That worked, however only for the day. I changed the other %d to %02d in order to preserve a two digit format, such as 05 instead of 5. But the year and month part of the mm/dd/yy did not change, even with %02d, they still printed as a single digit. This isn't a problem of any kind, other than I'm curious to know why my below code didn't do the same thing for each number.

    Thanks
    Scott



    #include <stdio.h>

    main ( )
    {

    int month ;
    int day ;
    int year ;

    printf ("Enter a date (mm/dd/yy):") ;
    scanf ("%d/%d/%d", &month, &day, &year) ;
    printf ("You entered the date: %02d/%02d/%02d\n", year, month, day) ;

    return 0 ;


    Enter a date (mm/dd/yy):02/09/09
    You entered the date: 9/2/09
    Scott-Deans-MacBook-Pro:documents scottdean$
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    Works fine for me:

    Code:
    $ ./deleteme 
    Enter a date (mm/dd/yy):1/2/3
    You entered the date: 03/01/02
    
    Are you sure you compiled the last version?

    EDIT: I made two small edits to the code you posted. added int and a closing curly brace.
    Code:
    #include <stdio.h>
    
    [B]int[/B] main ( )
    {
    
    int month ;
    int day ;
    int year ;
    
    printf ("Enter a date (mm/dd/yy):") ;
    scanf ("%d/%d/%d", &month, &day, &year) ;
    printf ("You entered the date: %02d/%02d/%02d\n", year, month, day) ;
    
    return 0 ;
    [B]}[/B]
    
    B
     
  3. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int month;
        int day;
        int year;
        
        printf("Enter a date (mm/dd/yy):");
        scanf("%d/%d/%d", &month, &day, &year);
        printf("You entered the date: %.2d/%.2d/%.2d\n", year, month, day);
        
        return EXIT_SUCCESS;
    }
    
     
  4. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    width vs. precision.

    from
    Code:
    man 3 printf
    Both give the same result in this case.

    If there is a reason one is preferable over the other, I'd love to know it.

    B
     
  5. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #5
    Man, I feel like an idiot. Yeah, I hate to admit it, but I must have forgotten to compile it. I compiled it and it worked fine. I'm sure it didn't work because after I changed the code, I ran the program without compiling it again. I know better, I just forgot.
    Sorry!!




     

Share This Page