1

I'm trying to print out a character array (carefully choosing my words here) as if it were being typed by someone to the console. For some reason, printf does not immediately print the character to the console. As-is, the program will have nothing printed to the console for the exact amount of time taken to print the entire character array. However, when I uncomment the line //printf("\n"); , the console will actually show the contents being "typed", although vertically. In other words, using a newline seems to force the console to immediately print the character.

Why is this, and what would be an ideal way to fix this? I ask because I'm trying to think generally: what if I were writing a text-engine for a graphical video game?

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

void milliDelay(unsigned int milliseconds)
{
    usleep(milliseconds * 1000);
}

void printConsole(char* txt)
{
    //Define local variables such as the iterator and timers
    unsigned int i;
    unsigned const int keyDelay = 75, puncDelay = 1000;

    //Print each character to the screen until we reach the null character
    for(i = 0; txt[i] != '\0'; i++)
    {
        if(txt[i] == '.' || txt[i] == '!' || txt[i] == '?')
        {
            printf("%c", txt[i]);
            milliDelay(puncDelay);
        }
        else 
        {
            printf("%c", txt[i]);
            milliDelay(keyDelay);
        }
        //printf("\n");
    }
    //Newline for much more appealing text formatting
    printf("\n");
}

int main(int argc, char** argv) {
    printConsole("Hello. This is a test using punctuation! Does this pause correctly?");
    return 0;
}

For what it's worth, I'm using an Ubuntu system. I have not been able to see if the code works as intended on other platforms. I have also researched this question. I have written the code as I thought of it. Others have used putchar(), but this is something I never saw in my C programming class--hence I don't use that here. Is there anything bad about using printf() over putchar() in such a case?

Decaf-Math
  • 279
  • 1
  • 9
  • 1
    The characters are not displayed because stdout is a buffered device. So when printf writes characters to stdout, they are buffered and not displayed until a \n character is output, or when the buffer is flushed. If you call fflush(stdout), it will output the characters in the buffer immediately. – ScottK Jun 19 '17 at 21:15
  • `printf()` is stdout, by default stdout is line buffered.In that case, you need to flush the stdout. – danglingpointer Jun 19 '17 at 21:16
  • printf is buffered by default, change this using setvbuf or flush it when needed. Using printf takes more time then using putchar for a single char because print needs to parse its format string at least. – Jean-Baptiste Yunès Jun 19 '17 at 21:16
  • @Jean-BaptisteYunès: Check the output of a simple `printf("Hello World\n");` with a modern compiler like gcc. Chances are good you will not find a call to `printf` at all, but `puts`! What we learn from this: Leave optimisations to the compiler unless you **really** have a problem and fond the true hot-spots by profiling! – too honest for this site Jun 19 '17 at 21:31

0 Answers0