第一单元-W1

本文介绍了在Linux环境下使用mkdir创建目录,vi编辑器编写C语言代码,包括添加、复制和修改源文件,然后用gcc编译器进行编译生成可执行文件,最后执行程序。示例包括计算浮点数之和、圆的面积、马拉松距离和华氏温度转摄氏温度的程序。
摘要由CSDN通过智能技术生成

一、通过mkdir创建多级目录,并进入目录

mkdir -p CPROGRAMS/W1
cd CPROGRAMS/W1
pwd    //查看当前目录位置 print working directory

二、创建add2.c

//1.通过touch命令创建
touch add2.c
  
//2.通过vi创建,不过是代码编写完成后,需要使用ESC+:,再加上W保存,才会创建add2.c

vi add2.c  

 add2.c内容 



/*  Read in Two Floats and Print Sum
    Jhyfugug Coder
    March, 21, 2023
*/

#include <stdio.h>

int main(void)
{
    float a, b, sum;
    printf("Input two floats:");
    scanf("%f%f", &a, &b);
    printf("a = %f, b = %f", a, b);
    sum = a + b;
    printf("\nsum = %f\n\n", sum);
    return 0;
}

三、复制add2.c并修改代码为add3.c

//1.可以通过touch命令直接创建add3.c,但是里面没有内容
touch add3.c
//2.可以通过vi命令模式到末行模式,直接复制add2.c的内容回写到当前文本界面
vi add3.c
//在命令行模式下输入:(进入末行模式),再使用读取命令 r add2.c
:r add2.c    //add2.c的内容就被读取到了add3.c里面,add3.c就能快捷编辑内容

add3.c内容



/*  Read in Three Floats and Print Sum
    Jhyfugug Coder
    March, 22, 2023
*/

#include <stdio.h>

int main(void)
{
    float a, b, c, sum;
    printf("Input three floats:");
    scanf("%f%f%f", &a, &b, &c);
    printf("a = %f, b = %f, c = %f\n", a, b, c);
    sum = a + b + c;
    printf("sum = %f\n\n", sum);
    return 0;
}

四、使用gcc将add3.c源代码编译成可执行文件

//1.gcc -o 目标可执行文件 源文件
gcc -o add3.exe add3.c
//2. gcc -Wall -g -o 目标可执行文件 源文件
gcc -Wall -g -o add3.exe add3.c
// -Wall    W为大写,-Wall 代表编译器在编译过程中会输出警告信息(Warning)
//-g    dubug,-g 代表编译器会收集调试(debug)信息
//-o    output, -o代表编译器会将编译完成后的可执行文件以你指定的名称输出到你指定的文件夹下。-o 的空格后的名称就是输出的文件的名称.

//再使用ls查看(List的缩写)
ls

五、执行可执行文件add3.exe

// 在当前文件目录,使用./命令+可执行文件。    ./ 代表当前目录 
./add3.exe

附录一:其他代码1-circle.c



/*
    Circle and Area
    Jhyfugug Coder
    March, 23, 2023
*/

#include <stdio.h>

#define PI 3.14159

int main(void)
{
    double area = 0.0, radius = 0.0;
    printf("Enter radius: ");
    scanf("%lf", &radius);
    area = PI * radius * radius;
    printf("radius of %lf meters; area is %lf sq. meters\n", radius, area);
    return 0;
}

其他代码2-marathon.c



/*  The distance of a marathon in kilometers
by Jhyfugug Coder
March, 23, 2023
*/

#include <stdio.h>

int main(void)
{
    int miles = 26, yards = 385;
    double kilometers = 0.0;

    kilometers = 1.609 * (miles + yards / 1760.0);
    printf("\nA marathon is %lf kilometers.\n\n", kilometers);
    return 0;
}

其他代码3-fahrenheit



/*  C For Everyone
    Homage to K&R
    Conversion of fahrenheit to Celsius

    C = (F - 32) / 1.8
    Jhyfugug Coder
    March, 23, 2023
*/

#include <stdio.h>

int main(void)
{
    int fahrenheit = 0.0, celsius = 0.0;

    printf("Please enter fahrenheit as an integer:");
    scanf("%d", &fahrenheit);
    celsius = (fahrenheit - 32) / 1.8;  //note conversion
    printf("\n %d fahrenheit is %d celsius.\n", fahrenheit, celsius);
    return 0;
}

 

附录二-参考资料:

1、Mac下调整tab键的长度

2、如何在 Linux 终端中删除文件和文件夹

3、linux中创建一次创建多个目录或创建多级目录

4、mac终端编辑文件退出?

5、在mac电脑的terminal里该如何运行c语言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值