C language compilation process, C language compilation process

Source: Internet
Author: User

C language compilation process, C language compilation process

The result of running a source program usually needs to be compiled and executed. The C language compilation and execution process is as follows:

Compilation process:
The compilation function is to convert a high-level language that people can understand into a binary language that computers can understand. It can be divided into the following six phases:

  • Lexical analysis stage: analyzes the lexical rules of a language, lexical rules can be expressed by regular grammar or regular syntax, which refers to a set of languages and regular expressions that are limited to automatic identification of regular grammar.
  • Syntax analysis stage: Based on lexical analysis, words are divided into various types of syntax units, which analyze the source program structure based on the syntax rules of the language.
  • Semantic Analysis stage: Check for semantic errors in the code to prepare for the code generation stage.
  • Intermediate code generation stage: After the above stage, an intermediate code is generated, that is, the source code is converted into an internal sign language, which is based on the intermediate code generation rules.
  • Code optimization stage: the intermediate code is transformed and optimized to save time and space and improve execution efficiency.
  • Target code generation stage: converts intermediate code into command code on the machine. The generated target code is related to the hardware and system of the machine.

A source file can be converted into a target code file through the above six processes, and the advanced language can be converted into a machine language.

The complete process of C language compilation is as follows:

1. Compile preprocessing
Process pseudocommands and special symbols. Pseudoinstructions generally refer to commands starting with "#" and include the following four aspects:
(1) macro definition commands
Macro definition, also known as macro replacement, refers to replacing another formula with one expression, or macro for short.
(2) conditional instructions
Conditional commands allow programmers to define different macros to control the execution of program code, the code to be executed, and the code to be skipped. Pre-compilation removes unnecessary code.
(3) header file commands
For example#include<stdio.h>To include the corresponding header file into the program, including the header file to include the system-defined functions in the header file so that the program can directly call it.
(4) special symbols
During pre-compilation, special symbols are detected, such as LINE and FILE, which are processed as special symbols and replaced by appropriate values.

After the source file is pre-compiled, it becomes a file without macro definition, header files, conditional commands, and special symbols. This file has the same functions as the source file, but the content has changed.

2. Compilation and Optimization
Compile: Perform lexical, syntax, and Semantic Analysis on the pre-compiled files to convert them into intermediate or assembly code with equivalent functions.
Optimization: the code is optimized to reduce the time and space of the code and improve the execution efficiency.

3. Assembler
To execute a program, it must be converted into a machine language. The assembler converts the assembly code into machine code, that is, the code that the machine can recognize.

4. Link Program
The linked program connects the target files related to each other, that is, all these files are linked into a unified whole and transferred to the machine's memory, which means that the target files can be called each other.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.