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

How to parse constants and strings in C language

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to parse C language constants and strings, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Constant type

In the C language, we often use the following constants:

Literal constant

Const constant

# identifier constants defined by define

Enumeration constant

The code is as follows

# includeenum {/ / use enum to create enumeration constants papa,// does not assign values from 0 by default, if assigned, then increment mama, me, familynumber}; int main () {3.1415926 const int constant / ordinary literal constant const int a = 100 beat / define const constant printf ("aversion% d\ n", a) / an is defined by const and becomes unmodifiable # define T 999 printf ("t% d\ n", T); / / using the macro definition constant T (both define identifier constant) printf ("familynumber=%d", familynumber); / / the application of the enumeration constant, print out a shaping return 0;}

After defining a constant with const, if you still assign a value to the constant, the following will occur

Attention!

At this point, this constant is neither a variable nor a constant!

Because, if we use this constant to define the size of an array, the system will also report an error

String

In C language, char is used to define character type data variables.

We know that, for example,'a character,'is a character.

So what is a string?

In fact, a string is a number of characters.

"Hello World!"

This is a simple string.

The end flag of a string

Char arr1 [] = {"abcd"}; char arr2 [] = {# # * * $$}}; char arr3 [] = {'axiaqiaoyuanzhongyuzhuangzhuangyuanzhongyuzhuo]; printf ("% s\ n", arr1); printf ("% s\ n", arr2); printf ("% s\ n", arr3)

At this point, the print result is

There are four characters in arr2. Compared with arr3, the closing flag\ 0 is missing.

So there is a classic scalding, which is due to the lack of an end flag, which will lead to data overflow.

So why is there nothing wrong with arr1?

Because arr1 is a string of characters, there is an ending standard\ 0 hidden at the end of the string, but we just can't see it.

On the length of a string

We learned earlier that we want to know how much memory a data type takes up.

Need to use the library function sizeof

So if we want to get the length of a string, what should we use?

Strlen

Int main () {printf ("% d\ n", strlen ("abcd")); / / use the library function strlen to get the length of the string, note;\ 0 will not be counted in return 0;}

So, we get the length of the string, 4.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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