C Language Basics and details

Source: Internet
Author: User
Tags array definition logical operators modifier

简单来说什么是C语言,就是用c写的语言就是C语言。接下来言归正传。1.没有注意数据类型存储范围 int main(int argc, char **argv) { char a; a = 1; for (a=0; a<128; a++) printf("#"); return 0; }你猜是循坏了128,还是死循环呢? 答案是死循环,因为a的类型是有符号char型,它的取值范围为-128-127;当a=127时,a<128,会继续执行,代码执行完之后a会加1,但是a的最大值为127,在加1就会变成最小值-128,为什么会变成-128呢,就是我们平时的时钟,时间为12:59时,再过一分钟就会变成1点,这就简单的说明了为最大值加一会变成最小值,最小值减1就会变成最大值。在计算中数据都是以2进制进行存储的,127在计算机存储时为0x7f,加1,就会变成0x80,因为char中0x80表示最小值-128;所以-128小于128,以此类推,a<128;永远为真。

2. When printing the type, select the correct format.
For example double A = 4.5;
printf ("%lf", a);
Must not print with%f, otherwise it will report a warning
Summary: Whenever double, long type of data printing don't forget to add l
3. Show unsigned integers
%u: Show unsigned integers
%o: Show unsigned octal
%x: Show unsigned 16 binary
4. Various data types of variables and 0 to compare the correct way of writing
int a=5;
if (0 = = a);

 char ch = ‘a‘; if (‘\0‘ == char) float f =0.152; if ((x>-0.000 001) && (x<0.000 001)) bool  flag = 1; if ( flag ) int *p = &a; if ( NULL == p)

5. Escape character: The transfer character is a special character constant, and the transfer word constant begins with a backslash "\"
Commonly used transfer characters:
\a: Beep, Bell
\b: Fallback: back one pane
\f: Page Change
\ n: NewLine: Cursor to the beginning of the downstream line
\ r: Enter, cursor to the beginning of the bank
\ t: Horizontal tab
6. Debugging code has a lot of commonly divided into printf debugging, bug debugging,
I'm talking about debugging with printf, which is easy and easy to operate.
UseFILE, Line,FUNCTIONDATE,
TimeImplementing the Code with
FILE: Displays the file name of the code;
FUNCTION: Displays the name of the calling function, not the default primary function
Line: Displays the number of rows in the row;
Time: Show Time
DATE: Show Date
7. Conversion of data types
The system generally automates the type conversion when mixed operations are performed on multiple data types
The rule of conversion: Short storage lengths convert to longer storage lengths and are not lost
(a) automatic conversion:
int a=5; Double d = 4.5;
A + D; The result is a double type of
(b) Assignment conversion
int a=5; Double d = 4.5; Char ch;
CH = a + D; The result is a char type of
(c) Forced conversions
A = (int) d + C;
When the character data is assigned to an integer variable, the character data is placed in the lower eight bits of the integer variable storage unit
--For unsigned types, place the 8 bits of the character into the integer variable low eight bits, high 24 bits 0
--for the signed character type, if the highest bit is 0, the integer variable 24 is 0, if the character is the highest bit is 1, the high 24 bit 1
Char 0000 0001--0000 0000 0000 0000 0000 0001
1111 1111-11111 1111 1111 1111 1111 1111
When assigning an integer data to a char type variable, the precision is lost by simply sending its low eight bits intact to a char variable (that is, truncated).
When assigning integer data to floating-point variables, the values will not change, but the precision may be lost in floating-point form into the variable;
The floating-point data is assigned to integer data, and the floating-point data is stored directly in the int type storage space;
Assign the float type to the double type data, place the 30--22 bit directly into the double type 62--52, and put the rest in the remaining 51 bits,
Assigns the double type data to the float type, directly placing the 7-bit significant number into the floating-point storage space.

The

8. Operator
operator is very common, and no matter how you write programming in that language, you use the operator, and when it comes to operators, you think of his priorities, where we don't talk about his priorities, straight down error-prone places.
Priority: Who is not the priority of the first person who, the so-called priority is the combination type, who is the priority of high and who combine, as to how to calculate the order of priority operations, the same priority sequence is determined by the compiler
(a) + +,-
a++: is dedicated to the state value a ; then add a
++a, add one first, and then give a+1 the status result;
-A; a++ same as
(b) Do not assign a value to the same variable multiple times in an expression
For example: a = 5;
c = a++ + a++ + a++; The compiler doesn't know where to start counting
A = 5;
A = a++;
printf ("%d%d%d%d%d\n", a++, a++, a++, a++, a++);
If you want to do this, assign the a++ result to other variables, and use the other variables to calculate the
(c) Displacement (only for integers or character types)

                Unsigned data type: Left high 0 when moving right, 0 on the right when moving left, signed: Positive number: Right shift, left side 0, left shift to right 0;  Negative number: Right shift, left 1, left, 0 for positive right shift one equivalent to divide by 2, left shift 1 is equal to 2; (d) Logical operators & |   ^ &: Both are 1, only 1, otherwise bit 0; Function 0 |  : The two are only one to 1, the result is 1, otherwise 0, the function of a ^: the same as false, different for the false, the function of the reverse example: & A & 1 = A; A & 0 =       0 | A |     1 = 1; A |                                        0 = A;  ^ A ^ 1 = ~a;        A ^ 0 = A; (e) Conditional operator: three-mesh operator expression 1?                                    Expression 2: Expression 3 conditional operator operations have precedence: first, the expression 1, if the expression 1 is true, the expression 2, the end, otherwise, the expression 3, calculate the end of the example: a = b=c= 1; x = a++?                                    b++ c--:c++:b--? c++:c--;         The result: a=2;b=2,c=1; (f) The comma operator has the lowest precedence, linking two expressions, and counting from left to right, note that: not everywhere the comma appears as a comma operator, such as a function argument, and a comma-spaced 9.scanf and printf function: scanf: modifier function m: input ru data width,The end of a space or non-convertible character is encountered *: Suppress, specifies that the input item is not assigned to the variable input delimiter when it is read in: Typically a space, tab, or enter as a separator for other characters: a character in the format string between two format characters example: scanf ("%d                ,%d,%d ", &a,&b,&c);            The input method must be: 12,13,14 carriage return scanf ("%d", &a);            Input 12 12 Return the car to end otherwise do not end the filter separator, to enter two times scanf ("a=%d", &a);            Input: Must start with "a=" + number + Enter end scanf ("%*4d", &a);            Input: 132345 a=45; scanf ("%d%c", &a,&c);             Input 12 it's over. Because%c will automatically read the carriage return, empty char ch;             int i;                     for (i=0; i<10; i++) {printf ("i=%d\n", I); scanf ("%c", &ch);                     printf ("%c\n", ch);  GetChar ();             means take the return key, can only take one time}%s He%c encountered a space, the enter will automatically stop scanf ("%[^\n]", &a);//means no space key scanf ("%[a-z]", &a);//means only A-Z 10.switch: is a multi-way judgment statement, he determines whether the expression is matched to a value in an integer or character constant, and if the corresponding match is found, the statement associated with the constant is executed, SWITC H,case, and default are all keywords. Statements can be simple or conform to statements.        The switch only needs to be enclosed in parentheses, and the subject of the switch is enclosed in {}, and the expression that evaluates to the data type of the specified case constant data type can contain any variable name, or it can always be        Constants (int and char) the length of the case cannot be the same, and can be out of order, without affecting program results, but generally in order. If the case is not followed by a break, it indicates that a match is found and then executes, if no break is encountered, and executes to the end of the program, default: Is the description of the option that does not match, put the start position to add break, otherwise it will continue to execute, put to the end, do not add,                                It ends after playing. For example: Case 5: statement, break, Case 5: statement, break,//error, two identical case, because the selection is not known in the match, Case 1: statement, break, Case 7 | | 6: statement; break;//ERROR 7 | | 6 Returns a status result of 1, with Case 1: Repeat, but you can write case 6:case 7: statement;

Last: A one-dimensional array and a two-bit array
The array, the set, the set, is to put the same things together. What about arrays? Also the same, is to put the same type of data together, if not an array to hold the same data, with a variable to represent, it will appear disorganized, no regulation, time is long, only God knows, you define the meaning of the variable; and then formally introduce the array
storage type data type Variable name [number of elements]//array is actually composed of element data type +[element number]
Storage type: Plainly, you define where the variable is stored. (The general default auto, put in the stack, when the code block execution end is put space, static type, static, put the data storage type statically, know that the program ends to free space, otherwise do not free space, extern declared as a global variable, Other files can be used with extern-declared variables, scoped to the entire function, the register modifier implies that the corresponding variables of the compiler will be used frequently, and if possible the words should be placed in the CPU register to speed up their storage speed)
int a[5];//a can actually be seen as int [5]; the size of the
//array is determined at compile time, and the variable is at run time value
//array element number must be integer or char type
Initialize: Define assign value to variable or array element at the same time
int a[3] = {n/a};// Initialize all.
Char ch[5] = {65,66,67};//non-initialized partial auto clear 0
int be[100] = {[95] = 15,};//Specify element initialization, no initialized part auto clear 0
int c[6];
C[2] = 2; Assigning a value to an element does not affect other elements, does not initialize the array, the stored value of the memory is unknown, the array is not cross-checked, and access to the element that is out of bounds can cause unpredictable errors;
int cat[9];
CAT[2] = 5; [] Array definition fixed format
2[cat] = 5;//reference variable, [] time-variant address operator a[i] = = (a+i) = = (i+a) = = I[a];

    The meaning of the array name: #define N-int a[n]; 1. Represents the entire array: only son ah &a, and sizeof (a) &a gets the entire array address sizeof (a) Gets the byte size of the entire array 2. Other cases the array name represents the address of the first element, a + 0 = = &a[0], sizeof (A+0); Represents the address of the first array element, is an address constant, cannot be added by itself, can not be assigned to the group name a++;//Error A = {1,23,5};//error int a[0];         Represents an address that cannot hold data 3. Array Traversal int a[10]= {0};         int i; for (i=0; i<10; i++) {printf ("%d\n", A[i]);} 4.int a[4];//array at compile time, the space is determined, and the variable is applied at execution time.            Space-assigned two-dimensional arrays: In fact, consists of one-dimensional arrays, memory storage space is linear.  int a[] [5]; You can omit the subscript of one-dimensional array; initialize: int a[5][5]= {15,6,3,9,7,8,2,1,5,4,6};//Partial initialization, others are 0; int c[8][9] = { [6]                    [3]=9,}; Specify element initialization, first row, and then specify the meaning of a two-dimensional array name in the row: 1. Array name: sizeof (array name) ==> entire array size & (array name) ==> the entire array address 2. For a two-dimensional array.            Each element is a one-dimensional array, the array name is placed in the first one-dimensional array address &a[0], in addition to the above situation, is representative of &a[0] is a = = &a[0];    int a[m][n];           For example: sizeof (a) = = 4*m*n;        sizeof (a[0]) = = 4*n;        sizeof (a[0][0]) = = 4;                Traversal: #define M 4 #define N 5 int main (int argc,char **argv) {int a[m][n]={0};                int i,j; For (i=0, i<m; i++) {for (j=0; j<n; j + +) printf ("%                       D\t ", A[i][j]);                    printf ("\ n");                } return 0; }

C language Basics and details

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.