Basic knowledge of C Language

Source: Internet
Author: User
Tags print format

Basic knowledge of C Language

Hexadecimal:

L grinding (nian) Division: Convert decimal to octal, and convert decimal to hexadecimal to decimal to hexadecimal)

L decimal: multiply by the power of a number;

L binary: binary; Octonary: octal; Hexadecimal: Hexadecimal;

 

Source code, reverse code, and supplementary code: [data storage form] (the computer stores the Supplementary Code)

L-1: original code: 10000001

Reverse code: 11111110

Makeup: 11111111

When a negative number is used for complement, the symbol bit is not involved in the operation.

L positive number: the original code is the same as the reverse code;

L negative number: complement = the original code is reversed and added. The symbol bit is not involved in the operation.

L (when a negative number has only one highest bit, for example, 1000 0000, the symbol bit is also the data bit. When a negative number has more than one highest bit and another bit has one, the symbol bit is only the symbol bit, not involved in calculation)

Short a =-17;

-17: original code: 10000000 00010001

Reverse code: 11111111 11101110

Complement: 11111111 11101111 // 0 xffef;

L The data stored by the computer is in the form of a complement code. When the output is, the stored complement code is converted to the original code for reading and outputting.

{

Short a =-32768;

Short B = a + 1;

Printf ("% d", B );

Convert-32768 to the complement form, compress the complement code, and then convert the result to the original code and output it to B;

}

 

The difference between '\ 0' and 0:

'\ 0': it is an escape character, that is, an empty character. It cannot be displayed. The value corresponding to ASCII is 0,

When defining the char type, '\ 0' is the same as 0; for example, char a =' \ 0'; char a = 0; the two are the same, occupying one byte.

When defined separately, for example, int I = 0; char j = '\ 0'; the two are different. The first part occupies 4 bytes and the last part occupies 1 byte.

The character '0' indicates that the corresponding value in the ASCII code is 48;

 

Program:

L getchar (); // request the terminal to obtain a character, which is often used to stop the console and is used in the same way as system ("pause ").

L sizeof (a) // calculate the number of nodes occupied by a in the memory;

How many bytes does an integer int occupy in the memory? Correct answer: sizeof (int) bytes;

L view the memory: Set the breakpoint, run the breakpoint, and obtain the address. Click debug-> window-> memory to view the memory, set the breakpoint to the next line, and run f11 lines by line to see the changes in the memory.

L %: the remainder can only be an integer, and the remainder cannot be 0. a % B obtains the remainder result symbol to see the symbol of;

L a/B; the divisor B cannot be 0;

 

Bit, byte, and word:

L bit: bit;

L Bytes: byte; 1 byte = 8bit;

L: two bytes;

 

L Uint: it corresponds to a 32-bit unsigned integer (unsigned int)

L Static: defines Static variables or functions. modified variable data does not release space because the function exits.

L size_t: it is used to define an unsigned integer. In 32-bit systems, size_t is 4 bytes, while in 64-bit systems, size_t is 8 bytes, this type enhances program portability. the sizeof () operator runs the size_t type.

 

L The Float type can only represent a maximum of 7 valid values. The default value is 6 decimal places;

L error expressions allowed by floating point numbers:

Abs (x-y) <1e5;

Determine whether two floating point numbers are equal:

If (abs (x-y) <1e5)

The floating point number follows the rounding principle when printing in % m. n format.

 

Print format controller:

L % hd (short integer); % d (integer); % p (print address); % u (unsigned integer ); % ho (the short type Integer is printed in octal format); % lo (the long type Integer is printed in octal format); % lu (the unsigned long type Integer is printed ); % ld (print an integer of the long type );

% Llu (print unsigned long type Integer); % lld (print long type Integer );

L % m. n (m: Total character width (including the decimal point); n: n digits after the decimal point. The Truncation will be rounded to the nearest digit ;)

L % g: intelligently select a method for output by scientific counting and floating point notation;

L % p: The format controller used to print the address; printf ("% p", & );

L % 0: auto-fill 0; if the vacancy is at the low position, no 0 is filled; if the vacancy is at the high position, 0 is filled; (0 is filled only when the right alignment is exists and there is a vacancy; left alignment does not need to be supplemented, otherwise the original value will be changed)

 

Escape characters:

L \ v: vertical

L \ r: Move the current seat to the beginning of printf ("11111 \ r22"); Result: 22111

 

Char type

L The char type actually stores integers rather than characters, because each character corresponds to the corresponding integer in an ASCII code (ASCII code: represents a specific character with a specific integer, for example: A ---> 65; ASCII code value range: 0-127)

L The Char type can be used for addition, subtraction, multiplication, and Division operations.

 

Implicit type conversion:

1) Value assignment;

2) When different data types are involved in the operation (low-byte conversion to high-byte conversion) (two types occupy the same number of characters long to double (decimal precision is higher than integer ));

3) when passing parameters;

4) During the value assignment operation, the type on the left of the equal sign is converted to the right of the equal sign (the precision is missing );

 

Forced conversion: int (x + y); (int) x; (int) (x); forced conversion is not automatically rounded

 

Priority:

For operators with the same priority, the operation order is determined by the combination direction.

Note :! > Arithmetic Operators> Relational operators >&>||> value assignment operators

 

If condition judgment:

(1) No matter how many if .... Else .... Only one judgment statement

(2) Switch (I) select to judge that multiple statements can be contained in the case;

(3) The value of the value assignment expression is the value on the right of the equal sign;

 

Loop statement:

Break;

Continue; skip this loop

Return; returns the function. Function execution ends.

Goto; used with labels

 

Time seed:

Void typing_speed ()

{

Int I, j;

Srand (time (NULL ));

// Time seed. If no time seed is pre-defined, when the program executes the typing_speed () function for the second time, rand () the random data is the same as the random data obtained from the previously called function. After srand (time (NULL) is used, the random data is different.

Char arr [100] = "dgshdgawsfj e; wloitrfbdjmhfjcdxagfasGWyhetwhfrdskajgfoiueraiwehfkdsh ";

For (I = 0; I <strlen (arr); I ++)

{

J = 500 + rand () % 1000;

SetTimeout (j );

Printf ("% c", arr [I]);

}

}

 

Random Number:

Rand () % (n-m + 1) + m

The principle is that for any number, 0 <= rand () % (n-m + 1) <= n-m is equal to 0 + m <= rand () % (n-m + 1) + m <= n-m + m that is, m <= rand () % (n-m + 1) + m <= n

 

Auto-increment and auto-increment:

{

Int a = 3, B = 2;

A = ++ a--- B; // The result is a = 3, B = 1; ++ a first implements the + 1 operation in this operation, a is equal to 4 first, then -- B is executed, and then a = ++ a--- B is assigned a value.

A = a ++-B --; // The result is a = 2, B = 1. In this operation, a ++ first implements the operation, then execute the -- B operation, calculate the value assignment of a = a ++--- B, and then add the value of a to 1.

}

The sum of minus signs (-) and minus operators has the same priority. The combination of data is from right to left, for example:

B =-a ---B ++-c ++-d ++; // equivalent to B =-(--) -B ++-c ++-d ++; right-to-left combination

5 ++ (a ++) (x + y) ++ // Several writing errors a ++ is an expression and cannot be auto-incrementing (floating point numbers can be auto-incrementing)

 

Shift left and right:

L shifts the number of digits left to the power of 2, and shifts the number of digits right to the power of 2;

L {int a = 10; a <2; printf (% d, a) ;}result: 1010 <2 à 101000-> 40; (int four bytes; there are 32 bits, so there is no overflow on the left, there are many bits)

 

Calculate the length of an integer array:

Sizeof (arr)/sizeof (int );

Macro definition:

# Define A (x) x * x

A = A (10); a = 10*10 = 100;

B = A (5 + 5); B = 5 + 5*5 + 5 = 35; pure replacement, no parentheses

 

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.