-1

I was able to run code that uses the randomize function without including the time.h library. is it automatically included with some other libraries i might have already included in my code? Below is a list of the libraries I included:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <string.h> 
#include <io.h>
yonatan
  • 35
  • 3

3 Answers3

2

This is very very specific to the version and implementation of your library. The standard doesn't force any header to include time.h 1 so you cannot rely on that.

In your case, it could be that one of dos.h, io.h, conio.h for example has included time.h (or any other of the headers there for all it's worth).


1 At least not the ones there and not likely in your seemingly ancient library. C11 says threads.h should include time.h

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • Would this line in `dos.h` explain it: `void _Cdecl _dos_gettime( struct dostime_t *__timep );` – yonatan Apr 02 '13 at 17:05
  • @yonatan, I cannot find any documentation on `randomize()`, so I can't really tell. However it seems like your calls (i.e. `randomize()` and `random(300)`) do _not_ use time functions at all. Why do you expect `time.h` to be included? You need to find out where `randomize` is defined in your non standard platform. `stdlib.h` is a good guess. – Shahbaz Apr 03 '13 at 08:24
  • As `randomize()` makes the initial value of `random()` random based on the current time and the compiler documentation therefore states it requires `time.h`. – yonatan Apr 03 '13 at 12:51
  • Since `randomize()` is not taking any parameter, then it is calling `time.h` related functions _itself_. This means that in the source file implementing the function `randomize()`, `time.h` is included. Once that source file has been compiled by whoever wrote the library that comes with turbo C, `randomize()` would work correctly all by itself. You wouldn't need to include a header file for it. – Shahbaz Apr 03 '13 at 13:24
  • From your terminology, I think I understand where the problem is. You see, when you write `#include `, you are **not** including a _library_, you are merely making the interface to the library visible to you. The library itself is linked at a later phase after the compilation and by the linker. So if you don't _use_ the time functions yourself, you don't need to include its header files. However, since the time functions are in the standard library, they _will_ get linked with your program whether you like it or not. – Shahbaz Apr 03 '13 at 13:26
  • Read [here](http://stackoverflow.com/a/333964/912144), [here](http://stackoverflow.com/q/1945846/912144) or [here](http://en.wikipedia.org/wiki/Header_file) for example. – Shahbaz Apr 03 '13 at 13:26
1

What does <compiler with high warning level> yourcode.c say? My guess would be:

  • either one of the non-standard DOS-specific headers (conio.h, dos.h, io.h, ...) includes it,

  • or there's no declaration at all, i. e. it's not included, in which case your compiler silently and implicitly assumes a function signature (specifically, it assumes a return value of int and whatever type of argument you call it with for the first time).

Note that the latter case is wrong, and you should pay attention not to do it (since it may lead your program invoking undefined behavior). Always compile with all warnings enabled so you can track down such an error.

  • @0A0D Please elaborate, what do you mean by that? I've looked at that link and there's no `time()` in ``. –  Apr 02 '13 at 16:53
  • I mean that on a non-standard platform, such as the ticalc website I linked to, randomize() is a function in stdlib. –  Apr 02 '13 at 16:53
1

When C compiler can't find a prototype to a function it assumes it is a function that returns int. It also prints a warning function if you didn't change the default settings.

So. In your case perhaps time.h was included, but be aware that it can cause a lot of problems if it wasn't.

stdcall
  • 27,613
  • 18
  • 81
  • 125