Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the basic use of C language string

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "C language string basic use case analysis". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. the concept of string

A string is a collection of ordered characters

String is one of the basic elements in a program.

There is no concept of string in C language.

Simulating string through special character array in C language

A string in C is an array of characters ending in'\ 0'.

Character array and string

In C, single or multiple characters referenced by double quotation marks are a special literal quantity.

Stored in the global read-only storage area of the program

It is essentially an array of characters, and the compiler automatically adds'\ 0' characters to the end.

Let's take a look at the code for an array of characters and strings:

# include int main () {char ca [] = {'Hello world,' eBay, 'lump,' lump,'o'}; char sa [] = {'weighted,' oaked, 'rented,' lumped, 'dumped,'\ 0'}; char ss [] = "Hello world!"; char* str = "Hello world!"; printf ("% s\ n", ca) Printf ("% s\ n", sa); printf ("% s\ n", ss); printf ("% s\ n", str); return 0;}

The output is as follows:

Because ca [] is an array of characters rather than a string, the formatted character% s means that what needs to be printed is a string, so printing ca is naturally wrong.

III. The secret of the literal quantity of a string

The essence of the literal quantity of a string is an array

The literal quantity of a string can be regarded as a constant pointer

Characters in the literal amount of a string cannot be changed

The literal amount of a string contains at least one character

"Hello World!" Is an array of nameless characters

Let's take a look at the literal code of a string:

# include int main () {char b = "abc" [0]; char c = * ("123" + 1); char t = * ""; printf ("% c\ n", b); printf ("% c\ n", c); printf ("% d\ n", t); printf ("% s\ n", "Hello"); printf ("% p\ n", "World"); return 0;

The output is as follows:

Notice that char t = * ""; it contains a'\ 0'

Fourth, the length of the string

The length of a string is the number of characters it contains.

String length refers to the number of characters that appear before the first'\ 0' character.

The length of the string is determined by the'\ 0' Terminator

The function strlen returns the length of the string

Here's a look at the use of strlen:

# include # include int main () {char s [] = "Hello\ 0world"; int iTuno; for (iTun0; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report