Dark Horse programmer--c Language Basic grammar

Source: Internet
Author: User

Tag: Style color Using strong SP file data on problem

1. Variable name and variable value

the variable name is actually a symbolic address.  When the program compiles, the system gives the variable the memory space needed for the variable's corresponding type (to store the value of the variable), and then the system establishes a mapping from the variable name to the first address of the memory space, so that the value of the variable can be accessed through the variable name. Variable names consist of numbers, letters, and underscores. Case sensitive, cannot start with a number. The length of the variable name (the number of characters) is not rigidly defined, except that different C compilers may have different requirements. However, this should not be a matter for everyone to consider, I believe no one will write such a long variable name to

In the C language, you must define the variable before you use it, or the compilation will fail. When defining a variable, specify the type of the variable to which it belongs. When the program compiles, it determines how much memory space is opened up for the variable according to the type of the variable, and it can also check the legitimacy of the operation. For example, the actual data can not do the remainder of the operation.

2. Integral type data

In the C language, there are three types of integer data: Short,int,long; There are two types of attributes: signed,unsigned. There are six different combinations.

length of the integer data type: The C standard does not rigidly stipulate the length of each type, but only specifies that the length of the long is either less than the length of int;short and not greater than int. The general C compiler will store an int type of data in a machine word.

3. Variable initialization

the initialization of a variable is not the allocation of memory space during compilation (only static variables and external variables are initialized during compilation), but only when the function is executed while the program is running.

4. Format input and output

%md,%mc,%ms (M is a positive integer): The size of M determines the width of the output content, when the output content is less than m, the output is right-aligned, left-fill space, when the output content is longer than M, the output is not limited by M, output as-is.

%-md,%-mc,%-ms: Use the same as above, just the output content left-aligned, right-fill the space.
%m.nf,%m.ns:m control the total width of the output content, for the real data, the decimal point is also counted as one, when the actual width of the output is greater than m when the limit of M; N in%m.nf controls the number of decimal places, and N in%m.ns means the left n bits of the truncated string, and left-fill space if n is less than M.

%0MD: The integer value of the output m-bit, if the number of data bits to be output is less than m, the former complement 0

5. The storage type of variable in C language

There are many kinds of variable storage types in C language, which can be divided into dynamic variables and static variables according to the life cycle, and can be divided into: local variables and global variables (also called internal variables and external variables) according to the field of use.

a), dynamic variables, and static variables.

The space occupied by dynamic variable variables is static, that is, the program always exists. The code representation is the keyword "static" + variable type name + variable name, the initialization of the static variable is completed at compile time, and only initialized once, the program executes no longer assigns the initial value to the static variable.

b), local variables and global variables (also called internal variables and external variables)

A local variable is defined as a variable outside the body of the function, so it is also called an external variable. Its scope is at the declaration of the global variable until the end of the program. Unlike a local variable, a global variable has a static storage area that is allocated space at compile time.

External variables can extend scopes with the "extern" keyword. For example: An int's external variable A is defined in the middle of the program, the scope of the external variable does not contain more than its function, if you use "extern A" at the beginning of the file to declare the external variable, then the scope of the external variable will be extended to the entire file. You can also extend the scope of an external variable to another file by using "extern". For example: There is an external variable A in A.C, which is also available in B.C after using the extern a declaration in B.C.

6. About pointers and pointer variables
Pointers: pointers and memory addresses. For a 32-bit machine, the memory address needs to be represented by a binary number of 32bit, which is 4Byte, so the pointer is a 4Byte binary number.
Pointer variable: A variable that stores pointers, that is, the contents of a pointer variable is a 4Byte binary number of a memory address. Regardless of what type of data the pointer variable points to, its value will always be a 4Byte binary memory address.
The stored content is the same length memory address, then why do you want to specify the type of the pointer variable when declaring it? We know that different types of data are not in the same number of bytes in memory, so when accessing data through pointers
The step size of the pointer variable movement is also different. For example: A pointer variable p points to the first element of an array of integers a[0], at this point I want to use this pointer variable p to access a[1] only need to p+1 that is * (p+1) can get a[1]
Value. When we encode, we let p+1 and actually the memory address move 4 bytes (the memory space occupied by an integer data), that is to say p+i represents the memory address to move p+i*c (c is the number of bytes that p points to the memory space of the data type),
So if a pointer variable does not have a type, the memory address does not know how many bytes to move in order to properly access the data when it encounters this situation.

The next few are very basic, but you may want to pay attention to the novice tips:

When inputting and outputting with scanf and printf functions, be sure to remember that int corresponds to%d, long is corresponding to%ld, and float and double%f correspond

1. At the end of each statement, add a semicolon instead of the end of each line.

2. When entering or outputting with the scanf and printf functions, the type corresponds well to the format control character (for example, int%d). Otherwise, the output may be inconsistent with the data.

3. When using the SCANF function, the format of the input is consistent with the format in double quotes, so it is best to have only a variety of format controls in double quotes

4. In numerical operation, be careful to prevent data overflow. Data loss can occur once the maximum type value is exceeded

5. Note the precedence of the various operators, which can be added () to ensure that the order of operations satisfies the requirements.

Dark Horse programmer--c Language Basic grammar

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.