Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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$
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
I'm curious to know why my below code didn't do the same thing for each number.

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
 

lloyddean

macrumors 65816
May 10, 2009
1,047
19
Des Moines, WA
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;
}
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
width vs. precision.

from
Code:
man 3 printf

Both give the same result in this case.

o An optional decimal digit string specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given) to fill out the field width.

o An optional precision, in the form of a period . followed by an optional digit string. If the digit string is omitted, the precision is taken as zero. This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, the number of digits to appear after the decimal-point for a, A, e, E, f, and F conversions, the maximum number of significant digits for g and G conversions, or the maximum number of characters to be printed from a string for s conversions.

`0' (zero) Zero padding. For all conversions except n, the converted value is padded on the left with zeros rather than blanks. If a precision is given with a numeric conversion (d, i, o, u, i, x, and X), the 0 flag is ignored.

diouxX The int (or appropriate variant) argument is converted to signed decimal (d and i), unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x and X) notation. The letters ``abcdef'' are used for x conversions; the letters ``ABCDEF'' are used for X conversions. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros.

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

B
 

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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!!




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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.