The first C program has been created in the previous lecture, knowing that the C program is composed of functions, which continues to learn some basic grammar of C language. C language belongs to a high-level language, in fact, all the basic grammatical components of the high-level language are the same, but the expression of the same form. Like Asians and Africans, we all have a human structure: 2 hands, 2 feet, 1 heads, but they look different, like skin color, face shape. Therefore, you have mastered a high-level language, and then to learn other high-level language, it is quite fast. Moreover, many other high-level languages, such as the objective-c to learn later, are based on C language, derived from C language, learn C language Well, absolutely not suffer.
first, the key word1. What is a keyword
Keyword is the C language provides a special meaning of the symbol, and some places are also called "Reserved words."
2. What are the key words
The C language provides a total of 32 keywords, all of which are given special meaning by C language.
auto double int struct break else long switch
case enum register typedef char extern return union
const float short unsigned continue for signed void
default goto sizeof volatile do if while static
Roughly browse once again, do not have to Baidu each keyword role, these keywords will be used in the future, then you want to not remember is difficult.
3. How to identify keywords
These keywords will often be mixed with other symbols to use, a program with so many English symbols, how do I know those are keywords? Keywords have 2 major features:
1> are all lowercase
2> displays special colors in development tools or smart text editing tools (such as UltraEdit)
For example, the following C program appears in UltraEdit
In this code, only int and return are keywords, and all keywords in the C language will appear blue in UltraEdit! As you can see, main is not a keyword. As to what the special meaning of int and return is, we will not discuss it first, and we'll explain it in detail later.
Second, identifiers1. What is an identifier
Identifiers are some of the symbols and names that you customize in your program. To differentiate from keywords: the keyword is the symbol that is provided by the C language by default, and the identifier is customized by the programmer.
2. The role of identifiers
A 1> identifier, literally understood to be a symbol used to identify something, is meant to distinguish it. In fact, the function of the identifier is similar to the human name, in order to distinguish each person, at the time of the birth of each person has a name.
2> said: C language is composed of functions, a C program may have more than one function, in order to distinguish these functions, each function has a name. The name of the function is one of the identifiers. In addition to the function, the concept of "variable" will be learned later, and the name of the variable is also the identifier.
So, in the code above: int and return are the keywords, and main is the identifier
3. Naming rules for identifiers
Identifiers are custom-defined by programmers, but cannot be arbitrarily named, and they have the following naming conventions:
1> can only be made up of 26 letters in uppercase and lowercase, 10 Arabic numerals 0~9, and an underscore _.
The following identifiers are all correct:
Test1 mike2jack my_text
The following identifiers are all wrong:
test!32 haha (DA) TT haha _text
2> are strictly case-sensitive, with the uppercase and lowercase letters of the same English letter being two different identifiers.
For example: Main and main are two different identifiers
3> cannot start with a number.
The following identifiers are all wrong:
123haha 78text 98111
4> can not use keywords as identifiers.
The following function is wrong:
1 int int()
2 {
3 return 0;
4 }
The name of the function is one of the identifiers, and the function name of line 1th, called Int,int, is a keyword in the C language, which is illegal and will certainly report a syntax error.
4. Identifier naming specification
This naming convention, you don't have to obey. However, 90% of the best programmers follow this specification to go.
1> as much as possible a meaningful name, such as a complete English word, others look at this name can be the role of this identifier. If you do not understand English, you can also use pinyin, try not to be like ABCDE, SFSDFSDF and other such seemingly meaningless names
2> If the identifier contains more than one word, you can use the Hump logo (except for the first word, the first letter of each word is capitalized): FirstName, MyFirstName, or use the underscore _ to connect: first_name, My_first_name
Third, comments1. What is a comment
- Annotations are a concept that is very important in all computer languages, literally, the meaning of annotations and interpretations.
- Annotations can be used to explain what a piece of program or a line of code means, facilitating communication between programmers. If I finish writing a line of code and add the corresponding comment, someone else will see this comment and know what my line of code is for.
- Comments can be any text, which means you can write Chinese.
Note: It is not possible to write a large paragraph of Chinese directly in the code, the compiler will error
1 This is a main function, the entry point of the C program
2 int main()
3 {
4 return 0;
5 }
The 1th line is written in Chinese to explain the function of main, but this code cannot be compiled successfully. The 1th line of Chinese is not called a comment, and the comment has its specific format.
2. Types of Annotations
There are 2 types of comments in C: single-line comments, multiline comments. Most high-level languages have these 2 types of annotations.
1> Single-line comment
A single-line comment begins with two forward slashes, that is, starting with//, only one line is commented, from//To the end of the line is the contents of the comment
1 #include <stdio.h>
2
3 // This is a main function, the entry point of the C program
4 int main()
5 {
6 // This code can output the string Hello text on the screen.
7 printf("Hello World");
8 return 0;
9 }
Lines 3rd and 6th are comments, and comments are generally green in the development tools. As you can see, the comments in line 3rd here are used to interpret the 4th line of code, and the comments in line 6th are used to interpret the 7th line of code. This is usually how you write a single-line comment that explains the purpose of a line of code.
Comments can also be written at the back of a statement
1 #include <stdio.h>
2
3 // This is a main function, the entry point of the C program
4 int main()
5 {
6 printf("Hello World"); // This code can output the Hello World string on the screen.
7 return 0;
8 }
The 6th line is a semicolon, and the green text behind it is a comment.
If you put//to the front of printf, the entire 6th line is a comment.
1 #include <stdio.h>
2
3 // This is a main function, the entry point of the C program
4 int main()
5 {
6 // printf("Hello World"); This code can output the Hello World string on the screen.
7 return 0;
8 }
You'll see that the entire line 6th turns green, stating that the entire line 6th is a comment.
2> Multi-line comments
Can only be used to comment on a single line of text, if there is more than one line of text need to comment, then the multi-line comment. Multiline comments start with/* and end with */, and the contents in/* and/* are comments.
1 /*
2 Author: MJ
3 Description: The first C language program
4 role: This is a main function, the entry point of the C program
5 */
6 int main()
7 {
8 return 0;
9 }
Lines 1th through 5th are comments
3. Nesting of annotations
1> Single-line comments can nest single-line comments, multiline comments
1 // Wow haha // Hehehe
2
3 // /* fsdfsdf */ // sdfsdfsd
All of these 2 lines are comments
2> Multiline comments can nest single-line comments
1 /*
2 // Author: MJ
3 // Description: The first C language program
4 role: This is a main function, the entry point of the C program
5 */
All the contents of these lines are comments
3> Multiline comments cannot nest multiple lines of comments
1 /* Hahaha
2
3 /*
4 Xixixi
5 */
6
7 Hehehe */
You will find that only the 1th to 5th line is the comment, and the 7th line does not belong to the comment. Because/* after finding the first */after the comment is finished, and the 1th */In line 5th.
4> The following is the wrong wording.
1 // /*
2 Hahaha
3 */
Only Line 1th is a comment, line 2nd to 3rd is not a comment
4. Details of the note
1> comments are written to people, not to the computer. How can a computer see the Chinese we write? Therefore, when the program is compiled, comments are not compiled into the destination file.
In other words: The commented out statement is not executed.
1 int main()
2 {
3 // printf("Hello");
4 return 0;
5 }
The statement in line 3rd is commented out, so when you run the program, the statement on line 3rd is not executed, and there is no output on the screen.
2> to develop a good habit of writing notes. Most project managers check the subordinate code the first thing is to see if there is no comment, there are many companies will also check the machine test comments (the test is to give you a programming problem, a computer, in the specified time to solve problems).
3> Why is annotation so important?
- Today, you wrote hundreds of lines of code, very happy, made a very nice feature, but forgot to write the comments. After one weeks, it's normal for you to go back and look at that piece of code, which you may not be able to understand at all. If you write a comment, the situation is different, and comments can help you review the role of the Code.
- You've been in a company for over 1 years, and you've written 10, tens of thousands of lines of code, but you don't write a note. One day you quit and the new employee takes over your project, the first thing he must do is to read the code you wrote. But you did not write a little note, 100,000 lines of code, all in English, which will make the new employee very egg pain. Everyone has their own ideas, the idea of writing code is certainly not the same, it is very painful to see the code written by others, especially the code without comments. The way you do not write comments can greatly reduce your company's development efficiency. As a result, all formal companies attach great importance to annotations.
5. Hidden features of annotations1> the wrong line .
In fact, if you use it properly, annotations can be used not only to explain the program, but also to troubleshoot errors.
For example, the following code
1 #include <stdio.h>
2
3 int main()
4 {
5 printf("111\n");
6 printf("222\n")
7 printf("333\n");
8 return 0;
9 }
Compile the program and you will find that the compilation failed. At this point you can comment on the more questionable code, such as the comment line 6th, because it feels like the 6th line is missing something.
1 #include <stdio.h>
2
3 int main()
4 {
5 printf("111\n");
6 // printf("222\n")
7 printf("333\n");
8 return 0;
9 }
Compile again and find that the compilation was successful. Description is the 6th line of code Error!!!
2> checking the role of code
At work, occasionally encounter a function that they do not do, at this time we will go to the Internet to find some other people write code to fill their projects. But someone else's code can't exactly meet our needs, so we need to crop some useful code out, how to know which part is useful code? Then you need to figure out how each line of code works. At this point, we use annotations to figure out how each line of code works.
For example, the following procedure, I would like to know the role of the 3rd line of code
1 int main()
2 {
3 printf("Hello");
4 return 0;
5 }
This time you can run the program again, to see the effect of the operation, the effect is: on the screen output hello this string of content.
And then you annotated the 3rd line of code.
1 int main()
2 {
3 // printf("Hello");
4 return 0;
5 }
Then run the program again and find that the previous Hello is not being printed on the screen. Description of the 3rd line of code is the function of: on the screen output hello this string of content!!! In summary, it is to make a comparison of the comments followed by the comments before the effect, to see if there is less effect, if the effect is less, it is the function of the annotated code is to achieve the effect of the less. This sentence may be a bit around, a good experience, to you a lot of help!
Li Hongqiang iOS Development Zero-based learning iOS development "02-c language" 03-keywords, identifiers, comments