How to define strings in the C language?

In C language, strings can be represented using character arrays. There are two ways to define a string:

  1. Use an array of characters:
char str[] = "Hello, world!";

In this example, the character array “str” will automatically allocate enough space based on the length of the string, and copy the string into the array.

  1. Using character pointers:
char *str = "Hello, world!";

In this example, the character pointer str points to the first address of the string constant “Hello, world!”

It is important to note that strings defined with character arrays can be modified, while strings defined with character pointers cannot. If you need to modify the string content, you should use a character array.

bannerAds