"Kitty Cat Classroom" 4 - Arrays, operators, common keywords

It’s not easy to update, so please give me likes, your questions are welcome, and thank you for forwarding,

Finally, follow me, follow me, follow me, you will see more interesting blogs! ! !

Meow meow meow, you are really important to me.

Table of contents

Preface

 Array

definition

 Array subscript

Use of arrays 

Operator 

arithmetic operators

displacement operator

Bit operators

assignment operator

Unary operator

Relational operators

Logical operators

conditional operator

comma expression

Subscript references, function calls, and structure members

Common keywords

keyword typedef

Keyword static

Modify local variables

Modify global variables

modified function

#defineDefine constants and macros

Summarize


Preface

This time we talk about arrays, operators, common keywords, and #define constants and macros.

Meow meow meow, let’s get started!


 Array

  • definition

int arr[10] = {1,2,3,4,5,6,7,8,9,10};//Define an integer array with a maximum of 10 elements.

Among them "data type + arr [number of elements] = {display of each element}; " is the definition of the array

  •  Array subscript

C language stipulates that each element of the array has a subscript, the subscript starts from 0.

Arrays can be accessed through subscripts

  • Use of arrays 


Operator 

Let’s learn about the simple and practical ones first, kids.

arithmetic operators

+    -    *    /    %


displacement operator

>>     <<


Bit operators

&   ^   |


assignment operator

=    +=    -=    *=    /=    &=    ^=     |=       >>=      <<=


Unary operator

! Logical reverse operation

- Negative value

+ Positive value

&                         Get the address

sizeof The type length of the operand (in bytes)

!

--                                                                                                                                              but

++                                                                                                                                     

* Indirect access operator (dereference operator) For example *p - dereference application pointer

(Type) Forced type conversion


Relational operators

>=

<

<=

!= is used to test "inequality"

== is used to test "equality"


Logical operators

&& Logical AND

|| Logical OR


conditional operator

exp1 ? exp2 : exp3


comma expression

exp1 ,exp2 ,exp3,.....expN


Subscript references, function calls, and structure members

[ ]    ( )   .    ->

-> is a whole, which is used to point to structures, classes in C++ and other pointers containing sub-data to obtain sub-data. In other words, if we define a structure in C language and then declare a pointer pointing to the structure, then if we want to use the pointer to retrieve the data in the structure, we must use "->".


Common keywords

C language provides a wealth of keywords. Thesekeywords are pre-set by the language itself,< a i=3>Users themselves cannot create keywords.

Let’s first introduce a few operators that are both unfamiliar and useful.

keyword typedef

As the name suggests, typedef is a type definition, which should be understood as type renaming.

Keyword static

In C language: static is used to modify variables and functions

1. Modify local variables - called static local variables

2. Modify global variables - called static global variables

3. Modified functions - called static functions

Modify local variables

Compare the effects of code 1 and code 2 to understand the meaning of static modified local variables.

Conclusion: static modification of local variables changes the life cycle of the variables

Let static local variables still exist when they go out of scope. The life cycle will not end until the program ends.

 Baozi pay attention to the different roles that static plays in almost the same code. static only applies the scope of i to test(), so i in the main function has no effect and i is not redefined. The value of i can be continuously added. This is the best way to use static to modify local variables in sub-functions. Isn't it very useful? It's simply black technology.

Modify global variables

dev version

vs2019 version

Code 1 is normal, but code 2 will cause a connectivity error during compilation.

Conclusion: A global variable is modified by static, so that the global variable can only be used in this source file and cannot be used in other source files.​ 

Um, Xiaomiao tried it, and I feel there is no change. Maybe the code is too small, or the compilers are becoming more and more mature, which can avoid some errors. But please don’t be careless. We’d better not do this.

modified function

But code 2 will report an error

 Code 1 is normal, but code 2 will cause a connectivity error during compilation.

Conclusion: A function is modified static, so that this function can only be used in this source file and cannot be used in other source files.

So there are advantages and disadvantages to static. Don’t use it casually. Xiaomiao thinks the best way is to modify local variables. Children, let’s practice it quickly!

The remaining keywords will be explained one after another in subsequent courses. It will take a long time, so let’s take our time!


#defineDefine constants and macros

 The first step in compiling a C language program is the preprocessing stage. This stage is where the macros come into play. The C preprocessor performs some textual operations on the source code before compilation. The main tasks include deleting comments, inserting the contents of files #included, defining and replacing symbols defined by #define, and determining whether part of the code is based on conditions. Compile (#if) to compile. The operation of "textual nature" means that one piece of text is replaced by another piece of text, regardless of any semantic content. Macros are just a text replacement tool in the C preprocessing stage. They are not visible tobinarycode after compilation.

Xiaomiao will elaborate in detail later, this is a bit much.


Summarize

Today we will come to an end. Next time we will talk about pointers and structures. After finishing that section, our first round is over. We will start the second round immediately. We will carefully explain some important things, such as statements. Functions, pointers, structures, oh, and the most important soul - debugging, you can preview it in advance, I'm really looking forward to it! ! ! The three rounds should start updating at the same time (Meow will mark it specially). Yes, Xiao Meow is desperate for his life. As long as his liver is not dead, he will die. Hahahaha, his brain is going to be broken.

I sincerely hope, Baozi, that after learning the three rounds and doing relevant exercises, Baozi will be satisfied!

Your future self will thank you now.


It’s not easy to update, so please give me likes, your questions are welcome, and thank you for forwarding,

Finally, follow me, follow me, follow me, you will see more interesting blogs! ! !

Meow meow meow, you are really important to me. Aaah! ! !

Guess you like

Origin blog.csdn.net/ormstq/article/details/128520177