Introduction to C language:
Born in Bell Laboratories from 1971 to 1973, by researcher Dennis Rich, Ken Created by Thompson, it is a special language for writing operating system. Because it is very fast, it is also commonly used to write the core code, underlying data structure, algorithm, etc.
Because it is the first high-level programming language, many later programming languages use C language for reference in their design, so c language is also known as the mother of language. Learning C language well can open the foundation for learning many other programming languages.
advantage:
1. The syntax is simple, with only 32 keywords.
2. Can directly control the hardware, can write the hardware driver, and carry out the development of embedded and single chip microcomputer.
3. The running speed is fast, which can be comparable to the running speed of assembly.
Disadvantages:
1. The inspection is not strict, and it is easy to make some mistakes.
2. There are some defects in grammar.
first C Language code: #include <stdio.h> int main(int argc,const char* argv[]) { printf("Hello World!\n"); return 0; }
1. The code written by the programmer is not standard C code. It needs a program to be translated into standard C code. The program responsible for translation is called preprocessor, and the translated code is called preprocessing instruction. All instructions beginning with # are preprocessing instructions.
#The function of include < > is to import a header file.
2. C language code is generally written in two files
. h header file: auxiliary code, generally including macro constants, macro functions, structures, unions, declarations of enumerating global variables and function declarations.
. c source file: it records the function code.
3. The C language standards committee provides some basic functions for C language in the form of functions, which are encapsulated in libc So library file with some header files to describe these functions, stdio H is one of them, which describes the input functions in the library.
stdio.h stardand input output head
4. In C language, code is managed by function. A function is a section of function code, and the main function is the execution entry of all functions, and there can only be one.
5. The main function can be attached with some parameters, which are provided by the provider of the program**
6. C language uses braces to distinguish management. The codes in braces belong to the same management area.
8. Used in C language; As an end flag, you can wrap at will.
9. printf/scanf C standard library functions for input and output.
10. When using printf to output data, there are some characters with special meaning, which are called escape characters:
\t tab
\b backspace
\r back to the beginning of the line
\n line feed
\a the bell rings
\Show a
%%Show a%
11. The return statement has two functions:
End function execution
Returns a data to the caller
12. The return value of the main function reflects the end state of the program
When the program is abnormal, the program cannot continue due to environmental reasons.
0 everything is normal
The negative value program has an error, the code itself has a problem, and the calculated result is inconsistent with the expectation.
The main function is actually called by the operating system, and its return is obtained by the operating system and recorded in the log file.
echo $? Gets the end status of the previous program
compiler:
The compiler is responsible for translating the code that human can understand into binary instructions that computer can understand. It is composed of preprocessing, compiler, assembler and connector.
The process of translating C code into executable program by gcc compiler:
1. Pretreatment
gcc -E xxx.c display the end of preprocessing to the screen
gcc -E xxx.c -o xxx.i store the preprocessing results in a file
2. Compile
gcc -S xxx.i translate the preprocessed results into assembly instructions, which will generate Assembly file at the end of s.
3. Compilation
gcc -c xxx.s translates assembly instructions into binary instructions and generates o end of the target file.
4. Link
GCC a.o, B.O, C.O... Merge several target files into one executable file. The default executable file is called a.out, which is used in the terminal/ a. Out executable.
Common parameters of gcc compiler:
-E pretreatment
-S compilation generates assembly code
-c compile the object file
-o set the name of the compilation result
-Wall displays all warning messages
-Werror turns warning messages into error messages
-l using library files, m is the math library
-STD = < Syntax Standard > generally, the default is C89 Syntax Standard
-std=gnu99
File type in C language:
. h header file
The compilation result of. gch header file will be better than h file call, it is recommended to delete it directly
. c source file
. i preprocessing file
. s assembly file
. o object file
. so shared library file (dynamic library)
. a static library file
C language data type:
Integer:
Signed integer: its highest binary is used to represent positive responsible symbol, 0 represents positive number and 1 represents responsible. signed char 1 [-128,127] signed short 2 [-32768,32767] signed int 4 2 First 10 bit integer signed long 4/8 signed long long 8 9 First 19 bit integer be careful: signed If you don't add it, you mean add it
Unsigned integer: all its binary bits are used to represent data and can only represent positive numbers.
unsigned char 1 [0,255] unsigned short 2 [0,65535] unsigned int 4 [0,4 First 10 bit integer] unsigned long 4/8 ()argggcc unsigned long long 4/8 [0,1 First 20 bit integer] be careful: unsigned Keywords cannot be omitted, but for convenience C Standard library stdint.h The redefinition of unsigned types in are: uint8_t,uint16_t,uint32_t,uint64_t
Floating point:
Single precision: float 4 Double precision: double 8 high-precision: long double 12/16 Floating point type is stored by scientific counting method, with symbol bits+index+Mantissa, their operation speed is slower than integer, so try to use integer data when programming. Note: six digits after the decimal point are valid by default. Simulation: char Character type, character is a symbol or pattern, but it can only be stored in integer form in the computer. When displayed, it can be stored according to ASCII The corresponding relationship in the code table displays the corresponding symbols or patterns. 'A' 65 'a' 97 '0' 48 '\0' 0 bool Boolean, invented first C The boolean type that appears after the language, so in C There is no real boolean type in the language, including stdbool.h The header file can be used only after bool,true,false.
Variable:
Data whose value can change during program operation is equivalent to a container for storing data. Definition: type name; int num; Note: the default value of the variable is uncertain. Variables with special use should be initialized to 0(Calculation and summation). use: assignment num = 10; Participate in operation num * 3.14 Naming rules: 1,It consists of numbers, letters and underscores. 2,Cannot start with a number. 3,Cannot have the same name as the keyword. 4,See the name and know the meaning. Output of variable: printf/scanf Use placeholders to represent the data type of the variable: %hhd %hd %d %ld %lld %hhu %hu %u %lu %llu %f %lf %LF %c int printf(const char *format, ...); Function: output data format: placeholder +Prompt information ...: Several variable names Return value: the number of characters output Exercise 1: define and initialize various types of variables, using printf Enter the value of the variable. Variable input: int scanf(const char *format, ...); Function: receiving data from terminal format: placeholder ...: Addresses of several variables,&Variable name calculates the address of the variable Return value: the number of variables that successfully obtained data Exercise 2: define various types of variables, using scanf Receive data from the terminal and use printf Display.
Constant:
Data that cannot be changed during program operation is called a constant. Type of literal constant: 100 int type 100u unsigned int type 100l long type 100ll long type 100lu unsigned long type 100llu unsigned long type 3.14 double type 3.14f float type 3.14F long double type
Format input:
Integer data:
%nd display n Character width, right aligned. If the width of data is not enough, fill in spaces. If the actual data exceeds n Characters are displayed according to the actual situation. %0nd If the width of the data is not enough, supplement 0 %-nd Left aligned display
Floating point data:
%.mf Keep after decimal point m position %n.mf display n Character width and save after decimal point m The decimal point also occupies one digit width. %g Do not display extra zeros