Introduction to C language
BCPL->newB->C->UNIX->Minix->linux->gcc C Language was born in 1970-1973 In 2011, in Ken.Thompson and Dennis.Rich( c It is written under the guidance of the father of language and belongs to the United States Bell Labs. C Language is specially designed to write the operating system, so it is naturally suitable for hardware programming. It is famous for its fast running speed and is also very suitable for hardware programming The data structure and algorithm are realized. because C Language appears too early, C Language has many defects, and I didn't expect ordinary people to use it C Language programming, so There are defects, but the predecessors have summed up some experience to avoid< C Traps and defects of language C Language three swordsman C The grammar of language is very free, which also means danger. Freedom comes from self-discipline C89 Grammatical standards, gcc Default Syntax Standard C99 Grammar standard, right C89 Extension and enhancement of standards gcc -std=gnu99 c11 Grammar standard, new upgrade
1, First C program
#include<stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
#include<stdio.h>
The code written by the programmer is not standard C code. It needs a program to translate him into standard C code
The translated program is called preprocessor, the translation process is called preprocessing, the translated code is called preprocessing instruction, and the code beginning with # is preprocessing instruction
#The function of include is to import a header file into the current file
#Include < > load the header file from the path specified by the system
#include '' first loads the header file from the current path. If it cannot be found, then loads the header file from the path specified by the system
The operating system specifies the path to load the header file by setting the environment variable
stdio.h
Header file: in At the end of h, there are some auxiliary code stored, most of which are function declarations
Source files: in c, which stores functional code
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
Many header files are used to classify and describe the functions in the library, stdio H is one of them, and stdlib is commonly used h string. h
stdio.h is responsible for explaining the functions of input and output functions
main function:
C Language manages code in the unit of function, which is the smallest unit of managing code. A function is the code segment with a certain function main Function is the execution entry of a program, and there is only one int It's a data type, he said main The result of the function is an integer return There are two functions: 1. End the execution of the function 2.Returns a data to the caller of the function main The function is called by the operating system, and its return value is given to the operating system, which reflects how the program ends. There are usually three cases: integer An exception occurred (someone else's error) 0 business as usual negative An error occurred (your own error) Can pass echo $? Command to view the return value of the previous program
printf/scanf
Is a function in the standard library. It is responsible for inputting and outputting data. It is generally used to debug code
printf("---------\n");
Escape character:
some keys on the keyboard have no symbols. They are represented by some special character combinations. Writing only the special character combination is the escape character, \ n is one of them
\n line feed
\b backspace one character \ b \b backspace
\r back to the beginning of the line
\t tab for aligning data
\a the bell rings
\Represents a
%%Represents a%
C language takes a semicolon as the end of a line of code, and uses braces to divide the code area
2, Compiler
Responsible for translating the text file recording the code that people can understand into binary file that computer can understand, which is composed of preprocessor, compiler and linker gcc By GNU Organization for compilation Linux The kernel code is developed and put into a free compiler, which is adopted by default c89 Standards,-std=can gnu99 To set syntax standards Common parameters: -E Displays the results of the preprocessing -S Generate assembly code .s -c Compile only without linking .o -o Set the name of the compilation result -I Specifies the loading path of the header file -l Specify the library file to use -lm -Wall Check the code with stricter standards and display as many warnings as possible -Werror Treat warnings as errors
3, The process of turning C code into an executable file
1,Preprocessing translates source files into preprocessed files gcc -E code.c Display preprocessing results gcc -E code.c -o code.i Generate to.i Preprocessing file at the end 2.Compiler translates preprocessed files into assembly files gcc -S code.i Generate to.s Assembly file at the end 3,Compile the assembly file into binary and put it into the target file gcc -c code.s Generate to.o End target file 4,Links combine several object files into one executable gcc a.o b.o c.o... Generate one by default a.out Executable file for C File type for language: .h Header file .h.gch The compilation result of the header file will be used preferentially .c source file .i Preprocessing file .s Assembly file .o Target file .a Static file .so Shared library file
4, Data type
Why classify data: 1,Data in real society is its own category attribute 2,Classifying data can save storage space and improve operation efficiency Unit of storage space: Bit Bit a binary bit that can only store 0 or 1. It is the smallest unit of data stored by a computer Byte The basic unit of data stored in a computer Kb 1024 byte Mb 1024Kb Gb 1024Mb Tb 1024Gb Pb 1024Tb C Data in the language is divided into two categories: self built (designed by programmers: structure, union and class) and built-in( C Language (native) Note: you can use sizeof Operator evaluates the number of bytes of the type Integer: Signed signed signed char 1 -128~127 signed short 2 -32768~32767 signed int 4 Plus or minus 2 billion signed long 4/8 signed long long 8 19 bit integer starting with plus or minus 9 be careful: signed If you don't add it, you mean add it Unsigned unsigned unsigned char 1 0~255 unsigned short 2 0~65535 unsigned int 4 0~40 Hundred million unsigned long 4/8 unsigned long long 8 0~20 bit integer starting with 1 Note: because it is troublesome to define unsigned data, the standard library redefines these unsigned data types into the following types: Header files need to be included when using: stdint.h uint8_t uint16_t uint32_t uint64_t int8_t int16_t int32_t int32_t Floating point: float 4 double 8 long double 12/16 Note: all floating-point data adopt scientific calculation method, and careful conversion is required between binary and real data. Therefore, floating-point data takes much more time than integer data, and integer data is a good choice for programming Valid to six decimal places time ./a.out Calculate program run time Analog type: character char Characters are actually symbols or patterns. Integers are stored in memory, When it needs to be displayed as characters, it will be displayed according to ASCII The corresponding relationship in the code table shows the corresponding symbols or patterns '\0' 0 '0' 48 'A' 65 'a' 97 Boolean bool Prior C After the language bool Type, so c There can be no real boolean type in a language, stdbool.h The header file simulates Boolean types bool true false
5, Variables and constants
What is a variable: the data that can change during the operation of a program is called a variable, which is equivalent to a box for storing data definition: Type variable name; Naming rules: 1,It consists of letters, numbers and underscores 2,Cannot start with a number 3,Cannot have the same name as 32 keywords 4,See name and meaning (function, type, scope)...) use: Assignment: num = 100; Participate in operation: num * 10; Note: the default value of the variable is random. For the sake of safety, it should be initialized to 0; Input and output of variables: int printf(const char *format,...); Function: output data format:"Format information contained in double quotation marks (prompt information)+Placeholder)" ...: Variable list Return value: the number of output characters Type placeholder: C In language, the type of a variable is passed by means of a type placeholder %hhd %hd %d %ld %lld Signed %uud %ud %u %lu %llu Unsigned %f %lf %LF %c int scanf(const char *format,...); Function: input data format: "Format information contained in double quotes (placeholder)" ...: Variable address list Return value: the number of variables successfully entered be careful: scanf Type and address of required variable Variable address=&Variable name