How to define strings in the C programming language?

In the C programming language, strings are defined as arrays of characters. Arrays of characters can be used to store and manipulate strings.

Here are some common ways to define strings:

  1. Initialize a string using a character array.
char str[] = "Hello, World!";
  1. Define a string using a character array, and assign values to it one by one.
char str[14];
str[0] = 'H';
str[1] = 'e';
str[2] = 'l';
str[3] = 'l';
str[4] = 'o';
str[5] = ',';
str[6] = ' ';
str[7] = 'W';
str[8] = 'o';
str[9] = 'r';
str[10] = 'l';
str[11] = 'd';
str[12] = '!';
str[13] = '\0'; // 字符串以'\0'结尾,表示字符串的结束
  1. Define a string using pointers and assign a value with a string constant.
char *str = "Hello, World!";

It is important to note that in C language, strings are stored in the form of character arrays, and the last character must be a special character ‘\0’ to indicate the end of the string. This ‘\0’ character is referred to as the string termination character in C language.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds