Key Excerpt from C language (1 ~ 5)

Source: Internet
Author: User

I. Basic data types:

The data type can be understood as an alias with a fixed memory size. It is a model for creating variables.

A variable is an alias for an actual Continuous Bucket. In a program, you can apply for and name a bucket through a variable. You can use the bucket by using the variable name.

/*************************************** **************************************** **************************************** ***********************/


Ii. Auto, static, register attribute Modifier

Auto is the default attribute of a local variable. It exists in the stack and can be left empty.

Static modified variables (global and local variables are originally stored in the static area) are stored in the static area of the program.

Another significance of static modification is the File Scope identifier:

The scope of static global variables is only in the declared file. However, non-static global variables are valid in other source files.

The static modified function scope is only in the declared file. It cannot be used in other files and can be used for file encapsulation.

The register keyword indicates that the variable is stored in the register. The register only requests the register variable, but may not succeed.

The register variable must be a value acceptable to the CPU register and cannot exceed the maximum number of CPU digits.

You cannot use the & operator to obtain the address of the register variable because the register variable is stored in the register and the & operator obtains the memory address.

/*************************************** **************************************** **************************************** **************************/



Iii. Use of branch loop keywords

If:

Bool variables should appear directly in the condition and should not be compared. Because bool variables are not in the C language standard, you can define them by yourself, not necessarily 0 and 1.

When comparing variables and values, the value should be on the left to prevent errors.

Float variables cannot be compared with 0, so precision needs to be defined because floating point variables are stored discretely.

#define EPSINON  0.00001float   f=0.0;if((-EPSINON <= f)&&(f <= EPSINON)){}

Switch

The value in case can only be of integer or simplified type.


If and switch summary:

1. If is used in the case where "by piece" is required for determination

Switch is used to determine discrete values separately.

2. The if statement can replace the switch statement in function, but the switch statement cannot replace the if statement.

The switch statement is more concise for multi-branch judgment


Do ...... Usage of while (0:

1. Goto can be eliminated. In some functions, we often perform some final work before the return function, such as removing a function and starting the memory of malloc,

Goto has always been a simple method:

int foo(){    somestruct* ptr = malloc(...);     dosomething...;    if(error)    {        goto END;    }     dosomething...;    if(error)    {        goto END;    }    dosomething...; END:    free(ptr);    return 0; }


But do {……} is used {......} While (0) can avoid using GOTO

int foo(){     somestruct* ptr = malloc(...);     do{        dosomething...;        if(error)        {            break;        }         dosomething...;        if(error)        {            break;        }        dosomething...;    }while(0);     free(ptr);    return 0; }

2. Use do {......} While (0), define an empty macro to avoid warning

#define EMPTYMICRO do{}while(0)


3. Assist in defining complex macros

This defines a macro,

#define DOSOMETHING()\               foo1();\               foo2();


The intention is to use dosomething () to replace the two functions, but in some places, it will have unexpected consequences.

if(a>0)    DOSOMETHING();


After expansion, it will be:

if(a>0)    foo1();foo2();

This is not the result we want.

We can use do ...... While (0)

#define DOSOMETHING() \        do{ \          foo1();\          foo2();\        }while(0)\    ... if(a>0)    DOSOMETHING(); ...


GCC can also be defined as follows:

#define DOSOMETHING() ({\        foo1(); \        foo2(); \})

/*************************************** **************************************** **************************************** ***************************/

4. Goto, void, extern, sizeof

Goto: basic unavailable

Void:

If the function does not return a value, it must be declared as void. Because the default return value is int, it must be declared as void if there is no parameter.

The void variable does not exist.

Void pointer ---- A pointer of the same type can be assigned a value. A void pointer can accept any type of pointer on the left and assign a value on the right to other pointers for forced conversion.

For example:

        extern void *malloc(unsigned int num_bytes); void *memset(void *s, int ch, size_t n);

Int * P = (int *) malloc (sizeof (INT); void * is universal when the pointer is used for memory operations.

Extern: Used to declare variables and Functions Defined externally; used to compile a piece of code in C mode by the compiler

extern “C”{ ……}

Sizeof: it is not a function. It is a built-in indicator. The result has been calculated during compilation and the memory occupied by the calculation variable. For example, the following code shows that it is not a function:

#include <stdio.h>int main(){      int a;        printf("%d\n", sizeof(a));    printf("%d\n", sizeof a);    printf("%d\n", sizeof(int));        return 0;}

Output:, 4, but in the form of () for unified writing.

/*************************************** **************************************** ******************************/

V. Const and volatile

The variable modified by const is read-only and will occupy space in the memory, but it is still a variable. It is read-only during compilation, but it is invalid during runtime. It can be modified through the pointer during runtime.

  1 #include<stdio.h>    2 #include<malloc.h>    3 int main()-   4 {|   5 const int i=1;                                                            |   6 int *p = (int *)&i;|   7 printf("%d\n",i);|   8 |   9 *p = 3;|  10 printf("%d\n",i);|  11 return 0;|  12 |  13 |  14 }
gexueyuan@gexueyuan:~$ ./a.out 13

The const modified array is read-only, and the const modified array space cannot be changed (possibly program crashes, compiler-related)

Const int A [5] = {1, 2, 3, 4, 5}; int * P = (int *) A; int I = 0; for (I = 0; I <5; I ++) {P [I] = 5-I; // oops may occur, related to the compiler}

The const modifier pointer takes the * field as the bounds. When the const is on the left, the modifier Pointer Points to the variable as the read-only variable, and on the right, the modifier pointer itself is read-only.


Const int * P; // P variable. The content pointed to by P cannot be changed.

Int const * P; // P variable. The content pointed to by P cannot be changed.

Int * const P; // P is unchangeable, And the content pointed to by P is variable.

Const int * const P; // The content pointed to by P and P cannot be changed.


Const modifier function parameters and return values

Const modifies the function parameter, indicating that the parameter value does not need to be changed in the function body.

The Return Value of the const modifier function indicates that the return value cannot be changed and is mostly used to return pointers.


The volatile variable is the warning indicator of the compiler. It tells the compiler to take a value out of memory each time and is mainly used to modify the variables accessed by multiple threads.

Modifies the variable modified by an unknown factor.




Related Article

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.