Skip to main content

Command Palette

Search for a command to run...

Different ways to create a string

Updated
3 min readView as Markdown
Different ways to create a string

Variables that store Strings

C has no specific provision within its syntax for variables that store strings, and because there are no string variable types, there are no special operators in the language for processing strings. This is not a problem though, because the standard library provides an extensive range of functions to handle strings.

First, let’s see how you create variables that represent strings. You use an array of type char to hold strings. This is the simplest form of string variable. You can declare an array variable like this:

char message[20];

This variable can accommodate a string that contains up to 19 characters, because you must allow one element for the termination character. Of course, you could also use this array to store 20 characters that are not a string.

Here you haven’t explicitly defined the array dimension. The compiler will assign a value to the dimension that is sufficient to accommodate the initializing string constant. In this case it will be 18, which is 17 elements for the characters in the string plus an extra element for the terminating \0. You can specify the dimension explicitly, but if you leave it for the compiler to figure out, you can be sure it will be correct. You can initialize just part of an array of elements of type char with a string. For example:

char message[40] = "To be";

The compiler will initialize the first five elements, str[0] to str[4], with the characters of the string constant, and str[5] will contain the null character, '\0'. Of course, space is allocated for all 40 elements of the array, and they’re all available to use in any way you want.

Initializing a char array and declaring it as constant is a good way of handling standard messages:

const char message[] = "This is an electronically generated report.";

Because you declare message as const, it’s protected from being modified explicitly within the program. Any attempt to do so will result in an error message from the compiler.

In C, the name of an array, is actually a pointer to the first element of the array.

int main(){
    printf("\n\nString literal assigned to char array : \n\n");
    char string1[] = "Hello";
    printf(string1);

    printf("\n\nSet of chars, ending with '\\0', assigned to char array : \n\n");
    char string2[] = {'H','e','l','l','o','\0'};
    printf(string2);

    printf("\n\nSet of chars, ending with '\\0', assigned to char array : \n\n");
    char string3[] = {72,101,108,108,111,0};
    printf(string3);

    printf("\n\nString literal assigned to char pointer : \n\n");
    char* string4 = "Hello";
    printf(string4);

    printf("\n\nChanging a character in the second string : \n\n");
    string2[2]='n';
    printf(string2);

    printf("\n\n");

    *string2='M';
    printf(string2);

    printf("\n\nChanging a character in the third string : \n\n");
    string3[0]=67;
    printf(string3);

    printf("\n\n");

    *string3=74;
    printf(string3);

    char* temp = string4;

    printf("\n\nTrying to change a character in the fourth string : \n\n");
    //string4[2]='n'; //Undefined behavior, error
    //*(string4+2)='n'; //Undefined behavior, error
    printf(string4);

    printf("\n\nTrying to change the string assigned to string4 variable : ");
    /* Error
    string1 = "World";
    printf(string1);
    */
    /* Error
    string2 = "World";
    printf(string2);
    */
    printf("\n\n String 4 Before Change \n\n");
    puts(string4);
    printf("\n\n Temp Before Change \n\n");
    puts(temp);

    string4 = "World";

    printf("\n\n String4 After Change \n\n");
    puts(string4);
    printf("\n\n Temp After Change in String4 \n\n");
    puts(temp);

    temp = "Welcome";

    printf("\n\n String4 After Change in Temp\n\n");
    puts(string4);
    printf("\n\n Temp After Change \n\n");
    puts(temp);

}

Output:

See the compile time warning and run time error generated by *temp = Welcome below.

Because *temp is actually temp[0]. Even

Even *temp = 'V' or any character also doesn't work. You cannot make changes in string4 by manipulating temp.

27 views

More from this blog

P

Programming Tutorials

89 posts