windows下C语言头文件的运用

头文件 singnext.dingswords

printf("终止我每丝呼吸,让心灵穿透所有的秘密\n");

头文件 singtocj.h

printf("当无数的日月沧桑后,你会在谁身旁?\n");

头文件 myhead.h

#include <stdio.h>
#include <Windows.h>


    int a = 6000;
    int b = 10000;

头文件 calresult.h

# include "myhead.h"

float c;
c = (a + b)*0.035;

主程序文件

#include <stdio.h>
#include <Windows.h>

void main()
{
    #include "singnext.dingswords"
    #include "singtocj.h"
    //#include "myhead.h"
    #include "calresult.h"
    printf("小烤肠快回来吧,老张决定这个月发你工资%d,另外还有你比赛夺冠的奖金%d\n", a, b);
    printf("共计%d\n", a+b);
    printf("另外还有%f元努力训练奖励\n", c);
    getchar();
}

点评,以上代码显示了头文件(xx.h)文件的调用,这很像python from xx.py import xxx的模块调用过程。需要注意的是头文件貌似不支持自定义函数;声明 #include “你定义的头文件”可以出现在程序文件的任何位置,就像主程序文件使用的那样。

头文件和主程序文件部署目录如下图:

运行结果

陷阱:头文件重复引用

假如对主程序文件作如下更改----第8行引入头文件myhead.h:

 1 #include <stdio.h>
 2 #include <Windows.h>
 3 
 4 void main()
 5 {
 6     #include "singnext.dingswords"
 7     #include "singtocj.h"
 8     #include "myhead.h"
 9     #include "calresult.h"
10     printf("小烤肠快回来吧,老张决定这个月发你工资%d,另外还有你比赛夺冠的奖金%d\n", a, b);
11     printf("共计%d\n", a+b);
12     printf("另外还有%f元努力训练奖励\n", c);
13     getchar();
14 }

则会报错提示多次初始化,其原因在于calresult.h引入了myhead.h主程序再次引用构成了对文件中变量的多次引用!解决方案是注释掉上述代码第8行

转载于:https://www.cnblogs.com/saintdingspage/p/9468526.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值