The basic data types in C depth learning

The basic data types in C depth learning

About naming variables defined problem

  1. Naming the best according toVariable PurposeLenovo defined variable names.
  2. Identifier names are permitted in the standard longer, but in general the compiler recognizes only the first 63 characters, the characters after more than 63 characters may not be recognized, so if the two variable names are more than 63 characters, and the first 63 characters of the same The compiler can cause an error.
  3. When naming variables allow the use of lowercase letters, uppercase letters, numbers and underscores. And the first character of the name can only be a character or an underscore, can not bedigital
  4. C libraries and operating systems often use identifiers (e.g., -kcab) to start one or two underscore characters, it is bestavoidUsing this name in your own programs. Standard tags are one or two underscore characters to start, such asLibrary Tag. Such identifiers are reserved. This means that, although they are no syntax errors, but can lead to name conflicts.
  5. The name of the C languageCase sensitive, That a letter of uppercase and lowercase as two different characters. Therefore, stars and Stars, STARS is different.

Assignment need to understand about the place

C language is throughAssignment OperatorsRather than the assignment to complete an assignment. According to standard C, C language and not the so-called "assignment."

return statement

Return in c language statement as a jump statement.

Tips to improve readability

  1. Select the function name and write meaningful comments. Note that when using these two techniques complement each other and avoid duplication of long-winded. If the variable name is the width, you do not have to write notes indicating that the variable represents the width, but if the variable name is video_routine-4, will explain the meaning of the variable names.
  2. On the concept of the function with a plurality of spaced empty line portions. For example, with a blank line and other parts of the declaration portion of the separate area of ​​the program. C language does not provide sure to use a blank line, but more use of blank lines to improve readability.
  3. Each statement on its own line, although the C language more freedom, does not require the code to be written on separate lines or have to be on one line, but it is clear that single statement per line space will be more attractive and comfortable.

Compiler Issues

Error position than the real error position lag line. For example, the compiler at compile the next line will be found on a line is missing a semicolon. Therefore, if the compiler error missing semicolon in a row, check the line.

Semantic and grammatical errors

  1. If you do not follow the rules of the C language will make mistakes in grammar, syntax errors similar to English; and semantic errors because the programmer did not properly understand the needs caused by the statement error and can not get the desired results. This situation can be run properly, because the compiler will not know your intentions, and no grammatical errors. You need to find their own mistakes.
  2. One method is to locate semantic errors: the key points in the program of inserting additional printf () statements to monitor the development of changes in the value of the variable. By viewing changes in values ​​can understand the implementation of the program. After performing satisfied with the program, you can remove the extra print () statement, and then recompile. Of course, the debugger is the main debugging tools.

Keywords are the words of the C language

So during the variable name, and do not need to pay attention to keywords or reserved identifiers coincide.

Variable output problem

% F used in printf statement to process a floating point value,% .2 2f for precise control of the output, the output of the specified floating-point only two decimal places after the decimal point.
% M.nf significance:
. 1, F represents the data output is floating;
2, n represents the data output from the n decimal places as a decimal rounding the first n + 1 bits, the n-bit complement if less than 0;
. 3, m denotes an output data terminal equipment m occupies the characters, and the right alignment, if the actual number of bits less than m, padded with spaces on the left, if the actual number of bits is greater than 7, extended to the right output.
For example:
the printf ( "% 4F \ n-", 123.4);
the printf ( "%. 2F \ n-", 123.4);
the printf ( "% 4F \ n-.", 123.4);
output is:
123.4
123.4
123.4000
note:
1, and when m is 0, representative of the output width is not limited. In principle, m> n, if m <n, was illegal.
2, when the display of% - hours. 2F, a left-aligned.
3,% 2f is output to all bits float 2, including the decimal point, if less than 2, 0s, if more than two, according to the actual output.

The meaning and relationship bytes, words, bits

  • Bit: In the computer, the smallest unit of data was a bit refers to a binary number, English name is a bit.
  • Byte: 8 bits constituting a byte (B), 1 byte can store a half of letters or characters. Byte is the basic unit of measurement of storage space, Yang computer memory and disk capacity are in bytes.
  • Words: different computer systems of an integral multiple of the word length.
  • Word: number of binary numbers in computer technology in the unit of CPU time (same time) can be processed at one time called the word length.

Float Considerations

  • The concept of floating-point math in almost real. 2.75, 3.16E7,7.00 and 2e-8 is a floating-point number. Note that, in a later value plus
    a decimal point, the value becomes a floating-point value. So, 7 is an integer, floating-point number is 7.00. Obviously, there are many forms of writing floating-point number.
    BRIEF SUMMARY e notation: 3.16E7 represents 3.16 × 107 (3.16 multiplied by 10 ^ 7). Wherein, 10 7 = 10,000,000, the index 10 is referred to 7. The key to understand hereDifferent integers and floating-point storage solutions. Computer floating point numbers into decimal part and an exponent part is represented, while
    and stored separately from the two portions. Thus, while 7.00 and 7 are the same in magnitude, but differ in their manner of storage. In decimal, you can
    put 7.0 written 0.7E1. Here, the fractional part is 0.7, 1 is the exponent part. Of course, the computer uses a binary power of 2 and is stored inside, rather than a power of 10.
  • Integer no fractional part, and floating-point numbers with decimal parts.
  • The number of floating-point numbers can be represented larger than the range of integers.
  • For some arithmetic (e.g., add and subtract two very large number), the higher degree of precision floating-point loss.
  • Because in any interval (e.g., between 1.0 and 2.0) there are infinitely many real numbers, so that the computer can not represent all floating point value within the interval. Float is usually only an approximation of the actual value. For example, 7.0 may be stored as a floating point value 6.9999, more accuracy will be discussed after the finger.
  • In the past, floating point slower than integer arithmetic. Now, however, it contains a lot of CPU floating point processor, narrowing the gap on the speed.

Octal and hexadecimal display

In a C program, and the display may be used a different number of hexadecimal. To use a different different binary conversion instructions. 0x or 0X to represent a hexadecimal number, prefix 0 for octal numbers. It is displayed in decimal numbers, using% d; in octal numbers, using% o; displayed in hexadecimal numbers, using% x. Also, to display the prefix for each binary numbers 0, Ox and 0x, must be used respectively% # 0,% # x,% # X.

Share-digit integer type

Now, the most common settings on the computer is a personal, long long accounted for 64, long accounted for 32, short accounted for 16, int
accounting for 16 or 32 (according to the natural word of the computer may be), in principle, this 4 Representative types of four different sizes, but in actual use
, the type is generally some overlap between.
Select by:

  • If a number beyond the range int type, and when the long range type, used long. However, for those larger than the space occupied by long int system, using long types can slow the speed of operation. Therefore, if not necessary, do not use long.
  • If the code is written on the same type long int and the type of machine space, when the 32-bit integer so desired long type should be used instead of an int, so that the transplanted to 16-bit machine may still work. Similarly, if the 64-bit integer does require, use type long long.
  • If a 32-bit system to use 16-bit value should be used short type to save storage space in the int. Usually, only when the program uses relativeLarge memory systems availableWhen the integer array, important to consider space-saving issues. Another reason to use short type is that some of the components used in the computerHardware registerIt is 16.
  • Octal and hexadecimal constants are treated as int. If the value is too large, the compiler tries to use unsigned int. If it is not big enough, the compiler will in turn use the long, unsigned long. Long long and unsigned long long type.
  • In some cases, the compiler needs to store a small number of long type. For example, when programming to the explicit memory address on the IBM PC use. Further, some of the C standard also requires the use of long type values. Should a smaller constant as long type treated, may be added l (lowercase l) or L suffix end of the value. L suffix better use, because l and the number 1 looks like. Thus, the 16-bit int, long 32-bit system, would 7 stored as 16-bit, 32-bit stored as the 7L.
  • 1 may be used or a suffix octal and hexadecimal integer, such as 020L and 0x101. Similarly, in a system supporting long long type, LL or 11 may be used to represent the values ​​1ong long postfix type, such as 3LL. Further, u suffix U or unsigned long long, as 5u11. 10LLU, 6LLU or 9Ull.

Integer overflow

When the integer is greater than the range variable assignment it can be represented by the overflow condition occurs. DETAILED can define two integer variables int i, unsigned int j. When exceeding the maximum value, j will start again from 0; i will start again from -2147483648.note: When an integer overflow occurs, the system will not notify the user, so he must be careful.

In the C language, a byte is defined as thechar type share-digit, So whether it is 16-bit or 32-bit systems, can be used char type.

Character constants and initialization

If we want to assign a character constant is A, simply defined as char str = 'A'; == Note: == in C language, a single character enclosed in single quotes character becomes constant.

C language will be treated as character constants of type int rather than char type

Existing following code char grade = 'B'; B corresponding to the original value of 66, should be stored in the storage unit 32, but can now be stored in the storage unit 8 in. Using a character constant such characteristics, can define a character constant, 'FATE', i.e., the four separate eight ASCII code stored in a memory unit 32. If the assignment to grade, only the last one is valid, that grade = 'E'.

Common escape character

Here Insert Picture Description

char type signed or unsigned

The C90 standard, C language allows the front keyword signed or unsigned char. In this way, no matter what the compiler default char type, signed char type represents signed, and unsigned char unsigned type. This is useful when dealing with small integers with char type. If only deal with char character, the previous char without using any modifier.

In general, long type is larger than the memory occupied by the type of short, int type, and the width or the same type of long or short, and the same type.

Released two original articles · won praise 0 · Views 177

Guess you like

Origin blog.csdn.net/xingranzhihuo/article/details/103957906