I am taking a class in C and I am having a problem bringing an int through scanf. I have to create a function that will check a date in this format (MMDDYY). So I have set up a scanf for the users input and then I bring it into the function.
It works fine unless the date starts with a 0. For example if the date I input is 022804 then when I printf thedate in the function it just outputs 18. I realize what is happening here is that it is reading the date as an octal notation because it starts with a 0, but how do I get it to stop doing that and read it as an integer?

You forgot to post the most important part -- the actual code. I take it you are not using "%2d%2d%2d" as a format specifier?

You forgot to post the most important part -- the actual code. I take it you are not using "%2d%2d%2d" as a format specifier?

Here is the code

int is_date(int thedate){
		
	int day; 				/* Local variable - two digit day	*/
	int is_leap_year = 0;	/* Local variable - Flag 			*/
	int month;				/* Local variable - two digit month */
	int year;				/* Local variable - two digit year 	*/
	int tmp;
	printf(" thedate = %i \n",thedate);
	month = thedate / 10000;
	
	tmp = thedate - (month * 10000); 	/* i.e. 120504: result 504 */
	
	day = tmp / 100;				/* i.e. 504: result 5  */
	year = tmp % 100;				/* i.e. 504; result 04 */
	
	
	printf(" month = %i \n",month);
	printf(" day = %i \n",day);
	printf(" year = %i \n",year);
	
	/* Check if the day is greater than 31*/
	if (day > 31) {
		printf(" day = %i \n",day);
		return (0);
	}//end if
	
	/* Check if the month is greater than 12 */
	if (month > 12) {
		printf(" month = %i \n",month);
		return (0);
	}//end if
	
	/* determine if it is leap year */
	if (year % 4 == 0 && year % 100 !=0 || year % 400 == 0){
		printf(" year = %i \n",year);
		is_leap_year = 1;
	}
		
	if (is_leap_year) {
		int days_per_month[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 }; 
	}else{
		int days_per_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	}//end if
	
	/* Determine if there is the correct amount of days for the month */
	if (day > days_per_month[month-1]) {
		return (0);
	}//end if
	
	
	return (1);
}

main(){
	int thedate;

        printf("Please enter a date MMDDYY:\n");
        scanf("%li\n", &thedate);

        if(is_date(thedate)) {
                printf("It is a good date \n");
        }else{
                printf("It is NOT a good date \n");
        }//end if


}
#include <stdio.h>

int main(void)
{
   const char text[] = "022804";
   int day, month, year;
   if ( sscanf(text, "%2d%2d%2d", &month, &day, &year) == 3 )
   {
      printf("month = %d, day = %d, year = %d\n", month, day, year);
   }
   return 0;
}

/* my output
month = 2, day = 28, year = 4
*/
This question has already been answered. Start a new discussion instead.