Preface: I haven't sorted it out for a long time, but I'm actually lazy and haven't learned much, so I'll briefly talk about what I've learned at the beginning, and talk about the problems encountered in learning and a little trivial experience. (the last part of the previous article is about the tips of variable types)
Small review:
1.const modifier constant. As follows, the commented code block will report an error when running.
//const constant attribute //const modifies a constant variable - it is a variable but has constant attributes int main() { const int num = 4; printf("%d\n", num); return 0; } //int main() //{ // const int num = 10; // int arr[num] = { 0 }; // printf("%d\n", num); // printf("%d\n", arr); // // // return 0; //}
2. Enum enum. Simple operation will know the basic law.
//enum enumeration key // // //enum Score //{ // zero, // one, // two // //The above are enumeration constants //}; // //int main() //{ // enum score a = two;// Note that the score here starts with lowercase // printf("%d\n", a); // printf("%d\n", one); // printf("%d\n", two); // printf("%d\n", zero); // // // // // return 0; //}
3. String type. If you want to store a string as a single character, you need to add 0 or \ 0. In essence, the storage form of the string has its own \ 0 end and is hidden. (you can observe their string length and remember to add #include < string. H >)
//String type //Double quotation marks cause "ABC"; "123": ""; //int main() //{ // // char arrp[]= "abc";// Hide at end \ 0 --- end flag of string // char arrq[] = { 'a','b','c','\0'}; // printf("%s\n", arrq); // printf("%s\n", arrp); // // // // // // return 0; //} //int main() //{ // char arrp[] = "\0"; // char arrq[] = "abc"; // char arrs[] = { 'a','b','c' ,'\0'}; // printf("%s\n", arrp); // printf("%d\n", strlen(arrp)); // printf("%d\n", strlen(arrq)); // printf("%d\n", strlen(arrs)); // // // return 0; // //}
4. Escape character, ASCII code, ASCII code value. As follows.
//When data is stored on a computer, it is stored in binary //ASCII encoding a-97 A-65 //ASCII code value int main() { printf("(alright\?\?)\n"); printf("(alright??)\n"); printf("%c\n", '\''); printf("%d\n", strlen("(alright\?\?)")); printf("%d\n", strlen("(alright??)")); printf("%c\n", '\132'); printf("%c\n", '\x61'); printf("%c\n", '\41'); printf("\a\a\a\a"); return 0; } //Escape character
5. Select the statement (if). As follows.
int main() { int input = 0; printf("college life\n"); printf("study hard?(1/0)\n"); scanf("%d", &input); if(input ==1) printf("greatoffer\n"); else printf("farming\n"); return 0; }
six while loop. As follows.
int main() { int line = 0; printf("Add bit\n"); while (line<20000) { printf("One line of code\n"); line++; } if (line>=20000) { printf("good offer\n"); } return 0; }
7. Function. I designed the addition function. In fact, it is not necessary. There are addition operators in the c library. The annotated code blocks are obviously more concise and efficient.
int Add(int x, int y) { int z = x + y; return z; } int main() { int a = 0; int b = 0; int sum = 0; scanf("%d%d", &b, &a); sum = Add(a,b); printf("sum=%d", sum); return 0; } //int main() //{ // int num1 = 0; // int num2 = 0; // int sum = 0; // printf("input two-phase addend: >"); // scanf("%d%d", &num1, &num2); // sum = num1 + num2; // printf("sum=%d\n", sum); // // return 0; //}
8. Logical operators. As follows.
//Bitwise AND& //Bitwise OR| //Bitwise XOR^ int main() { int a = 3, b = 5, d = 0; int c = a && b; int n = a || d; printf("%d\n%d", c,n); return 0; }
9. Array. For example, use int arr[10] to define an array containing 10 integer numbers.
10. Reverse by position (~ wave sign). This is difficult to understand.
int main() { int a = 0; printf("%d\n", ~a);//Reverse ~ wave sign by bit Integers are stored in memory as complements There are three binary representations of an integer:Original code, inverse code, complement code negative-1 Original code:10000000000000000000000000000001 Inverse code:11111111111111111111111111111110 Complement:11111111111111111111111111111111 Positive integers are the same return 0; }
11. Increment operator (+ +), decrement operator (- -), modulus operator. This is familiar and not difficult, so I won't give more examples.
int main() { int a = 10; //Int b = + A; / / pre + + use after + + //printf("%d\n", b);//11 //printf("%d\n", a);//11 int b = a++;//Post + + use first and then++ printf("%d\n", b);//10 printf("%d\n", a);//11 return 0; }
12. Type conversion. This rule is very complex. Different compilation environments and compilers also have different rules. Here is a brief display of the basic usage.
int main() { //int a = 3, 14; / / alarm int a = (int)3.14; printf("%d\n", a); return 0; }
13. Binocular operator. Easy to understand, an operator composed of three symbols. A simple demonstration is as follows.
int main()//?: trinocular operator { int a = 0; int b = 3; int max = 0; max = a > b ? a : b; printf("%d\n", max); return 0; }
14. Comma operator.
int main() { //int d=(2, 4 +5 ,6); int a = 0; int b = 3; int c = 5; //a=5 c=1 b=3 int d = (a = b + 2, c = a - 4, b = c + 2); //Comma expressions are evaluated from left to right //The result of the entire expression is the result of the last expression printf("%d\n", d); return 0; }
15. Subscript reference operator, function call operator.
int main() { //int arr[10] = { 11,22,33,44,55,66,77,88,99 }; //Printf ('% d \ n', arr [5]); / / subscript reference operator [] //printf("hehe\n"); / / function call operator () return 0; }
16. Common keywords. C language provides common keywords. Keywords cannot be used as variable names, (main,int...)
// Auto every local variable is auto decorated
// extern is used to declare external symbols
// Register register keyword
// signed Signed ten - twenty
// unsigned Unsigned
// static Static!
// union Consortium (Consortium)
// void Having no or empty meaning
//
// register int num = 100; / / it is recommended that the value of num be stored in the register
// A large number of / frequently used data want to be placed in registers to improve efficiency 4
//
// define include Not a keyword Is a preprocessing instruction
17.typedef.
typedef unsigned int u_int; int main() { unsigned int num1 = 100; u_int num2 = 100; printf("%d\n%d\n", num1, num2); return 0; }
18.static -- static.
(1) static modifies a local variable and runs to observe the change of the value.
void test() { static int a = 1;//static modifies local variables and changes the life cycle of local variables (essentially changes the storage type of variables) a++; printf("%d", a); } int main() { int i = 0; while (i<10) { test(); i++; } return 0; }
(2) static modifies global variables,
static modifies a global variable, so that this global variable can only be used inside its own source file (. c), and other source files cannot be used!
(3) static modifier function.
Static modifies a function so that it can only be used inside its own source file, not inside other source files. In essence, static is to change the external link attribute of a function into an internal link attribute.
19.define -- preprocessing instruction.
1.define symbols
#define MAX 100 int main() { int arr[MAX] = { 0 }; printf("%d\n", MAX); return 0; }
2.define macro
#define ADD(X,Y) X+Y int main() { printf("%d\n", 4*ADD(2, 3));//It becomes 4 * 2 + 3. If you want to change this situation, it should become ((x)+(y)) return 0; }
two
(continued from the previous article, I, a beginner, often encounter problems)
Some small syntax errors, such as forgetting to write mian functions, plus semicolons, and many other small problems, generally report errors, but sometimes run bug gy programs.
This is a small problem, but it is also a problem that often makes mistakes. Don't care. After being proficient, you can naturally reduce the frequency of mistakes.
three
The use rules of statements are not very clear, or the concept is not clear, resulting in inexplicable bug s in the final running of the program. For example, the following code is about the project of using the rolling division method to find the maximum common divisor of two numbers. I didn't encounter any big problems in program design and code code, and there are no big problems in logic, but the program can't run as expected My design idea runs. Finally, I try to change the variable range of my if and while statements to find the problem. In fact, this is mainly because I am not clear and proficient in the syntax rules, so I need to spend more time and energy on troubleshooting and modification. Moreover, it is good for small projects. If the project volume is too large, the cost of making such mistakes is also low Big.
#include<stdio.h> int main() { int m, n; scanf("%d%d", &m, &n); if (m>=n) { int r; r = m % n; while (r>0) { m = n; n = r; r = m % n; } if (r<=0) { printf("What is the greatest common divisor of two numbers%d\n",n); } } else { int r; r = n % m; while (r > 0) { n = m; m = r; r = n % m; } if (r <=0) { printf("What is the greatest common divisor of two numbers%d\n",m); } } return 0; }
For the time being, the above are some of my feelings and sharing. If there are any mistakes, please correct them. Because I'm lazy and busy recently, this report can only be written like this. Prepare to study hard in the next period of time. A summary will be published later.