1. Preface
The content of C language to be recorded today is: scanf() function, variable scope and life cycle, constants, strings, comments, and escape characters. Today's content is quite basic.
2. scanf() function and address fetcher&
Knowledge points:
- Supplementary description of header file "#include < stdio. H >"
- scanf() function
- scanf() function and scanf_ Differences between s() functions
- Fetch address character&
// Enter two numbers from the keyboard and print the addition result // To use the scanf() function in VS2019, you need to enter this line of code at the beginning #define _CRT_SECURE_NO_WARNINGS /* Knowledge points: 1. Supplementary description of header file "#include < stdio. H >". 2.scanf() function 3.scanf function and scanf_ Area of s() function 4. Take the address character 1.Supplementary note to header file: stdio is the abbreviation of standard input & output Thinking is the standard input and output, so if you want to use the input and output function, you need to use the header "stdio.h" File, including the scanf() function used by the code this time. 2.scanf()Function: the scanf() function is used to allow the user to enter the value of a variable from the keyboard. 3.If you use the scanf() function in VS2019, the compiler of VS2019 will report an error, and it will recommend you to use it scanf_s()Function, but scanf_ The s() function is unique to VS2019. If it is compiled with another compiler ,There will still be errors, so it is better to use the scanf() function, with better portability. Add: if you want to use the scanf() function in VS2019, you need to add a sentence at the beginning of the code "#define _CRT_SECURE_NO_WARNINGS",In this way, there will be no mistakes, and there is a kind of once and for all The method is to first find the path of "newc++file.cpp", drag the file to the desktop, and then use the Open it in Notepad mode, then enter "#define _CRT_SECURE_NO_WARNINGS", save it and put it back to the original So that every time you create a new C code, the beginning will be automatically entered "#define _CRT_SECURE_NO_WARNINGS"Yes. 4.Address fetching symbol: & in C language, it is called address fetching symbol, which is used to fetch the address of the variable after &. stay C In language, whenever you define a variable, the compiler will go to memory to apply for a space to store your newly defined variable Variable is equivalent to applying for a room for your variable, which also has a room number, that is, the ground Address, which is more convenient to manage variables. You can call your variables to get up and do things through this room number. */ #include<stdio.h> int main() { int num1 = 0; int num2 = 0; int sum = 0; printf("Please enter two numbers"); // For the use of scanf() function, an address fetching character needs to be written in front of the following variables& scanf("%d %d", &num1, &num2); sum = num1 + num2; printf("sum = %d", sum); return 0; }
3. Scope and life cycle of variables
-
Variable, which has scope and life cycle.
-
Scope: first of all, variables can not be used everywhere in the program after they are defined. As mentioned in the previous blog, variables have local variables and global variables, which can lead to the concept of scope, that is, where variables can be used, that range is the scope of this variable. The scope of a local variable is the local scope of the variable, and the scope of a global variable is the whole project.
-
Life cycle: variables do not survive until the end of the program after they are defined. When the scope of the variable is out, the variable will be destroyed, that is, the life cycle of the variable is the period from variable creation to variable destruction. The life cycle of local variables is: the life cycle of entering the scope starts and the life cycle of leaving the scope ends. The life cycle of global variables is: the life cycle of the whole program.
4. Constant
Knowledge points:
- What are constants?
- How many constants are there? What are they?
- How is each constant defined?
// constant /* Ask questions: 1. What is a constant? 2. What are constants? 3. How to define each constant The first problem: constant, as the name implies, is the amount of value that will never change, such as the number of PI, ID number and so on. The second question: there are four kinds of constants: 1. Literal constants 2.const modified constant variables 3.#define Defined identifier constant The third question: the answer is in the code. */ #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> // 3.#define defined identifier constant #define b 0 // Definition of enumeration constants enum Sex { MAN, WOMAN }; int main() { // 1. Literal constant 100; 200; // 2.const modified constant // It is essentially a variable, but now the value cannot be changed, so it is called a constant variable const int a = 0; //4. Enumeration constants // Enumeration constants start from 0 by default and are incremented by 1 printf("%d\n", MAN); printf("%d", WOMAN); return 0; }
5. String + comment
Knowledge points:
- What is a string??
- strlen() function
- What are the notes?
- How to use single line comments
- How to use multiline comments
// String + comment /* Ask questions: 1. What is a string? 2.strlen() function 3. What are comments 4. How to use single line comments? 5.How to use multiline comments First question: String: the string wrapped by the English symbol "" is a string, and the end is marked with "\ 0" End of string. The second problem: the strlen() function returns the value of the effective length of the string, if you want to use it strlen()The function needs to define "#include < string. H >" at the beginning. Third question: when you are writing code and encounter problems, you may need to write some text to clarify yourself My own ideas, like when I wrote knowledge points in the code, I always annotated the methods, and later work Yes, a code needs to be written by many people. If someone doesn't write comments, then One has to look at the code, causing a waste of time and getting the inner greetings of the next unlucky guy, so Annotation is a convenient function. The fourth question: "I have used the symbol" / / "many times. This is a single line comment symbol. Fifth question: for example, I use "slash asterisk asterisk slash" in this comment, which is the usage of multi line comments, Why do I use Chinese symbols instead of typing them? This is a disadvantage of multiline annotation, multiline annotation Comments cannot be embedded and applied. You cannot write a multi line comment symbol inside a multi line comment symbol. Dolls are prohibited! */ #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> int main() { // A method of defining strings char a[] = "abc"; char b[] = { 'a', 'b', 'c' }; char c[] = { 'a', 'b', 'c', '\0' }; /* be careful: As like as two peas, a and b? absolutely wrong! Array a will add a '\ 0' at the end by default, If you do not add a '\ 0' at the end of array b, the compiler will continue to print one Some strange things came out and didn't stop printing until they met '\ 0'. Array c added one at the end manually A '\ 0' has the same effect as array a, which shows the importance of the '\ 0' end flag!!! */ printf("%s\n", a); printf("%s\n", b); printf("%s\n", c); // Use of strlen() function printf("%d\n", strlen(a)); printf("%d\n", strlen(b)); printf("%d\n", strlen(c)); return 0; }
6. Escape character
The meaning of escape has changed.
I'll record some common escape characters here.
Escape symbol | interpretation |
---|---|
\' | Used to represent character constant ' |
" | Constant used to represent characters“ |
\ | Used to represent a backslash to prevent it from being interpreted as an escape sequence character. |
\n | Line feed |
\t | Horizontal tab |
\ddd | ddd represents 1 ~ 3 octal digits. Note that each digit cannot exceed 8, otherwise it will not be octal digits. |
\xdd | dd represents 1 ~ 2 hexadecimal digits. |