当前位置:网站首页>The initial C language framework is suitable for review and preliminary understanding

The initial C language framework is suitable for review and preliminary understanding

2022-04-23 14:25:00 KissKernel

First time to know c Language

In front of the word :


This blog is right c A summary of the language Framework , Suitable for review or preliminary understanding c Language people , Each part should be mentioned as much as possible, but not too detailed , If you want to know more about it, you can search the information by yourself or follow my blog post below , You can also discuss with me , Not much said , We enter the beginning c Language

Part one what is c Language and the first c I don't speak much about language programs , Go straight into , data type .

1, data type

char        // Character data type Occupied space 1 byte
short       // Short Occupied space 2 byte
int         // plastic Occupied space 4 byte
long        // Long integer Occupied space 4 byte
long long   // Longer plastic surgery Occupied space 8 byte
float       // Single-precision floating-point Occupied space 4 byte
double      // Double precision floating point Occupied space 8 byte
that C Does the language have string type ?
The answer is no ;
So what's the use of so many types ?
The answer is to express many situations in life
How do we use these types instead ?

char ch = 'z';
int weight = 120;
int salary = 20000;

In the above code, we define a character to represent the name , weight , Wages, etc , This is one of the most common types of data we use .

This involves creating variables

2. Variable

In the above code ch,weight,salary Are the variable names we created , Create variable names as long as they are not c The keywords of language can .
There are two other concepts about variables, that is :
(1) Global variables

int arr=10;
int main()
{
    
    printf("%d",arr);
    return 0;
}
  • This is the global variable , stay main Variables defined outside functions , Can be referenced throughout the program , Of course, if it is cross file reference, you should declare that the keyword is extern
    (2) local variable
int main()
{
    
    int arr;
    return 0;
}
  • The variables defined inside the braces, that is, within the code block, are called local variables , This leads to a point that if the local variable and the global variable have the same name , So in the range of local variables , A global variable with the same name does not work . But to avoid confusion, we usually don't write like this .

2.2 The scope and life cycle of variables

  • About scope : Generally speaking, variables are only useful when they are in this range. If they are out of this range, they will become invalid .
    1. The scope of a local variable is in the brace of the local variable
    1. The scope of global variables is the whole program
  • About the life cycle : This is the time from creation to destruction of variables , Of course, he doesn't mean how many milliseconds an absolute time is, but a relative time when the program runs
    1. The life cycle of a local variable is the beginning of the scope life cycle , Out of scope, the life cycle ends
    1. The life cycle of a global variable is the period during which the whole program is executed .
      Of course, there are some constant quantities in life, such as gender and so on , This leads to our constant .

3. Constant

First, constants have naming rules and cannot be defined arbitrarily .

1. The first is the shaping constant , It's actually decimal octal and hexadecimal integers

2. The second is a real constant , Floating point numbers . for example ,0.246 Or you could write it as . 246

There must be a decimal point , But before and after the decimal point 0 It can be omitted .
The second type of floating point number is exponential form . The format is as follows
mantissa E Index part
Or lower case e It's all OK. Pay attention to this place , The decimal part can be an integer or an exponent, but the right part of the decimal part must be an integer , Both positive and negative . give an example :
Correct :135e2、135e-2、.135e4
FALSE :e2、3.5e2.4、.e、e5、e

3. The third is the character constant

Character constants are enclosed in single quotation marks, including escape characters .

4. It's just a string constant

String constants are quoted in double quotation marks. Note that double quotation marks can be directly used by gods and demons .

5.const Modified constant variable

By const Modified variables have constant properties that cannot be changed , Read only . But he is essentially a variable ; give an example :

int main()
{
    
    const int n=10;
    int arr[10]={
    0};
    int brr[n]={
    0};
    return 0;

}
  • In fact, when you create it, you find brr The creation of will report an error , That explains it const Embellished n Has a constant attribute but is essentially a variable .

    6.#define Defined identifier constant

#define MAX 100
printf("%d",MAX);

This code constant 100 Replaced with MAX Is the identifier constant

6. Enumeration constants

enum color
{
    
    RED,
    BLUE,
    GREEN
};

In fact, enumeration constants have a value RED yes 0;BLUE yes 1; This value can be modified , However, from top to bottom, it is added by default .

4. character string , Escape character , notes

  • What is a string , A character enclosed in double quotation marks is a string . Note here that the end of the string will automatically add \0 To mark the end of a string . without \0 It may cause our cross-border visit .
  • Escape character , As the name suggests, it means to change the original meaning . There are many escape characters. Here we focus on two

\ddd // Here are three octal digits
\xdd // Here are two hexadecimal numbers

 In use, such as printing , First convert octal numbers into decimal numbers, and then treat them as ASCII The code value prints out characters , Hex is the same , In theory, as long as the number does not exceed ASCII The range of code values can be several digits . 
  • notes , See the demo directly L:
#include<stdio.h>

int main()
{
    
    printf("hello world\n");
}
//int main()
//{
    
// printf("hello world\n");
//}
/*int main() { printf("hello world\n"); }*/

There are two annotation styles above. One is /* This is a c The annotation format of language , The advantage is that you can annotate many lines at once , The disadvantage is that comments cannot be nested , End flag encountered … */ Will be over .

The other way is c++ The annotation style of // The double slash can comment at the beginning of any line , Can be nested , But commenting on a large piece of code can be cumbersome .

5, Select statement

There are always choices in life ,c Of course, there are in the language

1,if() sentence

Format :

if( If the judgment condition is true, execute 1 Statement is false execution 2)
{
    
1
}
else
{
    
2
}


if()
{
    

}
else if()// This is a c A multi branched structure of language 
{
    

}
else
{
    

}

if()// Omit else It's OK, too 
{
    

}

2,switch

switch It is suitable for multi branch structures, such as input 1 Print week 1… This is suitable for use switch Statement , If you use if Statement to write seven statements so much trouble .

switch(input)
{
    
    case 1:
    printf(" Monday \n");
    break;
    .....
    ...
    ..
    .
    case 7:
    printf(" Sunday \n");
}

This is it. switch The basic usage syntax of . it is to be noted that input Is an integer variable ;

6. Loop statement

Circulatory do while loop ,while loop , as well as for loop . The most used is for Cycle again is while loop ,do while Recycling is rare ;

1,for loop

int i=0;
for(i=0;i<10;i++)// The first expression is initialization , It's only going to be executed once , The second is that the judgment part will be compared with expression 2 after each execution. If it is true, the loop will continue, and if it is false, the loop will end , The third expression is hi It's the adjustment part .
{
    
     The loop body ;
}

2,while loop

int i=0;
while(i<10)// Judge the condition 
{
    
     The loop body ;
    i++;// Adjustment part 
}

3,do while loop

Do it first as your name suggests , First execute the loop body , Then judge .

int i=0;
do{
    
     The loop body ;
    i++;// adjustment 

}while(i<10);// There's a semicolon here , This part is the judgment part 

7, function

Functions are divided into user-defined functions and library functions .
The library function is simple printf,scanf Library functions .
The custom functions are as follows :

int add(int a,int b)
{
    
    return a+b;// This is the return value , To be the same as the return type of the function defined above .
}

Custom function design , Pay attention to the function return value and function parameters , Of course, it's OK. No , If there are no parameters and return values, it is set to void type . Function design should follow the principle of high cohesion and low coupling , Keep the function as simple as possible and reduce the association with external code , This can enhance the reusability of the code , Better portability .

8, Array

An array is a collection of elements of the same type ;

1, Definition of array

int arr[10]={
    0};// This defines an array of ten integer elements and initializes it to all 0;

2, The subscript of an array is from 0 At the beginning ;

3, Use of arrays

Now I use an array to save 10 Elements and print them out :

#include<stdio.h>
int main()
{
    
    int arr[10]={
    1,2,3,4,5,6,7,8,9,10};
    int i=0;
    for(i=0;i<10;i++)
    {
    
        printf("%d ",arr[i]);
    }
    return 0;
}

An array of the top ten elements 10 The subscript of is 9;

9, The operator

About operators , I'm just enumerating and not introducing them one by one. Later, a special blog will explain them in detail .

// arithmetic operator  
+ - * / %  
// Shift operator  
>> <<  
// Bit operators  
& ^ |  
// Assignment operator  
= += -= *= /= &= ^= |= >>= <<=  

// Monocular operators  
!  Logical anti operation   
-  negative 
+  Comes at a time 
&  Address fetch 
sizeof  The type length of the operands ( In bytes )
~  To reverse the binary of a number 
--  In front of 、 After --
++  In front of 、 After ++
*  Indirect access operators ( Dereference operator )
( type )  Cast  

// Relational operator 
>
>=
<
<=
!=  Used for testing “ It's not equal ”
==  Used for testing “ equal ”
// Logical operators 
&&  Logic and 
||  Logic or 
// Conditional operators 
exp1 ? exp2 : exp3
// Comma expression 
exp1, exp2, exp3, …expN
// Subscript reference 、 Function calls and structure members 
[] () . ->

10, Common keywords

Just meet me. I'll introduce it separately later, otherwise I can't control the length. I hope to understand .

auto break case char const continue default do double else enum
extern float for goto if int long register return short signed
sizeof static struct switch typedef union unsigned void volatile while

This is commonly used 32 Keywords in c99 The new 5 Keywords are not among them ;

11,#define Defined identifier constants and macros

#define MAX 100
#define ADD ((x)+(y))
#include<stdio.h>

int main()
{
    
    printf("%d",MAX);// What is printed here is 100,#define The defined identifier or macro is actually replaced by the following paragraph , Therefore, when defining macros, pay attention to adding parentheses to each replacement amount .
    printf("%d",ADD(2,3))// It's printed here 5, This is macro , Note that macros cannot be nested 
    return 0;
} 

版权声明
本文为[KissKernel]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231412251892.html