2013-03-09 112 views
1

我想學習在C中創建頭文件並將其包含在main.c的func()中。我創建了一個名爲call()函數的簡單tut1.c文件和一個名爲call()的extern tut1.c函數的tut1.h頭文件。那就是它,現在我在Linux Fedora上使用eclipse Juno進行C/C++。我沒有得到任何編譯錯誤,但代碼不會輸出?我徒勞地嘗試了控制檯和日食。你可以檢查嗎?由於C程序編譯但沒有輸出

---main.c----- 

#include <stdlib.h> 
#include <string.h> 
#include <stdio.h> 
#include "tut1.h" 

int main (void) 

{ 
    int tut1(void); 

    return 0; 
} 

-----tut1.c------ 

#include <stdio.h> 
#include <stdlib.h> 
#include "tut1.h" 

int call (void) 
{ 
    int *ptr; 
    int i; 

     ptr = &i; 
     *ptr = 10; 

    printf ("%d we are printing the value of &i\n", &i); 
    printf ("%d we are printing the value of *ptr\n", *ptr); 
    printf ("%d we are printing the value of ptr\n", ptr); 
    printf ("%d we are printing the value of &ptr\n", &ptr); 

    getchar(); 
    return 0; 
} 

----tut1.h---- 

#ifndef TUT1_H_ 
#define TUT1_H_ 

extern int call (void); 

#endif 
+0

爲什麼你在'main()'中聲明一個不存在的函數('tut1()')? – 2013-03-09 18:27:45

+0

沒有輸出,因爲你沒有調用任何東西 - 只是聲明一個函數,然後返回0. – 2013-03-09 18:29:06

+0

請注意,該程序還會調用未定義的行爲:指針和地址**必須使用「%p」格式說明符和參數**必須**(投給)一個'void *'。 – Jens 2013-03-09 19:40:53

回答

5

,因爲你不能從你的main()函數調用call()功能你沒有看到任何東西。

main()函數是運行程序時的默認入口點,即在執行過程中被調用的第一個函數。在您main()只是聲明瞭一個功能,你似乎沒有在任何地方定義

int main (void) 

{ 
    int result = call(); 

    return 0; 
} 

順便說一句,這條線int tut1(void);

要執行call()你需要從main()稱之爲如下功能。所以我已經在上面顯示的代碼中刪除了它。

+0

謝謝大家。是的,我意識到我的錯誤。非常感謝您的反饋,每一個都很有價值。乾杯 – user2152138 2013-03-09 23:38:45