Essential for programming Xiaobai — knowledge points of mainstream language c

Time:2021-11-24

For programming languages, it is often seen that they are interconnected because of the language camps they support. In fact, it is not necessary at all. It is just a tool. When most mainstream languages can be used, maybe you won’t be biased. The essence is just to describe a task of the computer, but the focus of each language design is different. We’d better not stay at the language level to argue. It’s better to spend time on the essence of computer implementation principle and structure, so as to better understand what is described in each line of programming language in the computer.

About C language

C language is the most classic language. The running environment of many other languages is also written in C. people who write programs can’t understand C language! Referring to C is bound to be related to pointers. The pointers that confused you in college are the basis of the infinite power of C language. C language may be lack of design and writing efficiency at a higher level, but it is classic enough and easy to manipulate the bottom. Although the risk is not small, the pointer is very powerful. In addition, ANSI C also enhances the migration of C programs in different operating systems. Here are some basic knowledge of C language.

 

C/C++Learning skirtseven one two   two eight four   seven zero five, whether you areXiaobai is still an advanced person, whether you want to change your career or join it, you can learn about it, make progress together and learn together! There are development tools in the skirt, and many dry goods and technical data are shared!

 

Essential for programming Xiaobai -- knowledge points of mainstream language c

 

Translation stage

The written C program needs to be compiled into executable machine instructions before it can run, which is translation. The main steps of translation are compilation and linking. Compilation is from source code to object code, and linking is to link each object file to form an executable program. Of course, the linker will also introduce the functions of all standard C function libraries used by the program. Sometimes the compilation process also takes preprocessing as a stage, which mainly processes the source file, such as replacing #define with the actual value and filling in the contents of the file specified by #include. The following is an example of compiling and linking using gcc. After compiling and linking, the executable program is obtained. These two steps are completed through GCC. The command is GCC hello. C – O hello. Finally, run. / hello and output “Hello world”.

#include

int main()
{
    printf("hello world");
}

 

Essential for programming Xiaobai -- knowledge points of mainstream language c

 

If we write multiple C files, the compiler will compile them into multiple obj object files, and then link all the object files through the linker to generate executable files.

 

Essential for programming Xiaobai -- knowledge points of mainstream language c

 

About extensions

Note that the target file extension of windows system is obj. Generally, it will not be deleted after the link is completed. The target file extension of UNIX like system is O, which is usually deleted after the link is completed. The executable file extension of windows system is exe, while the executable file name of UNIX like system can be named arbitrarily. In addition, C language source files are generally suffixed with C, while header files are suffixed with h. Although there is no mandatory provision, everyone will abide by this agreement.

About compiler

In the translation stage, the C language code needs to be changed into an executable program, which is completed by the C compiler. There are also many C compilers. The common ones are as follows:

  • GCC, GCC (GNU compiler collection), GPL licensed compiler free software developed by GNU. At first, it was only used as a C language compiler, but later it developed into a variety of language compilers, such as C, C + +, Java, Android, Objective-C, FORTRAN and so on. Now many UNIX like operating systems come with GCC as a standard compiler.
  • MS C is integrated and released together with Microsoft’s visual studio. It is a complete integrated development environment provided by Microsoft. After compilation, it can run on all Microsoft operating systems. For example, vs usually uses CL compiler.
  • Clang, a lightweight C / C + + / Objective-C compiler based on llvm, is commonly used in MAC systems.
  • Turbo C, a popular C compiler, is small and fast.
  • CC, or C compiler, is an ancient compiler for UNIX systems. Many classic books will see this compiler. In order to maintain compatibility, the current Linux system will point CC as a symbolic connection to GCC, that is, / usr / bin / CC – > GCC.

GCC compilation example

Take GCC of Linux system as an example to see several compilation examples. If the code of Hello. C is as follows,

#include

int main()
{
    printf("hello world");
}

We directly use the following GCC command to compile it without any parameters. At this time, an executable file named out. A will be generated, and “Hello world” can be output through. / a.out.

gcc hello.c

If you add name. H / name. C and adder. H / adder. C header files and source files, and slightly modify hello. C, the codes of the three files are as follows.

//name.h
char* get_name();

//name.c
char* get_name() {
	char* name = "seaboat : ";
	return name;
}
//adder.h
int add(int a, int b);

//adder.c
int add(int a, int b) {
	return (a + b);
}
//hello.c
#include 
#include 
#include 
#include"adder.h"
#include"name.h"
int main()
{
	char* name = get_name();
	char* hello = "hello world";
	char* output = (char*)malloc(strlen(hello) + strlen(name));
	sprintf(output, "%s%s", name, hello);
	printf("%s\n", output);
	int a = 1;
	int b = 3;
	printf("a + b = %d\n", add(a, b));
}

You can compile and link multiple source files through the following commands, and finally generate an executable file named a.out. When we execute the executable through. / a.out, it will output “seaboat: Hello world a + B = 4”.

gcc name.c adder.c hello.c

We can also compile the name. C and adder. C files to generate target files through the following two commands: adder. O and name. O. Then compile the hello. C source file through the following third line command. After compilation, it will automatically connect with the name. O and add. O target files.

gcc -c adder.c
gcc -c name.c
gcc name.o adder.o hello.c

In addition, you can compile multiple source files and generate their corresponding target files through the following commands, which means that they are not linked.

gcc -c  name.c adder.c hello.c

For multiple target files, if you want to link them, you can generate executable files through the following command.

gcc  name.o adder.o hello.o

If we want to name the generated executable file, we can use the first line of command below to generate an executable file named hello. Similarly, you can specify the executable file name when connecting multiple target files. As shown in the second line below, an executable file named hello2 will be generated.

gcc name.c adder.c hello.c -o hello

gcc  name.o adder.o hello.o -o hello2

About character sets

When writing C language, the source code can include the following character sets:

  1. English uppercase and lowercase letters
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
  1. Decimal Arabic numerals
0 1 2 3 4 5 6 7 8 9
  1. Other symbols
! " # % & ' () * + , - . / :
; < = > ? [ ] \ ^ _ { } | ~
  1. Blank character
Space, horizontal tab, vertical tab, line feed, page feed

About notes

C language provides two annotation methods: start with / * and end with * / to annotate multi line code, and start with / / to annotate single line code. Generally speaking, commenting on the source code means that it will be cleared by the preprocessor at compile time and replaced with spaces.

/*
The first annotation method
*/

//The second annotation method

About identifiers and keywords

Identifier is the name of variables, functions, types, structures, macros, etc. by our developers. C language also requires us to name them according to its provisions. According to regulations, the identifier can be composed of English upper and lower case letters (a ~ Z, a ~ z), Arabic numerals (0 ~ 9) and underscores (). The following points should be noted:

  • The request cannot start with a letter.
  • C language is sensitive to case letters.
  • C language does not limit the length of the identifier, but the standard allows the compiler to ignore the characters after the 31st bit. The specific number of the first bits intercepted is implemented by different compilers. When the intercepted strings are the same, it is considered to be the same identifier.
  • The identifier should not be named randomly, and the identifier name should have corresponding meaning as far as possible.

Of course, C language also retains 32 special keywords. The identifiers we name cannot be the same as them, otherwise an error will be reported. The 32 keywords are as follows:

Essential for programming Xiaobai -- knowledge points of mainstream language c

 

Author: Superman Wang Xiaojian
Source: Nuggets