C language-supplements (1) Keywords && operators

Source: Internet
Author: User

Key words

extern static const typedef define sizeof Union


extern means "A variable already exists, but not within the current compilation unit and needs to be found in other compilation units." ”

extern writes as much as possible to the. h file for cross-scope data passing across multiple files, frequently in extern statements


Static defines a variable
1) Modified local variables are stored in the static data area
2) Modify the global variable (so that the variable can only be visible from the source file)
For a global variable, it can be accessed either in the source file or in other sources in the same project (simply declare with extern).
3) modifier functions, like global constants, change scope


Const is defined as a read-only variable

Const after ' * ' means that it is to modify the pointer variable itself, so it must be initialized at the time of declaration and cannot be pointed back to other variables.

(1) In C language with a const to modify a variable, indicating that the variable is read-only, not by explicitly call a to modify the value of a, and at this point A is a read-only variable, not equal to the constant;

It is treated as a constant in C + +.

(2) It is important to note that the const is in a different position when declaring the variable, and it may be very different in meaning.

If the const is to the left of ' * ', the value of the variable pointed to by the pointer is immutable;

If the const is to the right of ' * ', then the value of the pointer is immutable;

typedef is used to declare type aliases, which are often used to increase the readability of code in the actual writing process. It can be an alias for a long string of type names, so you can use this alias to achieve the same effect as the original type name.

#define INTPTR1 int*
int* INTPTR2;

INTPTR1 P1,P2;
INTPTR2 P3,P4;

sizeof is an operator that calculates the size of a length

1) If it is an expression, the parentheses can be omitted, but for the type, the parentheses cannot be saved;

2) sizeof calculates the occupied space, if acting on the expression, the expression is not evaluated, only based on the type conversion to evaluate the type of the expression, and the type of expression can be determined at compile time.

In an expression with a two-tuple operator, parentheses must be added, or sizeof will only spatially calculate the first operand


Union unions

1) A consortium is a structure;

2) All its members have an offset of 0 relative to the base address;

3) This structure space is large enough to accommodate the most "wide" members;

4) Its alignment shall be suitable for all of its members;

Conversion between different types avoids coercion of type conversions

IP address, before using the Union, take the address convenient

Union u{    Char s[9];    int n;    Double D;};
S is 9 bytes, n is 4 bytes, D is 8 bytes, so it requires at least 9 bytes of space. However, its actual size is not 9, and the operator sizeof tests its size to 16. This is because there is a problem with byte alignment, and 9 cannot be divisible by 4 or divisible by 8. So the bytes are added to 16 so that they align with the self of all members. It can be seen from here that the space occupied by a consortium depends not only on the widest member, but also on all members, i.e. its size must meet two conditions: 1) large enough to accommodate the widest member; 2) the size can be contained by allbasic Data type (Here S is 1) the size is divisible.

Operator

A total of 15 priority levels: 1   ()  []  .  ->2   !  ~   -(minus) + +-  -   & (take variable address) *   (type) (mandatory)    sizeof 3   */%4   +-5   >> < <  6   > >= < <= 7   = = =   8   &  9   ^  |  &&12  | |  :   + = =  *=  /=   %=  |= ^= &=   >>=   <<=15  , Binding: 2   is from  right to left and the   other is  left to right


C language-supplements (1) Keywords && operators

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.