Key points of this chapter:
Why should we learn c language What is? C language first C Language program data type Variable, constant character string+Escape character+notes Select statement Circular statement
1. Why should we learn c language?
C produces all things
The foundation of programming
The first choice for long-term IT career development
C language is the parent language and the bridge between human-computer interaction and the bottom
Learning C/C + + is equivalent to mastering the core technology
Knowledge points can be opened up in one stroke.
The IT industry generally changes once every 10 years
In the past 50 years, C/C + + has occupied the top three places in the TIOBE ranking list for a long time without any shaking. It can be said to be classic and never out of date!
2. What is C language
Language is a tool for communication,
For communication between people, we may use Chinese, English, Korean and so on,
Computer language (C/C++/Java/Python...) is used for communication between people and computers,
C language is one of the most common computer languages
C language is a general computer programming language, which is widely used in Bottom development. The design goal of C language is to provide a programming language that can compile and process low-level memory in a simple way, generate a small amount of machine code and run without any running environment support.
Although C language provides many low-level processing functions, it still maintains good cross platform characteristics. C language programs written in a standard specification can be compiled on many computer platforms, even including some embedded processors (single chip microcomputer or MCU) and supercomputers.
In the 1980s, in order to avoid differences in C language syntax used by various developers, the American National Bureau of standards formulated a complete set of American national standard syntax for C language, called ANSI C, as the original standard of C language. At present, the C11 standard issued by the international organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) on December 8, 2011 is the third official standard of C language and the latest standard of C language. The standard better supports Chinese function name and Chinese identifier, and realizes Chinese character programming to a certain extent.
C language is a process oriented computer programming language, which is different from object-oriented programming languages such as C + +, Java and so on.
Its compilers mainly include Clang, GCC, WIN-TC, SUBLIME, MSVC, Turbo C, etc.
3. The first C language program
Classic hello world
#include <stdio.h> int main() { printf("hello world\n"); return 0; }
Note that the main function has one and only one
The main function is the entry to the program
The program is executed from the first line of the main function
The int main function returns a return value at the end
0 is an integer, which is returned by the main function
void main used to be used in the past, but it is not used now. Please don't write it like this
4. Use compiler
Now our common integrated development environment is Visual Studio, which can realize many functions and push forward a wave
However, if you learn c language, you may encounter the problem of unsafe scanf function and error reporting. You need this sentence
#define _CRT_SECURE_NO_WARNINGS 1
Specific methods can refer to this blog to solve the problem perfectly
VS2019 solution to input scanf error reporting (nanny level)
5. Data type
All computer languages must have symbols to represent data types. There are also many types in c language in order to express various values in life more abundantly.
char //Character data type short //Short int //plastic long //Long integer long long //Longer shaping float //Single-precision floating-point double //Double precision floating point number
Why refine so many types?
Each type has different sizes. How to look at the size?
Using the keyword sizeof, its unit is bytes
printf("%d\n",sizeof(char); printf("%d\n",sizeof(short); printf("%d\n",sizeof(int); printf("%d\n",sizeof(long); printf("%d\n",sizeof(long long); printf("%d\n",sizeof(float); printf("%d\n",sizeof(double);
6. Constants and variables
Some values in life are constant and some values are variable.
Constant values are represented by the concept of constants in C language, and variable values are represented by variables in C language.
Variables should be initialized when they are created. Not initializing is a very bad code style.
6.1 variable definition method
int age = 18; float weight = 150.1; char ch = 'a';
6.2 local and global variables
What is defined inside the code block is a local variable
Defined outside the code block are global variables
Generally, what is inside the parentheses is the local variable
int main() { int a=10; { int b=20;//local } return 0; }
When the names of local variables and global variables conflict, local variables take precedence
6.3 using variables
Using variables, I want to take the simplest example.
The input and output of variables are realized by scanf method and printf method.
#include <stdio.h> int main() { int m = 0; int n = 0; int sum = 0; printf("Enter two numbers:"); scanf("%d%d", &m, &n); sum = m + n; printf("sum = %d\n", sum); return 0; }
6.4 scope and life cycle of variables
Scope
The scope of a local variable should be its scope, which is out of use
You can see that g can be printed anywhere
a can be printed in the main function
b can only be printed inside braces
Summary:
-
The scope of a local variable is the local scope of the variable.
-
The scope of the global variable is the whole project.
life cycle
- The life cycle of a local variable is the beginning of the scope life cycle and the end of the scope life cycle.
- The life cycle of a global variable is the life cycle of the entire program.
6.5 constants
The definition forms of constants and variables in C language are different.
Constants in C language are divided into the following types:
-
Literal constant
-
const modified constant
-
Identifier constant defined by define
-
enumeration constant
-
#include <stdio.h> //give an example enum NBA//Enumeration needs to declare enumeration constants first {//The following are the possible values of this type LEBRON,//0 CURRY,//1 KD,//2 }; //Literal constant int main() { //Literal constant 3.14;//Literal constant 1000;//Literal constant //const modified constant const float pai = 3.14; //pai here is a constant variable modified by const //const modification into a constant can not be modified directly! //#Identifier constant for define #define SCORE 100 printf("score = %d\n", SCORE); //enumeration constant printf("%d\n", LEBRON); printf("%d\n", CURRY); printf("%d\n", KD); //Note: enumeration constants start from 0 by default, and then increase downward by 0, 1, 2 of 1 return 0; }
Note:
The pai in the above example is called const modified constant. Const modified constant is only limited at the syntax level in C language
The variable pai cannot be changed directly, but pai is essentially a variable, so it is called a constant variable.
7. String + escape character + comment
7.1 string
"hello world"
This string of characters enclosed in double quotation marks is called string literal, or string for short.
Escape character \ 0
The end flag of the string is an escape character of \ 0.
You can see the difference in the following code:
int main() { char arr1[]="abc"; char arr2[]={'a','b','c'}; printf("%s\n",arr1); printf("%s\n",arr2); }
The output is as follows
This is because the definition method in arr1 automatically has' \ 0 '
Because of \ 0, when printing arr1, \ 0 is the end flag of the string, and arr1 ends after abc is printed
The definition of arr 2 does not know when to stop, so other characters appear
Change to char arr3 [] = {'a', 'B', 'C', '0'}; that will do
When calculating the string length, \ 0 is the end flag and is not counted as the string content.
For example, this arr1 shows that there are 3 characters, while arr2 outputs a random 15 because there is no \ 0
7.2 escape characters
For example: when playing multiple at the same time? Some compilers interpret as three letter words (unimportant)
It can be solved with escaped \
Another example:
1: How to print a single quote 'on the screen?
2: Print a string on the screen. The content of the string is a double quotation mark. "How to do?
#include <stdio.h> int main() { printf("%c\n", '\''); printf("%s\n", "\""); return 0; }
8. Notes
- Unnecessary code in the code can be deleted directly or commented out
- Some codes in the code are difficult to understand. You can add notes
- It's not good to add too much. It will appear redundant and unprofessional
For example:
There are two styles of annotation:
- Comments in C language style / * xxxxxx*/
- Defect: comments cannot be nested
- C + + style comments / / xxxxxxxx
- You can annotate one line or multiple lines
9. Select a statement
#include <stdio.h> int main() { int melon = 0; printf("Do you keep the melon cooked? Choose cooked (1) or you TM Find fault (0):"); scanf("%d", &coding);//input //judge if(melon == 1) { prinf("I can sell you raw melons and eggs at a fruit stand\n"); } else { printf("Huaqiang sarilang\n"); } return 0; }
10. Circular statement
Some things must be done all the time, such as day-to-day learning and code practice.
For example:
How to implement loop in C language?
while statement
for statement (after)
do... while statement (after)
//An example of a while loop is given below
#include <stdio.h> int main() { printf("Learn a skill\n"); int hour = 0; while(hour<=10000) { hour++; printf("I'll keep trying to squeeze time to practice\n"); } if(hour>10000) printf("I'm out of school\n"); return 0; }
summary
The content of this section is only a brief introduction to the basic knowledge of c language. The content is not detailed. Later, it will be divided into sections one by one. Take notes and share them. Thank you for your support!
Because the article is too long to read, the later content will be shared in the next blog and a link will be added later
The following main contents are:
function
array
Operator
Common keywords
define defines constants and macros
Pointer
structural morphology