title: How to convert a c string into its escaped version in c?
body: <p>Is there any buildin function or a alternative simple and fast way of escape a <code>C</code> character array that if used with e.g <code>printf</code> should yield original character array again.</p>
  
  <pre><code>char* str = "\tHello World\n";
  char* escaped_str = escape(str); //should contain "\tHello World\n" with char \ ,t.
  printf(escaped_str); //should print out [TAB]Hello World[nextline] similar to if str was printed.
  </code></pre>
  
  <p>Is there a simple way in c to escape a string with c escape characters.</p>
  
  <h3>Update</h3>
  
  <p>I have buffer containing a string with escape character. And i want to include in a C file. For that i need to escape it so it can be complied. I just need to know if there is simple way of doing it instead of scanning the buffer for \n \t etc and generating there c escape char.</p>
  
  <pre><code>for(int i=0; i&lt; strlen(buffer);i++)
      if(buffer[i]=='\n')
        sprintf(dest,"\\n")
      else ....
  </code></pre>
  
  <h3>Update 2</h3>
  
  <p>I wrote this function. It work fine. </p>
  
  <pre><code>char* escape(char* buffer){
      int i,j;
      int l = strlen(buffer) + 1;
      char esc_char[]= { '\a','\b','\f','\n','\r','\t','\v','\\'};
      char essc_str[]= {  'a', 'b', 'f', 'n', 'r', 't', 'v','\\'};
    char* dest  =  (char*)calloc( l*2,sizeof(char));
      char* ptr=dest;
      for(i=0;i&lt;l;i++){
          for(j=0; j&lt; 8 ;j++){
              if( buffer[i]==esc_char[j] ){
                *ptr++ = '\\';
                *ptr++ = essc_str[j];
                   break;
              }
          }
          if(j == 8 )
        *ptr++ = buffer[i];
      }
    *ptr='\0';
      return dest;
  }
  </code></pre>