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?
irish
0
Newbie Poster
Recommended Answers
Jump to PostYou forgot to post the most important part -- the actual code. I take it you are not using "%2d%2d%2d" as a format specifier?
Jump to Post#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, …
All 5 Replies
Dave Sinkula
2,398
long time no c
Team Colleague
irish
0
Newbie Poster
Dave Sinkula
2,398
long time no c
Team Colleague
irish
0
Newbie Poster
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.