Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print to terminal one char and then pause and print another?

I’ve been trying to print to the terminal with a pause, it’s better explained with the following code:

I’m trying to have X print to the terminal and then wait 5s and have X print again but when I run the code, it waits 5s and prints XX can anyone help me get the proper functionality?

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

static void sleepMs(long long delayMs){
    const long long NS_PER_MS = 1000 * 1000;
    const long long NS_PER_SECOND = 1000000000;
    long long delayNs = delayMs * NS_PER_MS;
    int seconds = delayNs / NS_PER_SECOND;
    int nanoSeconds = delayNs % NS_PER_SECOND;
    struct timespec reqDelay = {seconds, nanoSeconds};
    nanosleep(&reqDelay, (struct timespec *) NULL);
}

int main()
{
    printf("X");
    sleepMs(5000);
    printf("X");
    

    return 0;
}

Thank you in advance, sorry for any missing tags.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

EDIT: I want them to print on the same line

>Solution :

You need to flush the output stream if you want to see the result before printing a \n:

putchar('X');
fflush(stdout);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading