How to print '\n' instead of a newline?

68,631

Solution 1

Print "\\n" – "\\" produces "\" and then "n" is recognized as an ordinary symbol. For more information see here.

Solution 2

The function printchar() below will print some characters as "special", and print the octal code for characters out of range (a la Emacs), but print normal characters otherwise. I also took the liberty of having '\n' print a real '\n' after it to make your output more readable. Also note that I use an int in the loop in main just to be able to iterate over the whole range of unsigned char. In your usage you would likely just have an unsigned char that you read from your dataset.

#include <stdio.h>

static void printchar(unsigned char theChar) {

    switch (theChar) {

        case '\n':
            printf("\\n\n");
            break;
        case '\r':
            printf("\\r");
            break;
        case '\t':
            printf("\\t");
            break;
        default:
            if ((theChar < 0x20) || (theChar > 0x7f)) {
                printf("\\%03o", (unsigned char)theChar);
            } else {
                printf("%c", theChar);
            }
        break;
   }
}

int main(int argc, char** argv) {

    int theChar;

    (void)argc;
    (void)argv;

    for (theChar = 0x00; theChar <= 0xff; theChar++) {
        printchar((unsigned char)theChar);
    }
    printf("\n");
}

Solution 3

Just use "\\n" (two slashes)

Solution 4

You can escape the backslash to make it print just a normal backslash: "\\n".

Edit: Yes you'll have to do some manual parsing. However the code to do so, would just be a search and replace.

Solution 5

If you want to make sure that you don't print any non-printable characters, then you can use the functions in ctype.h like isprint:

if( isprint( theChar ) )
  printf( "%c", theChar )
else
  switch( theChar )
  {
  case '\n':
     printf( "\\n" );
     break;
  ... repeat for other interesting control characters ...
  default:
     printf( "\\0%hho", theChar ); // print octal representation of character.
     break;
  }
Share:
68,631
samoz
Author by

samoz

I am a cryptographer, working in the West Lafayete, IN area. My interests include reverse engineering, information security, ellipic curves, trusted computing environments, operating systems, and embedded systems. Most of my reversing is on Windows, but I like to practice my make-fu and Unix-fu on Arch Linux.

Updated on August 23, 2022

Comments

  • samoz
    samoz over 1 year

    I am writing a program that uses prints a hex dump of its input. However, I'm running into problems when newlines, tabs, etc are passed in and destroying my output formatting.

    How can I use printf (or cout I guess) to print '\n' instead of printing an actual newline? Do I just need to do some manual parsing for this?

    EDIT: I'm receiving my data dynamically, it's not just the \n that I'm corned about, but rather all symbols. For example, this is my printf statement:

    printf("%c", theChar);
    

    How can I make this print \n when a newline is passed in as theChar but still make it print normal text when theChar is a valid printable character?

  • mark4o
    mark4o almost 15 years
    Should be < 32 (or 0x20) and >= 127. Also for %o you need to cast theChar to unsigned char (or & 0xff) since char is usually signed.
  • Jared Oberhaus
    Jared Oberhaus almost 15 years
    Thanks @mark4o, you're right about the 0x20 and the 32. Fixed my answer.
  • Jon
    Jon over 8 years
    This is not how std::basic_string::replace works. From en.cppreference.com/w/cpp/string/basic_string/replace we see that replace will replace the part of the string indicated by either [pos, pos + count) or [first, last) with a new string. It does not search for the original string and replace found occurrences.
  • truthseeker
    truthseeker over 6 years
    the condition for upper bound should be >= 0x7f instead of > 0x7f. 0x7f character represents delete which is non-printable character