How are strings represented in the C language?

In the C language, strings can be represented using character arrays. A character array is a sequence of characters, with the last character being the null character (‘\0’), indicating the end of the string.

Here are some common ways of representing strings:

  1. Character array method:
char str[20] = "Hello World";

Here a character array called str is defined and initialized as “Hello World”.

  1. Pointer to character:
char *str = "Hello World";

Here is a pointer named “str” defined, pointing to the first address of the string “Hello World”.

  1. Read a string through the input function.
char str[20];
scanf("%s", str);

Here, the scanf function is used to read a string through input and store it in a character array str.

It is important to note that in the C language, strings are stored in the form of character arrays, with each character occupying one byte of memory space and ending with a null character. Each character in the string can be accessed and manipulated through indexing.

Leave a Reply 0

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


广告
Closing in 10 seconds
bannerAds