Race car simulator help: using sleep()

Hi there !
My name is Marc, I am new to codding and C is my very first language.
I am following the Learn C course.
I have to admit I struggled a bit getting to the last project and had to use chatGPT to gain some basic understanding.
I wanted to introduce a time lag in the printCountDown() function. As well as in the updateFirstPlace() function using sleep() however everytime I try and use sleep() all it does it delays the total running of the program.

How best can this be done?
I thank you in advance for any help !

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

// Structures section
struct Race {
  int numberOfLaps;
  int currentLap;
  char firstPlaceDriverName[20];
  char firstPlaceRaceCarColor[20];

struct RaceCar {
  char driverName[20];
  char raceCarColor[20];
  int totalLapTime;
// Print functions section
void printIntro() {
  printf("Welcome to our main event digital race fans !\nI hope everybody has their snack because we are about to begin !\n");

void printLap(struct Race race){
  printf("The Total number of laps is %d!\n", race.numberOfLaps);

int printCountDown(){
  printf("Racers Ready!\nIn...\n");
  int j = 10;
  while (j >= 0){
    if (j == 0){
    } else {
      printf("%i\n", j);

void printFirstPlaceAfterLap(struct Race race){
 printf("After Lap %d, the leader is %s driving a %s race car.\n", race.currentLap, race.firstPlaceDriverName, race.firstPlaceRaceCarColor);

void printCongratulation(struct Race race){
  printf("Let's all congratulate %s in the %s race car for an amazing performance. It truly was a great race and everybody have a goodnight!\n", race.firstPlaceDriverName, race.firstPlaceRaceCarColor);

// Logic functions section
// The time it takes to complete a Lap is a function of Speed, Acceleration and Nerves. If all are 3 then the time will be low.
int calculateTimeToCompleteLap(){
  // This ensure that each time the function is called it generates a different number according to the actual time.
  int speed = rand() % 3 + 1;
  int acceleration = rand() % 3 + 1;
  int nerves = rand() % 3 + 1;
  return speed + acceleration + nerves;

void updateRaceCar(struct RaceCar *raceCar){
  raceCar->totalLapTime = calculateTimeToCompleteLap();

void updateFirstPlace(struct Race *race, struct RaceCar *raceCar1, struct RaceCar *raceCar2){
  if (raceCar1->totalLapTime >= raceCar2->totalLapTime){
    strcpy(race->firstPlaceDriverName, raceCar1->driverName);
    strcpy(race->firstPlaceRaceCarColor, raceCar1->raceCarColor);
  } else {
    strcpy(race->firstPlaceDriverName, raceCar2->driverName);
    strcpy(race->firstPlaceRaceCarColor, raceCar2->raceCarColor);

void startRace(struct RaceCar *raceCar1, struct RaceCar *raceCar2){
  struct Race race = {5, 1, "", ""};
  for (int i = 0; i < race.numberOfLaps-1; i++){
    updateFirstPlace(&race, raceCar1, raceCar2);

int main() {
  struct Race race = {5, 1, "", ""};
  struct RaceCar raceCar1 = {"Tom", "Blue", calculateTimeToCompleteLap()};
  struct RaceCar raceCar2 = {"Mike", "Green", calculateTimeToCompleteLap()};
  startRace(&raceCar1, &raceCar2);

Hey, Marc! Welcome to the forums. I find your desire to add a pause between printing the output lines to more closely simulate an actual race impressive. It’s also pretty gutsy on your part to begin your learning to code journey with C rather than a higher level language like Python or JavaScript. Kudos to you for completing the C course.

Assuming you implemented the Sleep function correctly, which it appears you did from your description of it delaying the entire output, the issue is on Codecademy’s side. The console in this project only shows the output after your program is executed to completion. To see the Sleep function actually work, compile and run the program on your own computer.


Hey there !
Thank you for your reply and your positive message ! :slight_smile:

That does make a lot of sense, I will do so.

1 Like