2015-01-04 35 views
0

所以我寫了這段代碼,我想問一下是否有可能在其他事情之前先寫main?main()在第一個地方C

#include <stdio.h>    // Standard Ein-/Ausgabefunktionen 
#include <at89c51cc03.h>  // CC03er-Grundregister 




#define CS_LCD 0xffb8 

xdata unsigned char eak_io @0xff80; 
xdata unsigned char DIS_IR_W @CS_LCD+0x0; 
xdata unsigned char DIS_DR_W @CS_LCD+0x1; 
xdata unsigned char DIS_IR_R @CS_LCD+0x2; 
xdata unsigned char DIS_DR_D @CS_LCD+0x3; 




void init_schnittstelle(void) 
{ 
    SCON=0x52;     // Initialisierung 
    TMOD |=0x20;    // Timermodus 8-bit auto-reload 
    TH1=0xfa;     // 4800 Baud 
    TR1=1;     
} 


void ms_warten(unsigned int multiplikator) 
{ 
    unsigned int i,j; 
    for(i=0;i<multiplikator;i++) 
    { 
     for(j=0;j<123;j++); 
    } 
} 


void dis_ready(void)   
{ 
    while ((DIS_IR_R & 0x80)!=0); 
} 



void init_lcd(void)   
{ 
    unsigned char i; 

    for (i=0; i<=2; i++) 
     { 
     DIS_IR_W=0x30; 
     ms_warten(10); 
     } 

    // Function Set: DL=1, N=1, F=0 
    dis_ready();     
    DIS_IR_W=0x38; 

    // Display ON/OFF: D=1, C=1, B=0 
    dis_ready();     
    DIS_IR_W=0x0c; 

    // Entry Mode Set: I/D=1, S=0 
    dis_ready();      
    DIS_IR_W=0x06; 
} 


void dis_clear(void) 
{ 
    dis_ready();    
    DIS_IR_W=0x01;    
} 


void dis_csr_set(unsigned char z, unsigned char s) 
{ 
    unsigned char csr_pos; 
    switch (z)      
     { 
     case 0 : csr_pos=s;   
      break; 
     case 1 : csr_pos=s+0x40; 
      break; 
     case 2 : csr_pos=s+0x14; 
      break; 
     case 3 : csr_pos=s+0x54; 
      break;  } 
    dis_ready();      
    DIS_IR_W=(csr_pos | 0x80);  
} 

void dis_text(unsigned char csr, unsigned char z, unsigned char s, char *a) 
{ 
    unsigned char i; 
    if (csr==1) dis_csr_set(z,s); 
    i=0;       
    while(a[i]!=0) 
     { 
     dis_ready();      
     DIS_DR_W=a[i];    
     i++;      
     } 
} 


void main(void) 
{ 
    char aktuellerWert;   
    init_schnittstelle();   
    init_lcd();      
    while(1) 
    { 
     RI = 0;      
     while(!RI);     
     if(SBUF != aktuellerWert) 
     { 
      aktuellerWert = SBUF; 
      switch(aktuellerWert)  
      { 

       case 'O': dis_clear();  
          dis_text(1, 1, 2, "blabla"); 
          dis_text(1, 2, 1, "blabla"); 
          dis_text(1, 3, 3, "blabla"); 
          break; 

       case 'G': dis_clear();  
          dis_text(1, 1, 2, "blabla"); 
          dis_text(1, 2, 1, "blabla"); 
          break; 
       case 'R': dis_clear();  
          dis_text(1, 1, 2, "blabla"); 
          dis_text(1, 2, 1, "blabla"); 
          break; 
      } 
     } 
    } 
} 

所以我想#定義之前寫的主要方法,鄰,它將在第一位置或多或少。

謝謝!

+1

短的理解什麼是*函數原型*是(提示的網頁搜索),我什麼也看不到,從這樣阻止你。 (不相關的,'void main'不是標準的,停止那個)。 – WhozCraig 2015-01-04 12:29:09

+0

你爲什麼不嘗試一下?不,這是行不通的,因爲僅僅包括將文件複製到你的項目,如果你使用一個函數出的這些功能是在不知道編譯的時候,因爲它是越往下包含 – Rizier123 2015-01-04 12:29:46

+0

試試吧,看它是否編譯。如果沒有,請閱讀錯誤消息。 – 2015-01-04 12:29:46

回答

2

編譯器在被調用之前必須知道您在代碼中使用的函數的一些內容。函數的實際實現/定義不是必需的,在調用之前只需要聲明(函數原型)。這可以通過兩種方式完成:

  1. 在頭文件中。如果您想在多個C文件中使用您的功能,請使用此方法。
  2. C源文件中的任何位置(最好在開頭)。這些函數僅限於文件範圍,即它們僅適用於聲明它們的C源代碼。

函數原型是這樣的:

return_type function_name(type_t param1, type_t param2); 

例如:

int sum(int a, int b); 

將宣佈的功能總和,告訴

  1. 的函數命名和編譯器存在某處
  2. 這個函數有兩個整數作爲參數
  3. 該函數返回一個整數。

此時,編譯器不知道如何的功能實現。但是,由於編譯器知道它存在以及它看起來像什麼,它會很好地編譯你的代碼。

下面是使用你的代碼很短的例子:

#include <stdio.h>    // Standard Ein-/Ausgabefunktionen 
#include <at89c51cc03.h>  // CC03er-Grundregister 

// Function prototypes for functions used in main() are here, now the compiler 
// is aware of them 

void init_schnittstelle(void); // Note the semicolon 
void init_lcd(void); 

// I didn't include the prototype for the function ms_warten(), since the main() 
// Doesn't use it directly. Declatring it beforehand wouldn't hurt, though. 

int main() 
{ 
    // Your code here 
} 

#define CS_LCD 0xffb8 // This isn't used by main() either, so the compiler 
         // doesn't needto know about it before the 
         // main() fucntion. 

xdata unsigned char eak_io @0xff80; 
xdata unsigned char DIS_IR_W @CS_LCD+0x0; 
xdata unsigned char DIS_DR_W @CS_LCD+0x1; 

void init_schnittstelle(void) 
{ 
    // Your code here     
} 


void ms_warten(unsigned int multiplikator) 
{ 
    // Your code here 
} 
0

有一種叫做函數聲明。相對於有函數定義

int foo(int x) 
{ 
    return x + 42; 
} 

,它告訴編譯器的功能是什麼,一個函數聲明告訴編譯器如何調用一個函數。這將是foo有效的函數聲明:

int foo(int x); 

通知缺少括號和尾隨分號。聲明一個函數足以讓編譯器知道如何調用它。因此,如果您預先聲明main()所調用的所有函數,則可以先定義主函數。這裏的是什麼樣子的例子:

void init_schnittstelle(void); 
void ms_warten(unsigned int multiplikator); 
void dis_ready(void); 
void init_lcd(void); 
void dis_clear(void); 
void dis_csr_set(unsigned char z, unsigned char s); 
void dis_text(unsigned char csr, unsigned char z, unsigned char s, char *a); 

void main(void) 
{ 
    /* Code hier */ 
} 

慣用的順序爲程序中的事情是這樣的:

  1. 功能測試宏
  2. #include指令
  3. #define指令
  4. 類型和枚舉聲明
  5. 函數聲明
  6. 變量定義
  7. 函數定義

注意到有一堆東西你之前定義main(),但你仍然可以main()定義的第一個函數。

相關問題