2017-01-25 5 views
-3

main関数から関数を呼び出そうとしていますが、コンパイルして実行したときに乱数が返されました。私はユーザにyrBorn関数で数値を入力するように依頼し、それをmain関数に返し、最終的にそれを端末に表示します。作成した関数をint main()Cプログラミングに呼び出す方法は?

int yrBorn(int); 
#include<stdio.h> 
int yrBorn (int yearBorn)  
{ 
    printf("Enter the year you were born in: "); 
    scanf("%d", &yearBorn); 
    return yearBorn; 
} 

int main() 
{  
    printf("%d",yrBorn); 
} 
+2

はぁ? 'yrBorn'は関数です。なぜあなたはその価値を印刷していますか?それはあなたが関数を呼び出す方法ではありません - Cの本やチュートリアルでそれを教えてくれるでしょう。 – kaylum

+2

現代のCのためのCの本を手に入れて、章を調べてください。章をスキップしないでください。 – Olaf

答えて

0

あなたは、このようなあなたのコードを理解する必要があります:あなたが期待するように動作するようにプログラムを修正

int yrBorn(int); // function prototype say it must accept a int argument 
#include<stdio.h> 
int yrBorn (int yearBorn) //it must accept a argument of int type because of this definition  
{ 
    printf("Enter the year you were born in: "); 
    scanf("%d", &yearBorn); 
    return yearBorn; 
} 

int main() 
{  
    printf("%d",yrBorn); // You are passing function pointer here not a function call use like yrBorn(1994) 
    printf("%d",yrBorn(1994)); //it will work :) 
} 
0
#include<stdio.h> 

int yrBorn(); 
int yrBorn()  
{ 
    int yearBorn = 0; 
    printf("Enter the year you were born in: "); 
    scanf_s("%d", &yearBorn); 
    return yearBorn; 
} 

int main() 
{ 
    printf("%d", yrBorn()); 
} 

を!

0

構文が正しくありません。例えば

と仮定し、私は数を取り、四角、それを返す関数を作りたい、私はあなたのコードでは、のようなので、

#include<stdio.h> 

int square(int); //Function Prototype Declaration Telling Compiler 
        //That there would be function named square whose   
        //takes an integer and whose return type is integer. 

int main() 

{ 
    int x = 4; 
    int y ; 
    y = square(x);   //Calling Square Function passing Value `x` 
          //And Storing it in `y`. 
    printf("%d\n" , y);  //Printing `y` 

    //Or You Could As Well Do This 
    printf("%d" , square(x));  //Without Storing Printing return 
            //value of square(x) 

    return 0; 
} 


int square(int k){ //Function Definition Contains The Code What The  
    int r = k*k;  //Function Does , Here it take the value passed  
    return r;   //to the function in this case `x` and store it 
         //in `k` , and then initialise `r` to be `k*k` 
    }     //and then returns ` 

を行くだろう

int yrBorn(int); 
    #include<stdio.h> 
    int yrBorn (int yearBorn)  
    { 
     printf("Enter the year you were born in: "); 
     scanf("%d", &yearBorn); 
     return yearBorn; 
    } 

    int main() 
    {  
    printf("%d",yrBorn);  //Here You are not calling function. 
    printf("%d" , yrBorn(0)); //This will work as in your prototype 
           //declaration you have declared 
           //function to take a int parameter and 
           //return int 
    } 

あなたは募集だろうか私はこれだと思った。

#include<stdio.h> 
    int yrBorn(void); 
    int yrBorn (void) //Here Function is Expected to take no arguments 
         //and return int value. 
    { 
    int yearBorn; 
    printf("Enter Your Birth Year : "); 
    scanf("%d" , &yearBorn); 
    return yearBorn; 

    }  

int main(){ 
    printf("%d" , yrBorn());  //You Would Call it like this 
    } 

functi onは引数を受け入れません。

あなたが既に知っているか、後であまりにも同様の誤解に直面するだろうか、すべてを忘れて完全な献身とまともなCブックを読んでください、私はこのシリーズNptel - Introduction to C Programmingを見て、初心者のための最高のCプログラミングブックのため、このThe Definitive C Book Guide and Listをチェックすることをお勧めします。

0
printf("%d",yrBorn); 

これは関数の呼び出し方法ではありません。あなたはyrBornに電話する代わりに、のアドレスを受け取ります。その住所はintと解釈され、そのまま印刷されます。

関数を呼び出すには、()演算子を使用する必要があります。関数へのパラメータはすべて括弧の中に入ります。

また、関数はパラメータを取っていますが、その値では何も実行していません。パラメータを取るのではなく、この変数は関数にとってローカルでなければなりません。

だから、これらの修正プログラムで、あなたの関数は次のようになります。

int yrBorn()  
{ 
    int yearBorn; // local instead of a parameter 
    printf("Enter the year you were born in: "); 
    scanf("%d", &yearBorn); 
    return yearBorn; 
} 

そして、あなたはこのようにそれを呼び出す:

printf("%d",yrBorn()); 
関連する問題