最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

terminal - usleep miliseconds is not working properly in C - Stack Overflow

programmeradmin3浏览0评论

I know this is a beginner's bug, but I'm struggling to fix it. The objective is to print the first message from the startGame function on the screen. If the player selects "Enter" the game screen should print out slowly using usleep(milliseconds). However, instead of displaying gradually, it prints everything at once without waiting.

I tested by printing the labirinto function first on the main and then labirinto, the first call it works correctly. But when it's inside the labirinto function, I encounter this issue, printing it out all at once.

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

#define SIZEl 30
#define SIZEh 10

struct sNOCOBRA{
    char cabeca[1][1], i, j;
    struct sNO *prox;
};

int labirinto();
int startgame();
int limites();
int gameover();
    
int main() {

    startgame();
//  limites();
//  gameover();
}


int labirinto() {
    int milliseconds;
    for (int i = 0; i < SIZEl + 2; i++) {
        printf("#");
    }
    printf("\n");
    for (int i = 0; i < SIZEh; i++) {
        printf("#");
        for (int j = 0; j < SIZEl; j++) {
            printf(" ");
            usleep(milliseconds);           
            if (j == SIZEl - 1) {
                printf("#\n");

            }
        }
    }
    for (int i = 0; i < SIZEl + 2; i++) {
        printf("#");
    }
    printf("\n");

}

int startgame() {
    char entrada;
    printf("You've got to eat your way out!\n(sútil referência a DOOM shh...)\n\n       S T A R  N O W ?");
    scanf("%c", &entrada);

    if (entrada == '\n') {
        labirinto();
    }
}

int limites() {
    for (int i = 0; i < SIZEl - 2; i++) {
        for (int j = 0; j < SIZEh - 2; j++) {
            printf("o");
        }
    }   
}

int gameover() {
    printf("G A M E O V E R !⠀\n");
}
``

I know this is a beginner's bug, but I'm struggling to fix it. The objective is to print the first message from the startGame function on the screen. If the player selects "Enter" the game screen should print out slowly using usleep(milliseconds). However, instead of displaying gradually, it prints everything at once without waiting.

I tested by printing the labirinto function first on the main and then labirinto, the first call it works correctly. But when it's inside the labirinto function, I encounter this issue, printing it out all at once.

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

#define SIZEl 30
#define SIZEh 10

struct sNOCOBRA{
    char cabeca[1][1], i, j;
    struct sNO *prox;
};

int labirinto();
int startgame();
int limites();
int gameover();
    
int main() {

    startgame();
//  limites();
//  gameover();
}


int labirinto() {
    int milliseconds;
    for (int i = 0; i < SIZEl + 2; i++) {
        printf("#");
    }
    printf("\n");
    for (int i = 0; i < SIZEh; i++) {
        printf("#");
        for (int j = 0; j < SIZEl; j++) {
            printf(" ");
            usleep(milliseconds);           
            if (j == SIZEl - 1) {
                printf("#\n");

            }
        }
    }
    for (int i = 0; i < SIZEl + 2; i++) {
        printf("#");
    }
    printf("\n");

}

int startgame() {
    char entrada;
    printf("You've got to eat your way out!\n(sútil referência a DOOM shh...)\n\n       S T A R  N O W ?");
    scanf("%c", &entrada);

    if (entrada == '\n') {
        labirinto();
    }
}

int limites() {
    for (int i = 0; i < SIZEl - 2; i++) {
        for (int j = 0; j < SIZEh - 2; j++) {
            printf("o");
        }
    }   
}

int gameover() {
    printf("G A M E O V E R !⠀\n");
}
``
Share Improve this question edited Mar 11 at 13:44 Ted Lyngmo 119k7 gold badges84 silver badges136 bronze badges asked Mar 11 at 13:28 KhaosKhaos 255 bronze badges 6
  • 1 add fflush(stdout); after printf – Iłya Bursov Commented Mar 11 at 13:31
  • 2 and you fot to initialize milliseconds variable – Iłya Bursov Commented Mar 11 at 13:32
  • Does this answer your question? – dimich Commented Mar 11 at 13:34
  • Note that usleep() sleeps for microseconds, not milliseconds. – pmacfarlane Commented Mar 11 at 13:45
  • Slow human eyes can't even register microseconds, even if you had an OS with that kind of real-time performance. Which you don't. – Lundin Commented Mar 11 at 14:16
 |  Show 1 more comment

1 Answer 1

Reset to default 7

You have two problems here.

First, milliseconds is uninitialized and therefore its value is indeterminate. Attempting to read a variable with an indeterminate value (in this case) triggers undefined behavior in your code. You need to set this to a specific value. Also, usleep sleeps for the given number of microseconds, not milliseconds.

Second, you're using printf to print to stdout, and doing so is line buffered by default. So any printf call that doesn't contain a newline should be followed by a call to fflush(stdout) so that output gets printed immediately.

发布评论

评论列表(0)

  1. 暂无评论