Bonjour, je me suis fait un chronomère qui calcule à la microseconde en utilisant gettimeofday(). Voici mon .h

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
#include<stdlib.h>
#include<stdio.h>
#include <sys/time.h>
 
enum ChronoState {RESET,ON,OFF,PRINT};
 
typedef struct
{
  enum ChronoState cs;
  struct timeval start,end;
  struct timezone tz;
} Chronometer;
 
void Chrono(Chronometer *,enum ChronoState);
et voici mon .c

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
#include<stdlib.h>
#include<stdio.h>
#include <sys/time.h>
#include "chrono.h"
 
#define PERROR(msg) fprintf (stderr,"%s %d : Error : " #msg "\n",__FILE__,__LINE__)
 
void Chrono(Chronometer * c,enum ChronoState cs)
{
  if(cs==OFF)
  {
    if(c->cs!=ON)
      PERROR("Impossible to stop the chronometer : it wasn't launched\n");
    else
    {
      c->cs=cs;
      gettimeofday(&c->end,&c->tz);
    }
  }
  else if(cs==ON)
  {
    if(c->cs!=RESET)
      PERROR("Impossible to launch chronometer : its state is different of RESET\n");
    else
    {
      c->cs=cs;
      gettimeofday(&c->start,&c->tz);
    }
  }
  else if(cs==PRINT)
  {
    if(c->cs!=OFF)
      PERROR("Impossible to print elapsed time : the chronometer wasn't stopped\n");
    else
    {
      c->cs=cs;
      long nbsec=c->end.tv_sec-c->start.tv_sec; /* secondes number */
      long nbmicrosec=c->end.tv_usec-c->start.tv_usec; /* micro secondes number */
      double eltime=nbsec+nbmicrosec*1e-6;
      printf("Elapsed time : %g sec\n",eltime);
      c->cs=RESET;
    }
  }
}
mais qu'est-ce qui me garantit que nbsec et nbmicrosec seront positifs ou nuls ?

Merci.