Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 904601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:04:48+00:00 2026-05-15T16:04:48+00:00

Is there any buildin function or a alternative simple and fast way of escape

  • 0

Is there any buildin function or a alternative simple and fast way of escape a C character array that if used with e.g printf should yield original character array again.

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.

Is there a simple way in c to escape a string with c escape characters.

Update

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.

for(int i=0; i< strlen(buffer);i++)
    if(buffer[i]=='\n')
      sprintf(dest,"\\n")
    else ....

Update 2

I wrote this function. It work fine.

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<l;i++){
        for(j=0; j< 8 ;j++){
            if( buffer[i]==esc_char[j] ){
              *ptr++ = '\\';
              *ptr++ = essc_str[j];
                 break;
            }
        }
        if(j == 8 )
      *ptr++ = buffer[i];
    }
  *ptr='\0';
    return dest;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:04:49+00:00Added an answer on May 15, 2026 at 4:04 pm

    No, there isn’t any standard function for creating the source code version of the string. But you could use the iscntrl function to write one, or just use the switch keyword.

    But, unless your program writes out a C source file intended to be run through the compiler, you don’t need to work with escaped strings. printf doesn’t process character escape sequences, only variable insertions (%d, %s, etc)

    Specifically, the following produce the same output:

    printf("\tHello World\n");
    

    and

    const char* str = "\tHello World\n";
    printf(str);
    

    and

    const char* str = "\tHello World\n";
    printf("%s", str);
    

    The second one isn’t a good idea, because if str contained % your program would produce bad output and could crash.

    EDIT: For producing the source code version, there are a couple of approaches:

    Simpler, but less readable output:

    if (iscntrl(ch) || ch == '\\' || ch == '\"' || ch == '\'') {
       fprintf(outf, "\\%03o", ch);
    }
    else
       fputc(ch, outf);
    

    More readable results:

    switch (ch) {
      case '\"':
        fputs("\\\"", outf);
        break;
      case '\'':
        fputs("\\\'", outf);
        break;
      case '\\':
        fputs("\\\\", outf);
        break;
      case '\a':
        fputs("\\a", outf);
        break;
      case '\b':
        fputs("\\b", outf);
        break;
      case '\n':
        fputs("\\n", outf);
        break;
      case '\t':
        fputs("\\t", outf);
        break;
      // and so on
      default:
        if (iscntrl(ch)) fprintf(outf, "\\%03o", ch);
        else fputc(ch, outf);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to Use the Builtin ScrollBars that comes with each .Net
Is there any other way to write javascript:false that is more pleasant? I'm building
Is there any way I can set a formatter on models that will convert
Is there any build-in function in Python that merges two lists into a dict?
Is there any build in function inside eclipse that allows you to preset certain
Is there any way to compare two functions in Haskell? My thought is that
Is there any way in Notepad++ (or even with another tool) to change the
Is there any way to use Google's API to retrieve a user's current zipcode
Is there any way to overload the > (greater than) operator, to be able
I was wondering if there is any php function to convert the number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.