views:

212

answers:

4

Does there exist any other alternative data structure instead of struct tm (having same memory allocated as this structure) ? So that I could use strftime without declaring <time.h>

I am aware of the fact that relying on implicit declaration is not good,but I faced this question in an interview.

EDIT: To be precise I was asked to print the month for a corresponding integer using standard library function,but not allowed to include any header file.

+1  A: 

No, you need to use time.h include file.

However, if you really want to use strftime and compile without errors or warnings, you could redefine the struct data type in your C file, and also the function prototype to use without including that file. You could call your struct type a different name as long as it matches up with the one currently in your time.h file.

Sean A.O. Harney
I was given the scenario that user input a integer I have to print the month.I wrote:`struct tm t;char b[10];t.tm_mon=n-1,strftime(b,10,"%B",puts(b);` How to do the same without `time.h` ?
nthrgeek
Open up /usr/include/time.h and cut and paste the 'struct tm' definition and strftime prototype into your source file.You are declaring a variable of type struct tm, but you have not defined the struct data type yet!
Sean A.O. Harney
@ Sean A.O. Harney:I hope I could be allowed to do that :-) Could we do solve this without declaring <time.h> anf of-course I have to use C functions to get the month name ?
nthrgeek
Perhaps the answer they were looking for was to use alternative libc functions. I'm not sure.
Sean A.O. Harney
What other alternative library function can give such result ?
nthrgeek
@nthrgeek: I think you may have misunderstood what you were asked to do. You used `strftime`, then you were asked how to print a month without using `time.h`, right? The interviewer probably means, "how would you do it without using any of the library functions made available via time.h?", not "how would you call the functions in time.h without including it?". They probably expected a fixed 12-entry lookup table, as opposed to the locale-dependent behavior of strftime. Or maybe it was an advanced question and they wanted you to access the locale directly.
Steve Jessop
@Steve Jessop : Nopes,then a simple hash will be sufficient,he wanted a solution that will C library functions,just you can't declare it explicitly.It may be hard,but interviewer seems very confident that it is possible :-)
nthrgeek
@ Steve Jessop :I think it's the advanced version,since the lookup table solution was dismissed instantly :-)
nthrgeek
So he specifically said he wanted you to call `strftime`, and he wanted you to do it without including `time.h`? Then (a) he's crazy, and (b) the only safe way to do it is what Sean says, copy the relevant definitions from `time.h` into your program. Which basically is what `#include` does, which is why it's so crazy.
Steve Jessop
@Steve Jessop:He want me to solve the problem using standard library function,but without declaring any header file and of-course I was not allowed to copy paste either:-)
nthrgeek
I am aware of `strftime` so I used that,but he was not satisfied. :(
nthrgeek
As for accessing the locale: I don't know if POSIX provides a standard way to read it, or just set it. You might need some innards, but either way that would require some headers and so on just like strftime. Sounds like a madman: the order of the members of `struct tm` isn't defined by the standard, so there is no way to call strftime without knowledge of the contents of `time.h` for your implementation. Obviously some platform-specific bodge might work, based on happening to know the layout of struct tm but without literally copy-pasting.
Steve Jessop
Okay lets suppose it's the same layout as http://en.wikipedia.org/wiki/Time.h,that is the fifth member is the `tm_mon`, then ?
nthrgeek
That wikipedia article doesn't give a layout, it gives a list of members. That's the whole point of `time.h`, it's implementation-dependent. Implementations can give them in any order, and can add new members if they like. I don't see any difference between assuming that the struct is defined in a particular way (and repeating that in your source file), as against just c'n'p'ing it out of time.h. The latter is more reliable, but for some reason this enigmatic interviewer forbids it. Who knows what he would allow? I guess he didn't offer you any solution.
Steve Jessop
The solution is possible by using GCC extension ;)
nthrgeek
A: 

So long as you do not need to access members of struct tm you can simply use a forward declaration of it thus:

struct tm ;

But to use strftime() you'd also need a declaration of its prototype. You don't want to work anywhere where they think such dubious practices are useful.

Clifford
Well then how could I print the correct month name if I don't access the members :-) ?
nthrgeek
Err... By calling strftime(). Which already knows them. You need to declare a prototype for strftime(), but you don't need to include the header.
Clifford
A: 

Using a library function requires that you include a header file...

Printing out a month name - I'm assuming you are allowed stdio.h - is independant of whether or not you can use strftime.

#include <stdio.h>

const char * months[] = {
    "January",
    "February",
    "March",
    "April",
    "May",
    ...
    "December"
};

int main () {
    int i = 0;
    for (; i < 12; ++i)
        printf ("Month %d: %s\n", i + 1, months[i]);
    return 0;
}

I'm being exact about your edit. Using only an int you can print a month associated with it. But printing itself, as has been mentioned, requires an include of its own...

ezpz
+1  A: 

The only thoughts I have are either the interviewer expected printing month strings, ignoring locale using your own const char array of month names, or one of those ill-defined "interactive" questions where you are suppose to stop and keep asking questions to clarify what the interviewer actually wants. Explicitly you want to express that you want to know what type of answer the interviewer is looking for. For example, just a short code fragment, ignoring details like error-checking and locale or reentrant issues, or an answer for some non-standard embedded or legacy environment, looking for another Standard C Library functions (ctime??), or a platform/OS specific answer?

ObCode:

const char* months[] = { "Jan", "Feb", ..., "Dec" };
...
printf("Month: %s\n", months[i]);

Or if a wildly "lateral thinker" on a Unix/Linux system:

char str[PATH_MAX];
...
assert(i >= 0 && i < 12);
cmd = snprintf(cmd, sizeof(cmd), "cal %d 2010 | head -1", i);
FILE* pipe = popen(cmd);
fread(str, 1, sizeof(str), pipe);
printf("Month: %s\n", str);

Pure bad idea. :)

mctylr