Skip to main content
5 of 5
typo
David Foerster
  • 1.7k
  • 10
  • 20
  • You're using printf to print a single char:

    printf("%c", aux[j]);
    

    printf is a very handy tool to format output, but it carries quite a bit of overhead with it – not something you want to call over and over without a good reason. A much more efficient way to print a single char to stdout would be:

    putchar(aux[j]);
    

    Or, instead of looping over the string to print it character-by-character, you can print the whole thing with just one function call:

    puts(aux);
    

    (Note that puts prints an additional newline character at the end of the string. If you want to avoid that, use fputs(aux, stdout) instead.)

    puts and fputs require the string (aux) to be terminated by a NUL-character ('\0'), which it isn't in your program. If you want to print a character sequence of known length (but possibly without such a terminator character), you can use:

    fwrite(aux, 1, strlen(n), stdout);
    
  • You can also use puts or fputs to print all the string literals in your program with less overhead, e. g.

    puts("\nWelcome to the super, duper string inverter!");
    

    instead of

    printf("\nWelcome to the super, duper string inverter!\n");
    

    Now you don't even need to worry about % characters being interpreted as format descriptors.

  • You can concatenate string literals that are printed right after each other to avoid the overhead of additional buffering and I/O locking and unlocking:

    fputs("Included with the super mojo from the string inverter, this is the result: ", stdout);
    

    If you don't like long string literals you can break them into multiple parts:

    fputs("Included with the super mojo from the string inverter, "
          "this is the result: ", stdout);
    

    produces exactly the same syntax tree and binary code as the source code just before.

David Foerster
  • 1.7k
  • 10
  • 20