0

We're required to use gettime() to get the current time in C. I'm trying to print the current time but the error:

error: storage size of 't' isn't known

occurs. I don't know how to solve this. Here is the code:

#include<stdio.h>
#include<dos.h>

int main(){

   struct time t;

   gettime(&t);

   printf("%d:%d:%d", t.ti_hour,t.ti_min,t.ti_sec);

   getch();
   return 0;
}
Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
PG20
  • 67
  • 1
  • 2
  • 10
  • 2
    Umm, `gettime` is a Javascript thing, not a C thing. If someone or something told you that there' s a C function called `gettime` on your particular system or platform, ask them how to use it. – David Schwartz Aug 28 '17 at 07:45
  • 1
    `gettime` is neither standard C or posix, you need to know the headers to be included and if required, the library to be linked. – Sourav Ghosh Aug 28 '17 at 07:47
  • Further this `struct time t;` is no standard C as there is **no** `time` struct but a [`tm` struct](http://en.cppreference.com/w/c/chrono/tm). – Andre Kampling Aug 28 '17 at 07:47
  • 6
    The major problem IMO, is the requirement to use old, obsolete and antiquated functionality from the old DOS days. I wish schools would stop using old Turbo C and Turbo C++ and DOS in general, and teach modern C and C++ instead. Or maybe even the old *standard* versions of those languages. – Some programmer dude Aug 28 '17 at 07:47
  • Your code is "fine" (although not standard) but you need to use a compatible compiler. I believe you have picked the example up from [here](http://www.programmingsimplified.com/c/dos.h/gettime). Which compiler are you using? – Ajay Brahmakshatriya Aug 28 '17 at 08:28
  • Yes, I did picked that example from there so that I could try it first before applying it to my program. It won't work on codeblocks but I tried it on Turbo C and it worked. The only problem is my Turbo C is not working properly. It can't write and read files no matter what I do. But my program can read and write on codeblocks. It's just weird to me. – PG20 Aug 28 '17 at 08:48
  • 1
    @JoseG. Turbo C compiler uses it's own library that supports these functions. But it is not a part of the present C standard. As a result, it Code blocks (with gcc as a compiler I assume) doesn't support these library functions. If your instructor has asked you to use these functions only, you can continue with Turbo C. But if you wish to use standard C, you can look at [time](https://www.tutorialspoint.com/c_standard_library/c_function_time.htm). The link also has example on how to use it. This will work with code blocks. – Ajay Brahmakshatriya Aug 28 '17 at 09:10

3 Answers3

3

It's not clear if you want to get the time, or just print it.

For the second case, there are few legacy methods that will provide formatted time (asctime, ctime). But those may not fit your needs.

The more flexible option is to use strftime based on data from time/localtime_r. The strftime support many of the escapes (%Y, ...) that are available with GNU date.

#include <stdio.h>
#include <time.h>

void main(void)
{

   time_t now = time(NULL) ;
   struct tm tm_now ;
   localtime_r(&now, &tm_now) ;
   char buff[100] ;
   strftime(buff, sizeof(buff), "%Y-%m-%d, time is %H:%M", &tm_now) ;
   printf("Time is '%s'\n", buff) ;
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
dash-o
  • 13,723
  • 1
  • 10
  • 37
1

The standard C function to get the local time is simply time, included in the header time.h

Example taken from here.

/* time example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, difftime, time, mktime */

int main ()
{
  time_t timer;
  struct tm y2k = {0};
  double seconds;

  y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
  y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;

  time(&timer);  /* get current time; same as: timer = time(NULL)  */

  seconds = difftime(timer,mktime(&y2k));

  printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);

  return 0;
}

More info on the different time format :

http://www.cplusplus.com/reference/ctime/mktime/

http://www.cplusplus.com/reference/ctime/localtime/

Clonk
  • 2,025
  • 1
  • 11
  • 26
1

Ok, you're trying to use the DOS.H library, so the time.h and ctime library does not apply to this particular issue due to DOS.H is an exclusive C library you cannot use it in any other C (C++ or C#), please read the library after you read this post so you can see clearly what am i talking about, so within the DOS.H library is an STRUCT that you use for save all the variables of time, this is hour, minutes, seconds, so the first thing that we have to do is to declare a variable that allow us save this type of data:

struct time tm;

Once you do that you can use the gettime() function wich is on the library to, this to save que values in a place where we can get access to:

gettime(&tm);

And finally to print o do wherever you want with this data you need to get each register of the structure:

printf("System time is: %d : %d : %d\n",tm.ti_hour, tm.ti_min, tm.ti_sec);

Check this code:

#include<stdio.h>
#include<dos.h>
#include<conio.h>

int main()
{
struct date fecha;
struct time hora;
union REGS regs;

getdate(&fecha);
printf("La fecha del sistema es: %d / %d / %d\n",fecha.da_day,fecha.da_mon,fecha.da_year);

regs.x.cx = 0x004c;
regs.x.dx = 0x4b40;
regs.h.ah = 0x86; /* 004c4b40h = 5000000 microsegundos */

int86(0x15,&regs,&regs); /* Interrupcion 15h suspension de sistema  */

gettime(&hora);
printf("la hora del sistema es: %d : %d : %d\n",hora.ti_hour,hora.ti_min,hora.ti_sec);

getche();
clrscr();
return 0;
}