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行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM