当前位置:首页 > 科学研究 > 电子编程 > 正文内容

C语言教程04:程序的编译与运行

RonWang3年前 (2023-05-31)电子编程1364

源程序也称源代码,是指未编译的、按照一定的程序设计语言规范书写的文本文件,是一系列人类可读的计算机语言指令,可以用汇编语言或高级语言编写。计算机源代码的最终目的是将人类可读的文本翻译成为计算机可以执行的二进制指令,这种过程叫编译,通过编译器完成。不同的程序设计语言的源程序的扩展名是不同的,例如,用C语言编写的源程序,其文件扩展名为.c; 用 Java 语言编写的源程序,其文件扩展名为.java。

编译程序是将源程序译成能被CPU 直接识别的目标程序或可执行指令的程序。例如,用汇编语言书写的源程序要经过汇编程序译成用机器语言表示的目标程序,用高级语言书写的源程序要经过编译程序译成用机器语言表示的目标程序。

目标程序是经编译程序翻译生成的程序,文件扩展名为.obj。

可执行程序是经连接程序处理过的程序,文件扩展名为.exe。

需要指出的是,源代码的修改不能改变已经生成的目标代码。如果需要目标代码做出相应的修改,必须重新编译。

C语言是一种通过编译程序处理的高级程序设计语言,其处理流程具体为 源程序(.c) -【编译】- 目标程序(.obj) -【连接】-可执行程序(.exe)。

(1) 编辑C语言程序:当确定了解决问题的方案后,可以用C语言系统提供的编辑功能编 写一个 C 语言源程序,源程序的扩展名为.c。

(2) 编译 C 语言程序生成目标程序:由于计算机只能识别和执行由 0 和 1 组成的二进制文件,而不能识别和执行用高级语言编写的源程序,所以必须先用 C 语言系统的编译程序(即编译 器)对其进行编译,以生成以二进制代码形式表示的目标程序,目标程序的扩展名为.obj。 

(3) 连接生成可执行程序文件:将目标程序与系统的函数库以及其他目标程序进行连接装 配,才能形成可执行程序文件,可执行文件的扩展名为.exe。 

(4) 运行可执行程序文件:将可执行程序文件(扩展名为.exe)调入内存并运行,得到程序的结果。

【例 1-7】求 3.5、4.6 和 7.9 这 3 个数的平均值。 

 #include <stdio.h>
int main() 
{ float x,y,z,aver; 
  x=3.5; 
  y=4.6;
  
  z=7.9; 
  aver=(x+y+z)/3; 
  printf("aver=%.1f",aver);
 return 0; 
}

程序运行结果

aver=5.3


版权声明:本文为原创文章,版权归donstudio所有,欢迎分享本文,转载请保留出处!

本文链接:http://parentscn.com/?id=228

相关文章

DIY - Monochrome Light Cube Maker Introduction Step by Step

DIY - Monochrome Light Cube Maker Introduction Step by Step

First, let's take a look at the complete welded diagram (the shell needs to be purchased separat...

Arduino Project 030 - Line Following Robot

Arduino Project 030 - Line Following Robot

Project 30 Line Following RobotUse L298P Driver Line Following Robot Line o...

Arduino Project 030B - MX1508 H-Driver Motor

Arduino Project 030B - MX1508 H-Driver Motor

MX1508 H-BridgeDual Motor DriverThe driver can drive up to two motors. The H-Bridge dual motor drive...

Arduino Project 034 - TM1637 4Digital 7Segment Display Module

Arduino Project 034 - TM1637 4Digital 7Segment Display Module

A standard 4-digit 7-segment display is needed for clock, timer and counter projects, but it usually...

Arduino Project 023 - Liquid Crystal Displays - Hello World

Arduino Project 023 - Liquid Crystal Displays - Hello World

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14...

Arduino Project 040 - Ultrasonic Distance Alarm

Arduino Project 040 - Ultrasonic Distance Alarm

The sensor consists of two primary components: a transmitter and a receiver . The transmitter is res...