La delay con C

Pubblicità

richieb

Nuovo Utente
Messaggi
4
Reazioni
0
Punteggio
24
Salve,

ho appena eseguito questo programma scritto in C che crea un timer, seguendo un manuale di C. Quello che non capisco e che non riesco a capire è a cosa serva la DELAY dell'header, e perchè il tipo dichiarato nella printf

"%02d:"

Qui posto il codice:



/* Visualizza un timer software */
#include <stdio.h>
#define DELAY 128000
struct my_time {
int hours;
int minutes;
int seconds;
};
void display(struct my_time *t);
void update(struct my_time *t);
void delay(void);
int main(void)
{
struct my_time systime;

systime.hours = 0;
systime.minutes = 0;
systime.seconds = 0;

for(;;) {
update(&systime);
display(&systime);
}
return 0;
}
void update(struct my_time *t)
{
t->seconds++;
if(t->seconds==60) {
t->seconds = 0;
t->minutes++;
}
if(t->minutes=60) {
t->minutes = 0;
t->hours++;
}
if(t->hours==24) t->hours = 0;
delay();
}
void display(struct my_time *t)
{
printf("%02d:", t->hours);
printf("%02d:", t->minutes);
printf("%02\n", t->seconds);
}
void delay(void)
{
long int t;

/* modificare a piacere DELAY */
for(t=1; t<DELAY; ++t);

}
 
Salve,

ho appena eseguito questo programma scritto in C che crea un timer, seguendo un manuale di C. Quello che non capisco e che non riesco a capire è a cosa serva la DELAY dell'header, e perchè il tipo dichiarato nella printf

"%02d:"

Qui posto il codice:



/* Visualizza un timer software */
#include <stdio.h>
#define DELAY 128000
struct my_time {
int hours;
int minutes;
int seconds;
};
void display(struct my_time *t);
void update(struct my_time *t);
void delay(void);
int main(void)
{
struct my_time systime;

systime.hours = 0;
systime.minutes = 0;
systime.seconds = 0;

for(;;) {
update(&systime);
display(&systime);
}
return 0;
}
void update(struct my_time *t)
{
t->seconds++;
if(t->seconds==60) {
t->seconds = 0;
t->minutes++;
}
if(t->minutes=60) {
t->minutes = 0;
t->hours++;
}
if(t->hours==24) t->hours = 0;
delay();
}
void display(struct my_time *t)
{
printf("%02d:", t->hours);
printf("%02d:", t->minutes);
printf("%02\n", t->seconds);
}
void delay(void)
{
long int t;

/* modificare a piacere DELAY */
for(t=1; t<DELAY; ++t);

}


Allora, DEFINE DELAY definisce una struttura grande 128000 (se non erro è un modo per definire un array)

Per quanto riguarda il printf è più semplice, tu stampi una variabile (%d) e però imponi che questa DEVE avere solo 2 cifre...
 
Pubblicità
Pubblicità
Indietro
Top