Microcontroller Embedded C Programming Lecture 176| String literal

  • Post author:
  • Post category:Blog

 

String literal

 

String literal or String constant

A string literal is a sequence of characters enclosed within double quotes (“) or single quotes (‘). It is a data type in computer programming that represents a sequence of characters, such as text.

String literals are used to represent strings of characters in a program. They are stored in the program’s memory as a sequence of characters terminated by a null character ‘\0’ (ASCII value 0).

It is important to note that string literals are read-only and attempting to modify them can result in undefined behavior. To create a mutable string in C, you should use a character array instead of a string literal.

 

Let’s say, I store the string in my program in this fashion.

char *msg1 = “Hello”;

This is also a valid method to store a string in your program. So, in this method, we have used a character pointer. 

Here on the left-hand side is nothing but a char pointer. So,  msg1 is a pointer variable of type char *. And you know that pointer variables are used to store the pointer or an address. 

“Hello” must be converted into a pointer. So, at the end of the day, msg1 will hold a pointer.

Who allocated the memory for “Hello” this string or for this message?

The answer to that is the compiler.

The compiler allocates some address to this string literal and then the compiler stores the base address of that message into the character pointer here, that is msg1.

 

char *msg1 = “Hello”;

char msg2[] = “Hello”; 

What is the difference between these two statements?

Both are valid ‘C’ definitions. 

char *msg1 = “Hello”;

In the first statement, the compiler allocates some memory for this string literal and that memory lives in the read-only memory or ROM. So, this string “Hello” actually lives in ROM. 

char msg2[] = “Hello”; 

But in the second statement, you have created a character array. And you have stored the characters. In this case, this string actually lives in RAM. That means, in this case, I can modify the contents of the string because it lives in RAM. 

 

Let’s say I can do msg2[0] = ‘S here. A string will be modified. 

But I cannot do the same thing in the first case. I cannot do msg1[0] = ‘S’; // This is wrong.

Why?

Because you are trying to modify the value which is living in a ROM memory. So, that doesn’t make any sense.

 

Let’s investigate this practically by writing a program.

Let’s write a small program to understand a string variable and a string literal or a string constant. 

#include<stdio.h>

int main(void)

{
    char msg1[] = "Hello how are you?";
    printf("Message is : %s\n",msg1);

    for(;;);

}

First store a string in my program. So, now let me create a char array. Let me call msg1 = “Hello how are you?”;

And after that, let me print this message. 

printf(“Message is : %s\n”, msg1); 

To print a string you should use a format specifier ‘%s’. And then just mention the string variable that is msg1. That’s it. 

You see in the SWV ITM Data Console the message is printed.

Figure 1. Code
Figure 1. Code

Now let’s analyze this variable. 

Now let’s see where exactly this variable is stored in the memory. For that, let’s take the help of a memory browser, but before that let’s check the variable window. Here you can see that the characters are stored in this array.  And the base address is 0x2001ffdc, so that’s actually a RAM location. That proves that our array is stored in RAM. 

Let’s go to the memory browser and type the base address 0x2001ffdc and here it shows that data, as shown in Figure 2. 

String literal
Figure 2. Output

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.