Overcoming the headaches of the C language

Source: Internet
Author: User

The C language is a total of 32 keywords, listed below:

1.auto declaring automatic variables

By default, all variables of the compiler default are auto

2.int declaring integer variables

3.double declaring double-precision variables

4.long declaring long integer variables

5.char declaring character-type variables

6.float declaring floating-point variables

7.short declaring short-integer variables

8.signed declaring a signed type variable

9.unsigned declaring unsigned type variables

The highest bit, if 1, indicates that the number is negative, and its value is added with the "-" sign for the value of the remaining bits except the highest bit;

If the highest bit is 0, it indicates that the number is positive and its value is the value of the remaining bit except the highest bit.

A 32-bit signed int type integer whose value representation range is:-2 (31) to (31)-1;

A 8-bit char type number whose value represents a range of-2 (7) to (7)-1;

A 32-bit unsigned int type integer whose value representation range is:-2 (32) to (32)-1;

A 8-bit char type number whose value represents a range of-2 (8) to (8)-1;

Compiler default variables are signed

10.struct declaring struct-body variables

11.union declaring federated data types

12.enum declaring enum types

13.static

Two effects

1. Modifier variables

Static all variables:

Global variables are inherently static, except that static global variables, scoped only to the defined file, are inaccessible to other files and extern, and more precisely, the scope is limited to the end of the file defined by this file, and the code before the definition of this file is inaccessible.

Static local variables:

The scope is limited to the definition of this function, and other functions cannot be called because static variables are stored in the static zone, so when the function is finished, the value of the static variable is not destroyed, and the function will use it the next time it is run.

to summarize:static changes the scope of global variables and changes the lifetime of local variables . .

2. Modifier functions

The function of the static modifier changes the scope of the function and is used only in this file, so it is also called an "intrinsic function", using intrinsic functions, without worrying about the names of functions written by others.

14.switch for switch statements

15.case Switch Statement Branch

The "other" branch in the 16DEFAULT switch statement

17.break jumps out of the current loop

(1) The break statement can be used only within the body of the loop and in the switch statement.

(2) When break appears in the body of the switch statement in the loop body, its function is simply to jump out of the switch statement body.

(3) when break appears in the loop body, but not in the switch statement body, then after the break, the body of the loop is out of the layer.
(4) can only be used in loop statements or switch statements to end the closest layer of loops .

18.register Declaring Register variables

This keyword requests the compiler to make the variables available in the CPU's internal registers as much as possible rather than through internal, stored addressing access to improve efficiency. Note that as far as possible, a CPU register is just a few or dozens of, if you define a lot of register variables, it may not be exhausted can not all put these variables into the register.

Although registers are very fast, there are some limitations to using the Register modifier:

The register variable must be a type that can be accepted by the CPU register. means that the register variable must be a single value, and its length should be less than or equal to the length of the integral type. The register variable may not be stored in memory, so the address of the register variable cannot be obtained with the fetch operator "&"

19.const declaring read-only variables

20.volatile explanatory variables can be implicitly changed in program execution

Volatile means that the variable may be modified by another program or other thread of the program, so the compiler cannot optimize it, and each read is fetched from the address. If this is not the case, the compiler can buffer it in the register, so that the other program modifies it, and the value inside the register is the original, you do not want the result. And the above three cases are this may be modified outside the program or this thread, so add it. Generally used in the following 3 cases.
1). Hardware registers for parallel devices (e.g., status registers)
2). Non-automatic variables that are accessed in an interrupt service subroutine (non-automatic variables)
3). Variables shared by several tasks in multi-threaded applications
The volatile keyword acts as a reminder that the compiler must carefully reread the value of the variable each time it is used, rather than using the backup stored in the Register In this way, the variables defined by the volatile variable are shared by the thread or interrupt service subroutine, otherwise, even if the variable is modified in the interrupt, the value that is read after the terminal returns is still the value before the modification, such a program will have such an unpredictable bug

21.typedef used to alias data types

22.extern declaring a variable is declared in another file

23.return subroutine return statement

24.void declaring a function with no return value or no argument, declaring a null type pointer

25continue ends the current loop and starts the next cycle

1) The general form of the Continue statement continue statement is: Contonue;
(2) Its function is to end this cycle, that is, the remainder of the loop is skipped the remaining outstanding statements, and then once again the condition of the loop to determine .
(3) Note: Executing the CONTINUE statement does not terminate the entire loop. In the while and do-while loops, the Continue statement causes the process to jump directly to the test portion of the loop control condition, and then decides whether the loop continues.
(4) in the For loop, after encountering continue, skips the remaining statements in the loop body, evaluates expression 3 in the For statement, and then tests the condition of expression 2.
Finally, based on the value of expression 2, determine whether the for loop executes. In the loop body, regardless of the statement component in which the continue is used, it is performed as described above, which differs from break.

Loop body for 26.do loop statements

Loop conditions for 27.while loop statements

28.if conditional statements

29.else Conditional Statement Negation Branch

30.for a looping statement

31.goto Unconditional Jump Statement

The amount of memory space that the 33.sizeof computing object occupies

Inti=0 (32-bit system test)

A), sizeof (int); B), sizeof (i); C), sizeof int;d), sizeof I;

A), B) has a value of 4. What about the C)? D)?

By debugging The result of D is also the answer to 4,c is wrong.

It is easy to conclude that sizeof is not a function .

sizeof the parentheses can be omitted when calculating the space size of the variable , and cannot be omitted when calculating the type (die) size .

Stacks and heaps of data structures

The stack is first known on the data structure , although we call it that, but the stack is actually two kinds of data structures: heap and stack. Heaps and stacks are a data structure in which data items are ordered sequentially.

Stacks are like buckets or chests of data.
We start from the familiar stack, it is a kind of data structure with the last-first-out nature, that is, after the storage of the first take, the first storage after the take. It's like we're going to take out something that's under the box (put in an earlier object), and we'll first remove the object that's pressed on it (the relatively late object put in).
Heap like a tree upside down
And the heap is different, the heap is a sort of tree data structure, each node has a value. Usually what we call a heap of data structures is a two fork heap. The heap is characterized by the minimum (or maximum) value of the root node, and the two subtrees of the root node are also a heap. Because of this feature of the heap, commonly used to achieve the priority queue, heap access is arbitrary, it is like we in the library shelves to pick up books, although the book is placed in order, but we do not want to take any of the same as the stack, first remove all the previous books, bookshelf This mechanism is different from the box, we can directly take out the book

The memory used by a program compiled by C + + is divided into the following sections:
1. Stack (stack)-Automatically allocated by the compiler to release, store the function parameter value, local variable value and so on . It operates in a manner similar to a stack in a data structure.

2, heap area (heap)-Generally by the programmer assigned to release, if the programmer does not release, the end of the program may be recycled by the OS. Note that it is not the same as the heap in the data structure, but the distribution is similar to the linked list.

3, Global Zone (Static)-, the storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another area adjacent. -released by the system after the program is finished.

4, literal constant area-the constant string is put here. Released by the system after the program is finished.

5. Program code area-binary code that holds the function body.

Pointer array array pointer pointer function function pointer

int *p[4];  An array of pointers. is an array of 4 elements, each of which is a pointer to an integral type . (each element of an array is a pointer)
int (*p) [4]; An array pointer.                It is a pointer to an array of 4 integral elements. (a pointer to an array with 4 integral elements)
int *func (void); pointer function.             A parameterless function that returns an integer pointer. (The return value of the function is int*)
Int (*func) (void);      Represents a function pointer, which can point to a function that has no parameters and returns a value of an integer pointer. (The return value of the function is int)

Interview question: What's the difference between A and &a

Please write the following code to print the results, the main purpose is to examine the difference between a and &a.

#include <stdio.h>
void Main (void)
{
int a[5]={1,2,3,4,5};
int *ptr= (int *) (&a+1);

printf ("%d,%d", * (a+1), * (ptr-1));
Return
}

Output results: 2, 5.
Note: The array name a can count the first address of the group, and &a is a pointer to the array. Think of the original int *ptr= (int *) (&a+1);

Instead of an int *ptr= (int *) (a+1), what will the output be?

Overcoming the headaches of the C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.