2017-05-04 26 views
-1

我是編程新手,我正在參加這個在線編程課程CS50。所以我有一個任務是用C編寫一個程序,用戶輸入一些單詞(無論單詞前後有多少空格),我們必須打印每個單詞的第一個首字母。所以我做了這個節目:有人可以解釋變量聲明在C

#include <stdio.h> 
#include <cs50.h> 
#include <string.h> 
#include <ctype.h> 

    int main(void) 
    { 
    int n; 
    int i; 
    string name = get_string(); 
    if(name != NULL) 
    { 
     if (name[0] != ' ') 
     { 
      printf("%c", toupper(name[0])); 
     } 
     for(i = 0, n = strlen(name); i < n; i++) 
     { 
      if(name[i] ==' ' && isalpha(name[i+1])) 
      { 
       printf("%c", toupper(name[i+1])); 
      } 
     }printf("\n"); 
    } 
    } 

但經過我聲明的變量INTñ它正確地只是做; int i; 在此之前,我甚至無法編譯程序。爲什麼?起初,我宣稱int i循環,但程序甚至沒有編譯。只是運氣不好,我試圖宣佈外部循環及其正確。我不明白這一點。有人可以解釋嗎? :)

+1

'的for(int i = 0;'...只能在C++中,也許在C99,但不是在標準C.所以,你必須事先聲明它,像你這樣 –

+0

@Karlaaz告訴您如何宣佈這些。程序中沒有編譯的變量 –

+10

@ Jean-FrançoisFabreehm,C99 *是*標準C.標準C現在是C11。 –

回答

1

所有變量和函數必須聲明纔可以使用。必須先聲明變量i,然後才能將其用作for循環中的索引。

在1989/1990標準和更早ķ& R輸入語言版本,所有聲明都必須來之前的塊中的任何可執行語句:

void foo(void) 
{ 
    /** 
    * The variable i is used to control a for loop later on in the function, 
    * but it must be declared before any executable statements. 
    */ 
    int i; 

    /** 
    * Some amount of code here 
    */ 

    for(i = 0; i < some_value; i++) // K&R and C90 do not allow declarations within the loop control expression 
    { 
    /** 
    * The variable j is used only within the body of the for loop. 
    * Like i, it must be declared before any executable statements 
    * within the loop body. 
    */ 
    int j; 

    /** 
    * Some amount of code here 
    */ 
    j = some_result(); 

    /** 
    * More code here 
    */ 
    printf("j = %d\n", j); 
    } 
} 

隨着1999年的標準,聲明可以與其它混合語句,它們可以表現爲for循環的初始表達式的一部分:

void foo(void) 
{ 
    /** 
    * Some amount of code here 
    */ 
    for (int i = 0; i < some_value; i++) // C99 and later allow variable declarations within loop control expression 
    { 
    /** 
    * Some code here 
    */ 
    int j = some_result(); // declare j when you need it 
    /** 
    * More code here 
    */ 
    printf("j = %d\n", j); 
    } 
} 

上述兩個片段之間的主要區別在於,在第一種情況下,是i在整個函數的主體上可見,而在第二個代碼段中,僅在for循環的主體內可見。如果您需要i要到下面的for迴路中的任何代碼可見的,那麼你需要聲明它的循環控制表達式之外:

int i; 
for (i = 0; i < some_value; i++) 
{ 
    ... 
} 
... 
do_something_with(i); 

再次,i必須聲明,纔可以在循環使用控制表達;只是在第二種情況下,該聲明是循環控制表達式的一部分。

編輯

我不知道你用的是什麼開發環境或編譯器。您可能想要查看是否可以指定要編譯的語言版本(例如,在gcc中,您將爲1989/1990版本指定-ansi-std=c90,爲1999版指定-std=c99)。

+0

這是2011版本。 'std = c11' – Karlaaz

0

首先 - 歡迎來到C語言!我認爲C是一個偉大的語言開始。

變量聲明 - 在C中,我們被允許在局部變量上聲明局部變量 - 在函數,循環,if語句等的開頭。

例如:

  1. 我們無法運行此:

    for(int i = 0; i<4: ++i) { printf("%d",i); } 
    

    的聲明是在for聲明 - 不是在範圍的開始。在C++中,此代碼將起作用。

  2. 然而,我們可以做到以下幾點:

    int foo() 
    { 
        int i; 
        for(i=0;i<4; i++) { ...} 
    } 
    

備註:通過局部變量和參數是在堆棧中分配。當我們聲明一個局部變量時,我們將變量推入棧中,然後到達作用域的末尾 - 變量彈出。

+0

您對「我們無法執行此操作」的評論僅適用於古老而過時的C90標準。 C99和C11都允許您聲明不允許的C++樣式表示法。因此,這個答案並不真正有幫助;它說明了在過去的千年中以前的準確情況,而不是現在的情況。 –

+0

'傳遞的參數被分配到棧上 - 要小心,這不一定是真的。某些平臺(如x86-64)可能會通過寄存器傳遞參數。 C語言的定義只是要求事物應該如何表現,而不是應該如何實施。 –