C Programming

How to Convert a C String into its Escaped Version in C Programming?

C Programming is a popular and powerful language that is widely used for system-level programming, embedded systems, and many other applications. When working with C strings, it is often necessary to convert them into their escaped versions. This is useful, for example, when dealing with special characters such as newlines, tabs, or backslashes that need to be represented in a specific way.

In this post, we’ll examine how to change a C string in C programming into its escaped version.

What is Escaped Version?

In C Programming, an escaped version of a string is a modified version of the original string that includes escape sequences for special characters such as newline, tab, backslash, and others. These special characters cannot be directly represented in a string using their literal form, so they are replaced with a backslash followed by a character code that represents the special character.

For example, if you have a C string with the value Hello, LinuxHint!, its escaped version would be \”Hello, LinuxHint!\”.

How to Convert a C String into its Escaped Version in C Programming?

There are no built-in functions in C Programming that allow you to convert a C string into an escaped version. But you can easily transform a C string into an escaped version by using the instructions provided below:

Step 1: The first step is to declare a new string variable that will store the input string that has been escaped. The input string’s data type should match that of this variable.

Step 2: The following action entails character-by-character iteration through the input string. We must determine whether there are any escape sequences when iterating through the string. If an escape sequence is found, it should be replaced with its corresponding ASCII character representation.

Step 3: In the third step, we must add each character from the input string to the new string variable. But first, we must determine whether the character is an escape sequence or a special character. If so, we should substitute the matching ASCII representation.

Step 4: Add a null character to the end of the string to finish it off after all the characters have been added to the new string variable. This is essential due to the null-terminated nature of C strings. The string’s end is indicated by the null character.

Step 5: At this point, we can print the string that has been escaped. To print the escaped version of the string, we can either use the “printf()” function or the “puts()” function. For these functions, the new string variable should be used as an argument.

The following C example follows the above-given steps and will allow you to convert a C string into its escaped version.

#include <stdio.h>

int main() {
    char str[] = "LinuxHint!\n";
printf("Original string: %s", str);

    for (int i = 0; str[i] != '\0'; i++) {
        switch (str[i]) {
            case '\n':
            case '\t':

    return 0;

In the above code, the string “LinuxHint!” is printed in its original form before iterating through each of the characters. The matching escape sequence (\n or \t, respectively) is printed if the character is a newline (\n) or a tab (\t). If not, the putchar() function is used to print the character itself. You can see that the loop replaces the special character n at the end of the original string with its appropriate escape sequence \n.


Text Description automatically generated


Converting C strings into their escaped versions is an important task when dealing with special characters. Although there are no built-in functions for this in C programming, it can be accomplished by iterating through the string and replacing any special characters with their corresponding ASCII representations. The example code provided in this article demonstrates how to convert a C string into its escaped version.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.