【悦~】实现两个数减法

一、程序

在这里插入图片描述

main.c

#include <stdio.h>
#include "sub.h" 

int main()
{
	int a, b, c;
	printf("请依次输入被减数和减数:\n");
	scanf("%d%d", &a, &b);
	printf("a=%d, b=%d\n", a, b);
	c = sub(a, b);
	printf("result: %d - %d = %d\n", a, b, c);
	return 0;
}

sub.c

#include <stdio.h>
int sub(int a, int b)
{
	return (a - b);
}

sub.h

#ifndef SUB_H
#define SUB_H

int sub(int a, int b);

#endif

Makefile

sub: main.o sub.o
	gcc -o sub main.o sub.o
%.o: %.c
	gcc -c $<
clean:
	rm -rf *.o sub

二、运行

将上述四个文件放到同一个文件夹下,比如我放到了桌面的test文件夹中,路径:/home/clay/桌面/test

在这里插入图片描述

快捷键ctrl+shift+T打开终端,进入到该路径,命令如下:

cd /home/clay/桌面/test

接着输入make运行程序,效果如下图:

在这里插入图片描述

接着输入./sub运行可执行文件,效果如下图:

在这里插入图片描述

按照提示,输入被减数和减数,这里以8 3为例,效果如下图:

在这里插入图片描述

三、对比上一次的那一道题

编写程序,main.c调用fun1.c输出“This is fun1!”,调用fun2.c输出“This is fun2!”,注意两个字符串输出均有换行。
(1)、编写三个源文件
(2)、编写生成work可执行文件的Makefile

解题过程如下:
(1)
main.c

#include <stdio.h>
#include "fun1.h" 
#include "fun2.h" 

int main()
{
   fun1();
   fun2();
   return 0;
}

fun1.c

#include <stdio.h>
void fun1(void)
{
	printf(“This is fun1! \n”);
}

fun2.c

#include <stdio.h>
void fun2(void)
{
	printf(“This is fun2! \n”);
}

fun1.h

#ifndef _FUN1_H
#define _FUN1_H
void fun1();
#endif

fun2.h

#ifndef _FUN2_H
#define _FUN2_H
void fun2();
#endif

(2)
Makefile

work: main.o fun1.o fun2.o
	gcc -o work main.o fun1.o fun2.o
%.o: %.c
	gcc -c $<
clean:
	rm -rf *.o work
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReCclay

如果觉得不错,不妨请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值