2017-07-17 105 views
-3

我想知道是否用戶定義函數的格式,我寫回車(x x x)是否正確。 因爲當我編譯我的代碼時,我必須輸入2次輸入。 這可能是一個愚蠢的錯誤,因爲我剛開始學習C語言 ****我的代碼:****``簡單C源代碼中的錯誤?

#include<stdio.h> 
long cube(long x); 
long input,answer; 
int main (void) 
{ 
    printf("Enter a number:"); 
    scanf("%ld ",&input); 
    answer = cube(input); 
    printf(" The cube of %ld is %ld",input ,answer); 
    return 0; 
} 
long cube(long x) 
{ 
    return (x*x*x); 
} 

**** ****回答

#include <stdio.h> 
long cube(long x); 
long input, answer; 
int main(void) 
{ 
printf("Enter an integer value: "); 
scanf("%d", &input); 
answer = cube(input); 
printf("\nThe cube of %ld is %ld.\n", input, answer); 
return 0; 
} 


long cube(long x) 
{ 
long x_cubed; 

x_cubed = x * x * x; 
return x_cubed; 
} 
+0

郵報答案的答案(有一些解釋)*我敢肯定,這是一個重複反正* – pmg

+1

它真的不清楚你在這裏問什麼 – hardillb

+3

提示:爲什麼你在''%ld「'中有一個空格?代碼在數字後面尋找空格,所以'scanf()'沒有完成,直到找到_all_白色空格出現在數字後 – chux

回答

0

刪除「%ld」後的空格將需要一個輸入。 根據你的代碼, scanf(「%ld'&輸入); 這裏編譯器首先需要'%ld'的一個輸入,然後等待'%ld'後使用的空格。一個輸入步驟後, 你應該使用, 的scanf(「%LD%」,&輸入)。