如何将您创建的函数调用到int main()c编程中?

时间:2017-01-25 02:30:33

标签: c function scanf

我试图从main函数调用一个函数,但是当我编译并运行它时它给了我一个随机数。我想要求用户在yrBorn函数中输入一个数字,然后将其返回到main函数,最后将其显示在终端中。

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);
}

4 个答案:

答案 0 :(得分: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 :)
}

答案 1 :(得分: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());
}

修改程序以按预期工作!

答案 2 :(得分: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 
   }

请注意,括号之间没有任何内容,因为函数不接受任何参数。

请阅读一本体面的C书,完全奉献忘记了你已经知道的一切,或者你以后也会遇到类似的误解,我建议你看这个系列Nptel - Introduction to C Programming并检查这个The Definitive C Book Guide and List以获得最佳C编程初学者书籍。

答案 3 :(得分: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());