C Language Notes (2), C Language notes

Source: Internet
Author: User

C Language Notes (2), C Language notes
Note

The compiler replaces the original comments in the Code with spaces and runs the pre-processing command before
/*... */Annotations in this form cannot be nested.
As long as there is no space between the slash (/) and the Star (*), it will be treated as the start of the comment. For example:
Y = x/* p;

\ Is a connection character, indicating that the row is broken. The compiler removes the backslash and the characters following the backslash are automatically followed by the previous line. However, note: there must be no space after the backslash and no space before the next line of the backslash. (Some compilers use spaces ). \ Can also be used as the start identifier of escape characters.

 

Bitwise operators

Exchange the values of two variables: a ^ = B; B ^ = a; a ^ = B;

0x01 <2 + 3; // = 32 // "+" has a higher priority than the Shift Operator

In a 32-bit system:

0x01<<2+30;0x01<<2-3;

The number of digits between the left and right shifts cannot be greater than or equal to 0. (Some compilers can still pass)

For the number of symbols, when you move the value> right,The symbol bit will be moved along. When the number is positive, the maximum bit is 0;

When it is a negative number, the sign bit is 1, and the maximum bit is 0 or 1, depending on the requirements of the compilation system. Turbo C and many systems require completing 1.

 

Common bitwise operation macros:

#define  SETBIT(x, y)  ((x) |=  (y))#define  CLRBIT(x, y)  ((x) &= ~(y))#define TOGLBIT(x, y)  ((x) ^=  (y))#define TESTBIT(x, y)  ((x) &   (y))
#define SETBIT(x, y)  ((x) |= 1<<(y))#define CLRBIT(x, y)  ((x) &= ~(1<<(y)))

 

Curly brackets
char a[10]{ = “abcde”};

Curly brackets are used for packaging.

 

Auto addition and subtraction ++ ,--
I = 3; (++ I) + (++ I); // undefined behavior, depending on the compiler // TCC: 15, VC6: 16, GCC: 18
i=3;
(i++)+(i++)+(i++);

The auto-increment or auto-increment operation ends in the current calculation unit (Encounter, or;) And then auto-increment or auto-increment.

int i,j;i=0;j=(i++,i++,i++);    //j=2,i=3i=0;j=(++i,++i,++i);    //j=3

 

// And %

Two groups of Computing

3/2; (-3)/2; 3/(-2); (-3)/(-2);3%2; (-3)%2; 3%(-2); (-3)%(-2);

 

Priority

. [] And () have higher priority *
= And! = Operation with a higher priority
= And! = The priority is higher than the value assignment.
The arithmetic operator is higher than the displacement operator.

 

Preprocessing

Pre-processing commands are the functions of the compiler and are not part of the C language.

Macro definition

ANSI Standard C also defines the following macros:
_ LINE _ indicates the row number of the file being compiled
_ FILE _ indicates the name of the FILE being compiled.
_ DATE _ indicates the DATE string at the time of compilation, for example, "25 Dec 2007"
_ TIME _ indicates the TIME string at the TIME of compilation, for example, "12:30:55"
_ STDC _ determine whether the file is defined as a standard C program

The following statements are correct:

#define EMPTY# define EMPTY

When macro functions are used, spaces between macro names and parameters are ignored by the compiler. Spaces are valid when defining macros.

 

# ErrorThe purpose of the pre-processing command is to generate a compilation error message and stop compilation if # error is encountered during program compilation. The syntax format is:
# Error-message

 

# LineIs used to change the current number of lines and file names, which are pre-defined identifiers in the Compilation Program. The basic command format is as follows:
# Line number ["filename"]

 

# PragmaThe purpose of an instruction is to set the status of the compiler or to instruct the compiler to complete certain actions.

 

##All are pre-processing commands.

 

Assert
#if !defined(NDBUG)#define assert(p)    if(!(p)){fprintf(stderr,\        "Assertion failed: %s, file %s, line %d\n",\        #p, __FILE__, __LINE__);abort();}#else#define assert(p)#endif

 

Main Function

Writing without Parameters

int main(void)

Writing with Parameters

int main(int argc, char *argv[])int main(int argc, char **argv)

Argc -- the total number of command line parameters, includingExecutable program name

Argv [0] -- Name of the executable program

Argv [I] -- The I Parameter

Example
# ./a.out Love Live !

In this example, argc is 4, argv [0] = a. out, argv [1] = Love, argv [2] = Live, argv [3] =!

 

Array int a [5];

When defining an array a, the compiler allocates a memory block of determined size based on the specified number of elements and element type, and names the memory block.
Once the name a matches the memory, it cannot be changed. A [0], a [1], and so on are elements of a, but they are not element names. Each element of the array has no name.

& A [0] And & a [I]

A [0] is an element, and a is the entire array. Although & a [0] And & a have the same value, they have different meanings.

The former is an array.First element addressWhile the latter isFirst address of the array.

Differences between array name a as the left value and the right value

When a is used as the right value, its meaning is the same as that of & a [0], representing an array.First element addressInstead of the first address of the array.

A cannot be left!You can only access an element of an array, but cannot access an array as a whole.

 

& A [0] And & a [II]
int main(){    char a[5] = {'A','B','C','D'};    char (*p3)[5] = &a;    char (*p4)[5] = a;    return 0;}

Both p3 and p4 are array pointers pointing to the entire array. The Data Types on both sides of the "=" definition of p3 are completely consistent, while the data types on both sides of the "=" definition of p4 are inconsistent. The type on the left is the pointer to the entire array, and the data type on the right is the pointer to a single character. However, since & a and a have the same value, and the compiler only obtains the value of the variable when the variable is used as the right value, there is no problem in running.

What are the values of p3 + 1 and p4 + 1?

 

In C, when a one-dimensional array is used as a function parameter, the compiler always parses it into a pointer pointing to the first address of its first element.

 

Pointer

Causes of wild pointers

The pointer variable is not initialized when it is defined; the pointer is not empty after it is released. Free and delete only release the memory indicated by the pointer, but they do not kill the pointer itself. The Pointer Points to the "junk" memory. The released pointer should be set to NULL to avoid "wild pointer ".

Usage of pointers

1. It is best to initialize NULL when defining pointer variables,
Char * p = NULL;
2. After the pointer is used up, set the value of the pointer variable to NULL,
Free (p );
P = NULL;

 

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.