C programming language - Keyword

Common (return, extern etc.)

return to return function's return value

extern: indicates that the variable or function is defined in another file, suggesting that the definition of the compiler to look in other files.

extern “c”:为了能够正确实现C++代码调用其他C语言代码。
//不可以将extern"C" 添加在函数内部

break; continue; struct data type definition structure
cycle number sentence structure, void-free type data, sizoef calculation expression or data type byte occupies

unsigned defined unsigned data, signed signed number, auto local variable (automatic storage)
and often Type Data Type


const (modified type data length)

To replace the pre-compiler directives, eliminating its disadvantages, while inheriting its advantages, the value of variable or object type often can not be updated.

const constant data type, the compiler can perform type-safe checking, debugging tools to debug the const constant.


static (defined static variables)

1. static memory type:
internal function definition (static local variable): only defined once, there is a static area of memory variables, functions, static variables end value is not destroyed, it can still function the next run with.

Defined outside the function (static global variable): variable scope only in the variable definition file, the other files can not be extern by reference.

2 internal link attribute
static functions can only be used in a statement on its source file.

typedef name change

typedef (as a new data type definition name), comprising a base type / custom type (struct, etc.)

A int 1.typedef;
⇒ A appears later appear identical to int (replacement character), A is type

A struct STR 2.typedef;
⇒struct STR is defined a structure type, change the name A (also type)

3.typedef struct str {} * A;
equivalent to typedef (struct str *) A
A is a pointer to a pointer of type struct str type

4.typedef int A [5];
equivalent to int typedef [. 5] A;
A is a pointer to the array of five integer variable type


The if statement (ie, non-zero execution)

Boolean variable: if (false) does not execute the statement following the
true to be translated (defined) as 1, false to be translated to zero
means that if the judgment on whether the nature 0

Pointer variable: if (NULL) behind the statements are not executed
NULL is defined as 0
Note unassigned (wild pointer, randomly oriented), executes the following statements

Integer variable: if (0) behind the statements are not executed
Note int a; if (a) executes the statement following
a pointer to a random number, a corresponding memory space is not 0

if (-1), if (100) executes the following statements

java supported only if (bool expression)


switch case

The switch statement principle: Jump to the implementation of the rest of the statement caseX position, or meet up until the last break.
switch case will generate a jump table to indicate the actual address of the branch of the case, and if ... else it takes to traverse a hit until the conditional branch condition,

  int k=1;
    switch(k)
    {
    case 1:
    case 2: printf("执行2");
    case 3:printf("执行3");break;
    case 4:printf("不执行4");
    }

Jump to the first case, there is no break and then jump to the implementation of the rest of the sentence

    int k=3;
    switch(k)
    {
    case 1:
    default: printf("不执行default");
    case 3:printf("执行3");break;
    }

Loop structure

while (expression)

Where expression is cycling conditions, the statement loop.
Value calculation expression, when the value is true (non-zero), execution loop, then determining the expression, until the expression is false (0) cycle ends.

while(2)
{
 cout<<"hello";break;
}

do statement; while (expression);

To execute a loop, in determining the value of expression, if it is true, the loop continues; otherwise abort cycle.
do-while loop executed at least once

do{
    cout<<"hello";
}
while(0);

for loop

Form: for (;;) statement;

General syntax:
for (initialization; conditional expression; increment) {} loop
where expression may be omitted, but can not be omitted semicolons.

Expression 1: initialization, is an assignment, which is used to loop control variable initial value;

Expression 2: conditional expression is a relational expression, which determines when to exit the loop;

Expression 3: Incremental, defined loop control variables once per cycle after a change in what way.


The difference between the conversion cycle

for: know the number of cycles after the first cycle of judgment

while: I do not know the number of cycles after the first cycle of judgment

dowhile: I do not know the number of cycles. After the first cycle judgment

while sometimes can not be exchanged for, for can be replaced while.
Because the for loop is suitable for known cycles, while cycles also applies to unknown number of cycles of time



Skip cycle (break, continue)

Skip Current cycle:
1) continue using the current loop to skip directly into the next cycle
2) used to break out of the current cycle; for the end of the current cycle

For out of multiple loop:
1) label statement
out of the loop for multi-:

   jump:
    for (var i = 0; i < 3; i++){
      for (var j = 0; j < 3; j++){
        if (i === 1 && j === 1) {
          break jump;
        }

Block out:

jump: {
  console.log(1);
  break jump; 
  console.log('不会输出');
}

2) Use function

function jumpFor() {
  for(var k=0;k<8;k++){
     for(var j=0;j<4;j++){
        if(k == j ){return false;}
        console.log(k,j)
     }
   }
}



type of data

Define a variable without assigning

NULL point whitespace character output
integer pointer have memory space, random point (the last used memory space)

int a; a random value (corresponding memory address)

int * p; random number points to an address (pointer field, a corresponding memory space)

  char *p;
    printf("%c",p);//输出D
    int a;
    printf("%d",a);//输出8

To avoid a NULL pointer field (C and C ++ language is replaced by default 0).
int * p = NULL equal int * p = 0;
that is, pointing to a null pointer. The p value of 0, a corresponding address.



1. The basic data types

Numeric types

Plastic (represents an integer: short, int, long)

The number of bytes occupied by a data type, referred to the length of the data type. For example, short occupies 2 bytes of memory, then its length is 2.

short int a =3;
    printf("%d\n",sizeof(a)); // 输出为2

Short or short int Short Integer
Length: 2 bytes may represent a fixed positive number of 2 ^ 16 -1 (which may represent the same number of re-negative)

Int and long could not be determined, behave differently in different environments.

Shaping int
int is not less than the short, generally 4 bytes

Long long
length not less than long int. Usually the compiler 32 to compile is 4 bytes long, 64 bytes compiled to 8
Note: 64-bit 32-bit software compatible

long long type
fixed length of 8 bytes


To sum up, their length (Number of Bytes) relationship:
2 ≤ ≤ int ≤ Long Short

Generally. 8. 4 64 2
32 to 244



Signed integer, unsigned integer
unsigned integer to signed integers results Default is unsigned (using complement arithmetic)

% D output, printf the default number is a signed integer (unsigned may be converted to signed numbers output)

unsigned i=-10; //unsigned:无符号整数

printf("%d",i);//输出-10



Float / real (a real number: float, double)
    printf("%f\n",b);  // 默认输出小数和点后六位
    printf("%.6f\n",b); // 输出小数和点后六位

REAL: rational numbers and irrational numbers general term including integer and decimal points

Single-precision floating point type float: 32 bit 4bytes
. 6 digits after the decimal point can be output

Double precision floating point type double: 64 bit 8bytes
15 digits after the decimal point can be output



Character (each character: char 1byte)

1, only a single quote character

2, correspond to the ASCII code (char type corresponding to the type of shaping a small range, 0-127)
as ASCII codes 98 correspond with each other and 'b',

 char ch=98;
                printf("%c\n",ch);//输出b
                printf("%d\n",ch);//输出98

 int a='b';
                printf("%d\n",a);//输出98
                printf("%c\n",a);//输出b

Since the character corresponding to the ASCII equivalent, so that characters can be integer arithmetic / character ASCII code is understood to operation

printf("%c\n",'a'+1);//输出b
                printf("%d\n",'a'+1);//输出98
                
printf("%d\n",'a'+'b');  //输出195(97+98)
				printf("%c\n",'a'+'b');  //输出?(97+98,没有对应的ASCII码)

printf("%d",'1'+'2');  //输出99(49+50)
				printf("%c",'1'+'2');  //输出c(ASCII码为99)

'0' - '9': ASCII codes from 48 to 57 is
'A': the ASCII code 65
'A': the ASCII code 97

//输出ASCII码为0的字符
printf("%c\n",0);//输出空白,实际是(null)
                printf("%s\n",0);//输出(null)




String (C ++)


2. pointer type (equivalent machine word length, such as 32 4bytes)

1) Array Pointer (pointer array, and writable)

Array Pointer (i.e., an array of pointers is a pointer to the array, the pointer is stored in the base address of the array)

When a pointer variable is initialized to the name of the array, said pointer variable that points to the array (p stored in the first address str)

The char str [20], * p; p = str;


2) Character pointers (C language, not writable)

char * str = “I love China!”;

char * str; str = “I love China!”;

Point to a fixed constant string (address of the first character string, i.e., the first address) non-writable read only (no longer replaced with other characters)

Char str pointer to make a command input string, the string will be all the constant output. (Printing the first character will automatically print the complete string)


String pointer array value and output:

* (P + i), p [i] is the first (i + 1) th element value

printf("%d\n",*p++);  //先*p,再p++

printf("%d\n",*(p++)); //先*p,再p++

printf("%d\n",(*p)--);  //先*p,再*p指向的值-1

C Output:
printf(“%s”,p);  printf(p); →输出字符串(p指向的字符串)

printf(“%d”,p);   →输出指针p值

C ++ Output:

cout <<*p ; →输出字符串(指针指向的内存空间)

cout <<p  ;  →输出指针中存放的地址

3. constructed type

Array

statement:type arrayName [ arraySize ];

arraySize must be greater than a constant integer zero, type may be any valid C / C ++ data types

int ch[5]; //通用,声明数组需设置内存空间

ch[0]=1;  //通用,声明后赋值

int b[]={1,23,4}; //通用,声明时初始化赋值,内存空间为成员数量

int c[5]={1,23,4}; //通用,声明时初始化赋值,内存空间为5

char c[10]="12312312";  //通用

string d[10]={"123","456"};  //仅C++

Character array (instead of strings in C)

Since there is no specific C language string type, we usually put the string in an array of characters

char b[10]={'1','2','3'}; //通用

char c[]="12312312";  //通用



Structure

struct structure: for a collection of different data can be stored in
C language structure can not contain the function, the function pointer can contain.
Structure can comprise a C ++ function

Collection: element only "belong to a set of" relationship

Structure comprises a plurality of variables or array variable, called members of the structure (each member theoretically contiguous in memory)

1) directly define the structure
as strut stu{int num;char *name;}stu1;
a member can not initialize when defining
member type is a base type may also be configured to other types of

struct stu structural body type name (type define new data / structure type)

stu1 structure type is of variable


If stu structural body using only one variable STU1 enough not intend to define a new variable name of the definition of the structure may be omitted, leaving only the variable name as strut {} stu1;

2) definition of the name change + typedef
typedef struct Stu {} a;

Is a structure type struct Stu increase (change) a new name A
→ struct Stu, A is the name of the structure type.

It can be omitted: may be omitted (a type name as required) when (Stu) defined

It can not be omitted: but if the definition of the structure, the internal structure of the type required by itself (name) defined variable structure name may not itself be omitted

Renaming structure type pointer
typedef struct Stu {} * a;

⇔ typedef (struct Stu *)a;

The struct Stu pointer type (such as plastic pointer type, int *)
change a name

A a1; defines a pointer variable of type struct Stu a1

3) the structure variable type declaration
① the variables are as defined in the structure declarations strut stu stu1;

② declare variables when defining the structure as strut stu {} stu1;

If stu structural body using only one variable STU1 enough
not intend to define a new variable name of the definition of the structure may be omitted, leaving only the variable name as strut {} stu1;

③ using typedef statement after renaming variables
typedef struct STU A
A STU1 (A STU is the structure type struct)
STU1 is the structure type of variable declarations

Assignments and access to members
初始化
struct tag
{chara;int b;
}x = {‘A’, 1};/*初始化*/

Press the field, the variable name. Member name
as output stu1.name

By pointers to access
node structure defines a node pointer to a particular type of structure variables
such as elm * p = & stu1; (equivalent to elem * p; p = & stu1 ;)

By (* p) .name access stu1.name:

(* P) ​​.name ⇔ p → name: p indicated structure variables (nodes) in the name member

(. 5) Notes
① (overall assignment / may be carried out only with the array when defining the overall)
as strut {} stu1 = {12, ' Li wave'};
use only by one process variable assignment members

② structure is a self-defined data types, variables are created template, do not take up memory space, structure variable contains the real data, you need memory space to store.

4. Type void space



Published 46 original articles · won praise 15 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_41850194/article/details/105315834
Recommended