what data type can be used to get the "DATE" from the user???
say, for example. date of joining of an employee
what data type can be used to get the "DATE" from the user???
say, for example. date of joining of an employee
Here's the details on struct tm from <time.h>:
You could also define your own struct - if you haven't learnt about them yet, you should..Code:struct tm Represents the components of calendar time: int tm_sec; seconds after the minute int tm_min; minutes after the hour int tm_hour; hours since midnight int tm_mday; day of the month int tm_mon; months since January int tm_year; years since 1900 int tm_wday; days since Sunday int tm_yday; days since January 1 int tm_isdst; Daylight Saving Time flag : is positive if DST is in effect, zero if not in effect, negative if information not known. Implementations may change field order and include additional fields.
actually i've to use the date inside a structure only... so thought it ' d further mess up the code by using nested structures......
thanks for your suggestions
that was really helpful...... thanks... this forum is really amazing!!!!!
Well unless you want to go UNIX-style and use a single large integer to represent the number of seconds since a certain date (UNIX counts the number of seconds since January 1, 1970), you're out of luck - as there's no native type for dealing with that, that's what structs are for. There's nothing inherently evil about nested structs - they can be messy, sure, but sometimes you need them.
edit: as discussed in this thread: time_t
Here are some of my dumb questions (i'm new to linux):
Is it possible to include a header file in linux C? I mean where are the header files located? Is the code "#include<time.h>" is enough for adding the header file to the program?
Why is it we have to use <int main> instead of <void main> everytime?
Should we have to put the "\n" at the end of the <printf> everytime?
Questions regarding this thread:
What's the advantage of using a "tm" struct? I can create my own format of date structure and use it to get the date values from the user.
Is it possible to include a header file in linux C? I mean where are the header files located? Is the code "#include<time.h>" is enough for adding the header file to the program?
Sometime you need to tell the compiler to link to the appropriate libraries. You shouldn't need to for time.h, though.
Why is it we have to use <int main> instead of <void main> everytime?
Because the standard says so! As to why it says so, its because programs in convention are designed to return an integer exit code. All programs do this on all popular operating systems, particularly unix which is most relevant to the development of C. The return value of main is this exit code.
Should we have to put the "\n" at the end of the <printf> everytime?
\n tells printf to start the next print statement on the next line. If you don't want to do this, don't end in \n.
What's the advantage of using a "tm" struct? I can create my own format of date structure and use it to get the date values from the user.
Just that it's standard and can work with other functions not written by you.
For most cases I would probably create a date struct.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
But how do i make use of the predefined struct tm. I tried but it does not seem to work.
[insert]Code:#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { struct tm; /* int tm_sec; seconds after the minute int tm_min; minutes after the hour int tm_hour; hours since midnight int tm_mday; day of the month int tm_mon; months since January int tm_year; years since 1900 int tm_wday; days since Sunday int tm_yday; days since January 1 int tm_isdst; Daylight Saving Time flag : is positive if DST is in effect, zero if not in effect, negative if information not known. */ //tm time; //time.tm_sec = 45; tm.tm_sec = 45; printf("\n Seconds after min: %d", tm.tm_sec); return 0; }
But i did create an instance of the struct tm using
tm time;
time.tm_sec = 45;
and then i tried printing it out and it gave me an error. Its commented out in my program. I did that initially but it didnt work.
AFAIK you need to specify that you want a struct of the type tm. Your declaration is:
struct tm;
It aught to be something like:
struct tm my_tm_struct;
That is just a declaration of the type struct tm.Originally Posted by roaan
That should be:Originally Posted by roaan
but then you should be using some other identifier instead of time since that is a name of a function declared in <time.h>.Code:struct tm time;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)