C language biography, you can see oh

  Understanding the structure of C language program

  A simple example c program, c language description of the basic structure, format, and good writing style, the junior partner have a preliminary understanding of the c language.

  Example 1: Calculation of two integers and c procedures:

  #include main () {int a, b, sum; / * define variables a, b, sum of integer variables * / a = 20; / * The integer assigned to an integer variable 20 a * / b = 15; / 15 * the integer assigned to an integer variable b * / sum = a + b; / * the sum of the two numbers is assigned to integer variables sum * / printf ( "a =% d, b =% d, sum =% d \ n ", a, b, sum); / * output the calculation result to the display * /}

  Highlights:

  1, any one of the c language program must include the following formats:

  main() { }

  This is the basic structure of c language, any program must contain this structure. Brackets can not write anything, then the program will not execute any results.

  2, main () ---- referred to in c language "primary function", a and c have only one main program function, any of a c program always starts execution from the main function, the main function of one pair of back parentheses can not be omitted.

  3, are enclosed in braces {} function is called content main body function, the contents of this part is to be executed by a computer.

  4, inside the {} behind every word has a semicolon (;), and c language, we have to end with a semicolon is called a word a c language statement, the statement is the end flag semicolon.

  5, printf ( "a =% d, b =% d, sum =% d \ n", a, b, sum); ---- screen output is provided to the function performed by our direct use of this language system c the user can see the results after the program runs, the following results are displayed on a display:

  a=20,b=15,sum=35

  6、#include

  Note: (1) beginning with a # (2) does not end with a semicolon semicolon is not this line, so it is not the statement, called the command line, or called "precompiled processing command" in c language.

  7, the program to / * * at the beginning and / annotation program portions represented end, comments may be added at any point in the program, in order to improve readability of the program is added, but the computer completely ignored in performing the primary function contents comment section, in other words as a computer that is not present in the comment section of the main function.

  Generation process procedure C

  C is for the source program to generate compiled object files, executable files and generated via connection.

  The source of the extension .c, expand the target program called .obj, extension executable named .exe.

  Identifier

  When programming, you must functions, variables and other names, the name called identifier. C language identifier naming rules as follows:

  Identifier only by letters, numbers and underscores;

  The first letter identifier must be a letter and underlined;

  Identifiers case sensitive, such as If and if two are completely different identifiers.

  Legal identifier as follows: A6, b_3, _mn illegal identifiers as follows: ab # 12, 8m, tr3: 4, yes no

  The same key identifier can not have special meaning to the program, the name of the function can not be programmed with the user, the same as C language library functions, various identifiers try not to repeat the program in order to distinguish. When you select a variable name and other identifiers, should pay attention to the "see to know the name of justice."

  

 

  Identifier is divided into the following three categories:

  1 keyword

  Keywords that have particular meanings, designed to illustrate a class identifier c language specific component can not be used as a user identifier.

  auto break case char union do double else enum extern goto if int long short signed static sizof struct switch unsigned void for while typedef continue float return typedef default

  2, the predefined identifier

  Predefined identifier c is also a specific meaning, but may be used as a user identifier, identifier into predefined categories:

  (1), library function name, such as (printf, scanf, sin, isdigit, etc.) (2), the compilation process the command name, such as (define, include)

  3, a user identifier

  Called user identifier user identifier required according to their own definition. Anyway custom identifier, must comply with three naming identifiers.

  

 

  constant

  In the program operation, the amount of its value can not be changed is called constants. Constant five types: integer constant, real constants, character constants, strings and symbolic constants.

  (A) numerical value conversion

  Digital four forms:

  ①: Binary: All figures consists of 0,1, every two into a binary number 2 does not occur .. Example: ②: octal: a digital 0 (note that other than a letter O, o) at the beginning, all numbers from 0 to 7 is constituted by, every eight into an octal number 8 does not occur. Example: 0112,0123,077 etc. ③: Decimal: all numbers from 0 to 9, configured, every decimal a decimal number 10 does not occur. Example: 0, 12, -15, etc. ④: Hex: with 0x or 0X (the letters X number 0), all the numbers 0 ~ 9, A ~ F (or a ~ f) configuration, every sixteen into a (wherein a, B, C, D, E, F represent 10,11,12,13,14,15) Example: 0x4A, 0X14c7 etc.

  Inside the computer, and figures are stored in binary form represented, general decimal number input by the user to the computer must be converted to a binary stored inside the computer, the computer is also the same operation result as binary, which would typically be converted again into a decimal number output to the user to read, this conversion is usually achieved automatically by a computer.

  (1) The decimal conversion binary, octal and hexadecimal

  Division: The decimal number is divided by 2, recording a remainder, the quotient obtained continues divided by two until the quotient is 0, then the remainder each time obtained along forward reverse order from the rear, the resulting remainder sequence of numbers is the decimal number corresponding to binary number. Octal and hexadecimal conversion method above.

  Example: 13 convert decimal numbers to binary value 1101, 015 convert octal, hexadecimal is converted into D.

  (2) binary, octal and hexadecimal to decimal conversion

  Sum of products: each bit binary from low to high (on the right is low, the left is high) multiplied by 20, 21, respectively. . . . Then these product summation.

  For example: = (13) 10 (317) 8 = (23E) 16 =

  (3) conversion between binary and octal, hexadecimal

  ①: Binary octal: right to left, a set of three for each converted to a decimal number, the resultant combination is the data corresponding to the number of octets (Note: zero padding is less than three high). Example: (010 110 111) 2 = (267) 8 ②: Binary Hexadecimal: right to left, each set of four bits is converted into a decimal number, the resulting composition is hexadecimal data corresponding to the number (note : high up less than four zeros). Example: (0101 1011) 2 = (5B) 16 ③: octal binary conversion: Each digit binary number is converted to three cases: (13) 8 = (001011) 2 = (note: to remove the first two 00 , because there is no meaning in the high 0) ④: conversion hexadecimal binary: each digit binary number is converted to four cases: (E3) 16 = (1110 0011) 2

  

 

  (B) the integer constant

  Integer constant There are three forms: decimal integer constant, octal and hexadecimal integer constant integer constant.

  (Note: c language does not directly represent a binary integer constant, c does not appear in the binary language source program.)

  Written as follows:

  Decimal integer constant: 123, 0, -24, 85L (long integer constant), etc. octal integer constant: 051, -026, 0773 and other hexadecimal integer constant: 0x55, 0x1101, 0x, 0x5AC0, -0xFF . Wherein L is a long integer.

  (Iii) real constant

  Real constants are presented in two forms: decimal form and exponential form.

  Decimal form: 5.4 0.074 -23.0 exponent: 5.4e0 4.3e-3 -3.3e4

  (1) is a real constant fractional part of 0, can be written as 453 or 453.0. (2) when expressed as a decimal, both sides of the decimal point must be the answer, can not be written as ".453" and "453.", but should be written as "0.453" and "453.0." (3) When the exponential written, must be preceded by e digital behind the e index must be an integer (note: integer order code may be positive, negative, it may be octal, hexadecimal number, but must be an integer) .

  (D) character constant

  Flag character constant is a pair of single quotation marks '', character constants c language there are two types:

  (1) a single character enclosed by a pair of quotation marks, such as 'a', 'r', '#'. Note: 'a' and 'A' are two different character constant.

  (2) a pair of single quotes, backslash \ beginning, followed by a plurality of digital or letters, such as '\ n', where "\" is the escaped meaning behind mean different things with different characters, such character is called the escape character constants. Specifically as shown in FIG.

  Meaning the ASCII escape character escape character

  \ N CRLF 10 \ t transverse skip to the next tab position 9 \ b backspace 8 \ r carriage 13 \ f the paper page transducer 12 \\ backslash '\ "92 \" single quote character 39 \ "double quote symbol 34 \ a ringer 7 \ ddd character 1 to 3 represented by octal number \ character xhh 1 ~ 2-digit hexadecimal numbers represent

  

 

  (E) a string constant

  C, in order, a sequence of several characters in double quotes is the string constants.

  Example: "ni hao" "happy" and so on.

  (Vi) symbolic constant

  Symbolic constant is a constant "#define" defined by the macro definition, in C programs available identifiers represents a constant.

  Example: calculating the area of ​​a circle c procedures.

  #include #define PI 3. main() { float r,s; r=12.5; S=PI *r*r; printf(“s= %f ”,s); }

  Description:

  #define macro is defined, all occurrences of PI 3. This program represents, while PI called symbolic constants. Traditionally we use capital letters to represent the symbolic constants, variables lowercase letters, it is easier to distinguish.

  

 

  variable

  The variable amount is its value can be changed. Variables have variable names, occupy a certain memory cell in the memory, the memory cell is stored in the value of the variable. Different types of variables which storage units of different sizes, the variable must be defined before use.

  (A) integer variable

  Integer variables are divided into four kinds: Basic (int), short integer (short int or short), long integer (long int or long) and unsigned type (unsigned int, unsigned short, unsigned long).

  Different systems have different requirements compilation of these four bits and the integer value range occupied by the data.

  Said type

 

  Signed word to explain "signed" (that is the number of positive and negative points), do not write signed also implicitly described as signed, unsigned used to illustrate the "unsigned" (only a positive number).

  

 

  (Ii) real variable

  C, into a single real variable type precision (float) and double precision (double) two kinds. Such as:

  float a , b ; double m ;

  In vc, float type data is 4 bytes (32 bits) in the memory, double data type 8 bytes. Providing single-precision real number seven significant digits, real double precision provides 15 to 16 significant digits. Real constant, regardless of type float and double type, a real constant can be assigned to a float-type or double-type variable, but variable depending on the type of significant digits taken corresponding real constants.

  Note: Real variables can hold only real value, real value can not be stored with integer variables, nor can the real variables to store integer values.

  (C) the character variable

  Character variable used to store character constants defined in the form:

  char variable name;

  Wherein the keyword char character data type is defined, occupied by a byte memory cell.

  例:char cr1,cr2; cr1= ‘A’ , cr2=‘B’ ;

  When a character is assigned to a variable character, the character itself is not stored in memory, but the character corresponding to the ASCII code stored in the memory unit. For example, the character 'A' is the ASCII code 65, the form is stored in memory as follows: 0

  Since the ASCII code characters are stored in memory, its storage form and stored as an integer Similarly, the C language can be common between the character data and integer data, a character can be output in the form of characters, but also can be an integer in the form of output, character data arithmetic operations can be performed, this time corresponds to the ASCII codes for their operation.

  

 

  Automatic type conversion, and casts

  When the same data type of each expression is not the same, the compiler will automatically put them into the same type then calculated. Conversion priority:

  char < int < float < double

  That left the level of "low" type conversion to the right. Specifically, if the highest priority data in the expression of double type, other data, then this expression are converted into a double-type, and the calculation result is double type; if the highest priority data in the expression it is a float type, then this other data expression are converted into float type, and the calculation result is a float.

  In doing assignment operator, if the assignment number of different types of the left and right sides, the type of number is assigned to the right to the left type conversion; when the right type than the type on the left, it is truncated at the right side of the data conversion.

  In addition to the automatic conversion, as well as cast, it is expressed in the form:

  (Type) (expression); Example: (int) (a + b)

  Discussion: When a value is assigned to 3.4, b value assigned to 2.7, (int) (a + b) and (int) a + b is the number of values?

  

 

  C operator awareness

  C language operators a wide range, can be divided into the following categories:

  , Arithmetic operators: used for various types of numerical computation. Comprising plus (+), subtract (-), multiply (*), divide (/), remainder (%) increment (+), decrement (-) co seven kinds.

  Assignment operator: for assignment operators, divided into simple assignment (=), complex arithmetic assignment (+ =, - =, * =, / =, =%) and compound assignment bit operations (& =, | =, ^ =, >> =, << =) a total of eleven kinds of categories. <= "" Span = "">

  , Comma operator: means for combining several expression into an expression (,).

  , Relational operators: used for comparison operation. Comprises greater than (>), less than (<), equal to (==), greater than or equal (= ""> =), less than or equal to (<=) and not equal (! =) Six kinds. <= "" Span = "">

  , Logical operators: for a logic operation. Including (&&), or (||), non (!) Three.

  , Conditional operator: It is a ternary operator, conditions for evaluation (:)?.

  Bit manipulation operators: involved in computing the amount of bits in binary arithmetic. Including bits with (&), bit, or (|), non-bits (~), bit XOR (^), left (<<), right (>>) six kinds.

  8, a pointer operator: for taking content (*) and taken address (&) two kinds of operations.

  9, the number of bytes required operators: the number of bytes occupied by data type (the sizeof) is used.

  10, special operators: parentheses (), the subscript [], the members (→ ,.) several.

  Further, according to the number of objects involved in computing, C language operators may be divided into: unary operators (e.g.,!), Binary operators (such as +, -) and ternary operator (eg:?).

  Arithmetic operations and arithmetic expressions

  First, the basic arithmetic operators

  (1) + (Addition operator or positive operator, such as 2 + 5).

  (2) - (subtraction operator or negative operators, such as 4-2).

  (3) * (multiplication operator, such as the 3 * 8).

  (4) / (division operator, such as 11/5).

  / Operation divided into two cases:

  a, or so "other" sides are integers, the result must be an integer obtained (note: only take the integer part, not rounded)

  For example: 5/2 is 2, the value is not 0 2.5, 1/2.

  B, there are about "other" on both sides of the at least one real data (i.e. decimals), the results obtained for the real data.

  For example: 5 / 2.0 2.5,7.0 value / 2.0 was 3.5.

  (5)% (modulus operator or said remainder operator,% on both sides of the data should be an integer, such as the value of 2 7 9%).

  It should be noted that: when the operands is negative, the results obtained differ with the compiler, the vc, the same sign of the result of the dividend, for example: 13% -2 value 1, value 2 and 15% - 1.

  

 

  Two arithmetic expressions and operators with the binding priority

  Arithmetic expressions are used arithmetic operators and parentheses computation (also known as operands) are connected, in line with C language syntax rules of expression. Computing objects include functions, constants and variables.

  In the computer language, evaluation of arithmetic expressions laws and laws of four operations similar mathematics, as the calculation rules and requirements.

  (1) in arithmetic expressions, parentheses may be used a multi-layer, but the brackets must be paired. When the operation starts from the inner parentheses, each of the expressions calculated value to outside from the inside.

  (2) in an arithmetic expression, calculates the priority for the different priorities in descending operator, the operator can, if the same expression operator precedence, binding direction press operator operations.

  (3) If an operator different operand types sides, by using the first automatic conversion or cast, so that both have the same type, then operation.

  Third, the increment decrement operators

  Role: to increase the value of the variable or decrement.

  Such as: ++ i, - i (i prior to use, to make the value of i plus 1, minus 1). i ++, i-- (i after use, so that the value of i plus 1, minus 1).

  (1) Only variable used to increment operator (+) and decrement operator (-), but not with constant or expression, ++, or such as 10 (x + y) ++ is illegal .

  (2) + and - the binding direction is "from right to left", such as + -i, i left operator is negative, the right is the increment operator, and a negative increment arithmetic operations are "self- right to left "binding, corresponds to - (i ++).

  In the common loop from the increase (decrease) operator, it is also commonly used in the pointer to the operator, candidates to figure out "i ++" and "++ i" and "i--" and "--i" of difference, especially to find out the values ​​of variables and expressions.

  

 

  Assignment operator and assignment expression

  An assignment operator and the assignment expression

  Assignment symbol "=" is the assignment operator, the role is assigned to a variable or a data value of a variable to another variable, the expression is referred to as the composition of the assignment operator assignment expression. The general form is:

  Variable name = expression

  Assignment to a variable multiple times in the program, each assigned a value, and its corresponding data storage unit is updated once the current data in memory is that the last data assigned.

  Example: a = 12; This expression is read "10 assigning a value to the variable a".

  a, if not on both sides of an assignment object type operation, the system will automatically type conversion, the conversion rule: converting the value type into an assignment type of the expression on the left side of an assignment variable,

  Example: int y = 3.5; final stored in the variable y is an integer of 3.

  B, can copy the value of the expression is then assigned to the variable, to form a continuous assignment.

  For example: x = y = 25 is a continuous assignment expressions, x = y = 25 is equivalent to x = (y = 25), so the expression x = y = 25 the final value of 25.

  

 

  Second, the compound assignment operator

  Before the assignment operator together with other operators may be formed compound assignment operators. Wherein the arithmetic operator is related compound: + =, - =, * =, / =,% =.

  Not have the same space, the composite priority assignment operator and the assignment operator between two symbols. N + = 1 expression equivalent to n = n + 1, taking the role of the value of the variable n by 1 and then assigned to the variable n, other composite assignment operator rules of operation and so on.

  The struggle to express the value of a + = a- = a * a, where a is the initial value of 12.

  step:

  (1) First performed "a- = a * a" calculation, corresponding to a = aa * a = 12-144 = -132. (2) then "a + = - 132" operation corresponds to a = a + (- 132) == - 264.

  Comma operator and expressions comma

  C-language, a comma as a delimiter in addition, it may also be used as a comma operator ---- operator, comma operator to connect several expressions, for example, a = b + c, a = b * c, etc. are called comma expression.

  The general form is:

  Expression 1, expression 2, expression 3, ..., n Expression

  Example: x = 2, y = 3, z = 4

  Having binding comma expression from left to right, i.e., to solve the expression 1, followed by solving Expression 2, until the value of the expression n. N expression value is the value of the entire comma expression. Comma expression value is above the value of the expression z = 4 4. Note that all the comma operator operators the lowest level.

  Example: the following program segment:

  main() { int a=2,b=4,c=6,x,y; y=(x=a+b),(b+c); printf("y=%d,x=%d",y,x); }

  Program displays the results: y = 6, x = 6

  Discussion: The y = (x = a + b), (b + c); to y = ((x = a + b), b + c) the results of the procedure?

  Relational operators and relational expressions

  A logical value of the C language

  Logical value C language are only two: true (true) and false (flase). With non-zero for true, zero for false. Thus, for any expression, if its value is zero, it represents a false value, if it is nonzero, represents a true value. As long as the value is not zero, either positive, negative, integers, real numbers, it represents a true value. For example, -5 logic is true.

  Second, the logical expression

  "&&" and "||" it has two operands, so that they are binary operators, and! Operands only one, so it is unary operator. Logic operation example as follows:

  (1) a && b: When both sides && "true", the expression a && b value is true.

  It is worth noting: In mathematics, the relationship 0

  (2) a || b: || sides when there is a "true", the expression values ​​a || b is true.

  (3) a:! Negated indicates that if a is true, then A is false and vice versa!. For example! -5 to 0 value.

  In the C language, && and || by a logical expression consisting of, under certain circumstances will have a "short circuit" phenomena.

  (1) x && y && z, only when x is true (non-zero), it requires determining the value of y; only x and y are true is only required to determining the value of z; long x is false it is not necessary determines y and z, the whole expression is 0. Formulas: "a false must be false."

  Example :(! 5 == 1) && (++ i == 0) (! 5 == 1) expression is 0, the computer operation is skipped (++ i == 0) This expression , (! 5 == 1) && (++ i == 0) expression is 0.

  (2) || x || y z, as long as the value of x is true (non-zero), it is not necessary discriminant values ​​y and z, the whole expression is 1, only the value of x is false, y is determined only need value, only the values ​​of x and y is false while only need to determine the value of z, formulas: "a true must be true."

  

 

  Bit computing

  I. Bitwise Operators

  In the computer, the data is stored in the form of a binary number, bit operation refers to operation on bits of the memory cell. Provides 6 C language bitwise operators.

  Second, bit operation

  Bitwise & | ~ << >> ∧ arranged in descending order of priority are:

  Bitwise operators negates the "~" highest priority, and the same right and left, in second, followed by the bit order is the "&", bitwise exclusive or "∧" or bitwise " |. " The order of ~ << >> & ∧ |.

  Example 1: left shift operator "<<" are binary operators. The function of each of its binary operand "<<" left a number of bits to the left of all, the number of bits of a mobile "<<" specifies the right, discarding the upper, lower 0s. = "" <= "" Span = "">

  For example: a << 4 refers to a movement of the respective four binary left. After such a = 00000011 (decimal 3), left 4 00 (48 decimal).

  Example 2: right shift operator ">>" are binary operators. Its function is to put all the various binary operand is ">>" Several left to the right, ">>" the number of bits specified by the right.

  For example: Let a = 15, a >> 2 represents the right of the decimal 3).

  It should be noted that, for a number of symbols at the right, the sign bit will move along. When a positive number, the most significant bit 0s, and is negative, the sign bit is 1, 0 or the most significant bit is the complement of a predetermined complement system depends on the compilation.

  Example 3: 00 is disposed a binary number, so that if a high exclusive OR operation by the inverse A∧B 4, the lower 4 bits constant, the binary number b Yes.

  Analysis: XOR operation used to adapt a particular bit flip, so long as the required bit and an inverted XOR operation on it, because the original number of bits of value 1 1 0 XOR too, the value of the original Number XOR result is to obtain a bit 0 and 1. And bit 0 is XORed with the retained. XOR operation may also be used to exchange two values, without temporary variable.

  The int a = 3, b = 4 ;, want to swap values ​​of a and b, the following statement can be used to achieve: a = a∧b;

  b=b∧a;

  a=a∧b;

  So the answer to this question is:

  The importance of the C language is well suited as an introductory programming language, to lay the foundation of self-evident.

Release potential: learning to enhance the efficiency, capacity building program
http://www.makeru.com.cn/live/3507_1276.html?s=45051

C language programming foundation
http://www.makeru.com.cn/course/details/2233?s=45051
enhance the C programming ability
http://www.makeru.com.cn/live/1392_1166.html?s=45051

Guess you like

Origin www.cnblogs.com/923327iu/p/11936474.html