如何从另一个函数调用局部变量c

How to call a local variable from another function c

我是编程新手。我想从函数中调用(第一,第二,第三函数 "enter" 和函数 "math" 中的变量)。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void math (void);
void first (void);
void second (void);
void third (void);
void enter(void);
void scan(void);


int main() 
{

 first();  //function
 second(); //function
 third();  //function
 enter();  //function
 printf("\n\n Would you like to edit? (Y/N)\n\n");
 scan();   //function
 return(0);
}

这是用户输入:

void first (void){
float a;
printf("Enter First number: ");
scanf("%f",&a);
 }

 void second (void){
 float b;
 printf("Enter Second number: ");
 scanf("%f",&b);
  }

  void third (void){
  float c;
  printf("Enter Third number: ");
  scanf("%f", &c );
  }

如何将用户输入的变量调用到下面的函数中?

  void enter(){
  float a,b,c;
  printf("you have entered a: %f, b: %f, c:%f", a,b,c);
     }

这是一个循环,以防用户想要编辑或继续初始输入的数字。

    void scan(void){
    int ch;
    scanf("%d", &ch);
    if (ch==1){
        main();

        }else{
        math();
        }
        }

在这里,我还想从函数(第一,第二,第三)中调用输入变量,以执行一些数学计算。

        void math (void){
           float a,b,c;
           float x,y,z;
           x=a+b+c;
           y= powf(x,2);
           z=sqrt(y);

           printf("Final Result of Addition is: %f \n Final Result of 
           Multiplication is: %f \n Final Result of squareroot  is:%f\n 
           ",x,y,z);
                          }

局部变量(顾名思义)是局部变量,在函数结束后不能保留它们的值。您有两个选择:

  1. 让函数 first、second 和 third return 成为它们的数字,另一个函数将它们作为参数。
  2. 创建 a,b,c 全局变量。

第二个要简单得多,但它被称为糟糕的编程风格。

例如在您的 Main 中声明 float a,b,c; 并将您的函数更改为 return 浮点数 float first() { ... } 然后写a = first();

函数最好不要依赖或修改它们外部的数据。这并不总是可能的,但如果可以在定义时考虑到该目标,那就更好了。

在您的例子中,函数 firstsecondthird 基本上做同样的事情。最好只使用一个函数,给它起一个更合适的名字,比如read_float,改变return类型,让用户输入的值return编辑到调用函数。

声明为:

float read_float(char const* prompt);

然后,您可以将其用作:

float a = read_float("Enter the first number:");
float b = read_float("Enter the second number:");
float c = read_float("Enter the third number:");

等等

函数 enter 没有正确表达它的作用。它应该重命名为更合适的名称,例如 display_input_values。它可以接受用户输入的值作为其参数。

声明为:

void display_input_values(float a, float b, float c);

并将其用作:

float a = read_float("Enter the first number:");
float b = read_float("Enter the second number:");
float c = read_float("Enter the third number:");
display_input_values(a, b, c);

scan函数也没有表达清楚它的意思。您可以将 scan 分成两个函数 -- 一个 return 让您选择是继续下一个输入还是调用一个函数来计算用户输入的内容。

您可以使用名为 read_int 的函数从用户那里获取选项,函数如下:

int option = read_int("Enter 1 for next round of inputs and 2 to compute with current input:");

并将 returned 值用作:

if ( option == 2 )
{
   // Do the computations
}

您还需要另一个选项来退出程序。获取选项的调用需要是:

char const* prompt = "Enter 1 for next round of inputs.\n"
                     "Enter 2 to compute with current input.\n"
                     "Enter 3 to exit program.\n";
int option = read_int(prompt);

在继续使用您希望读入的数据之前,始终添加代码以检查 scanf 是否成功。


这是您的代码的更新版本。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float read_float(char const* prompt);
int read_int(char const* prompt);
void display_input_values(float a, float b, float c);
void math(float a, float b, float c);

int main() 
{
   while ( 1 )
   {
      float a = read_float("Enter first number: ");
      float b = read_float("Enter second number: ");
      float c = read_float("Enter third number: ");

      display_input_values(a, b, c);


      char const* prompt = "Enter 1 for next round of inputs.\n"
                           "Enter 2 to compute with current input.\n"
                           "Enter 3 to exit program.\n";
      int option = read_int(prompt);
      if ( option == 3 )
      {
         break;
      }

      else if ( option == 2 )
      {
         math(a, b, c);
      }
   }
}


float read_float(char const* prompt)
{
   float num;
   printf("%s", prompt);

   // For flushing the output of printf before scanf.
   fflush(stdout);

   if (scanf("%f", &num ) != 1)
   {
      print("Unable to read number. Exiting.\n");
      exit(1);
   }
   return num;
}

int read_int(char const* prompt)
{
   int num;
   printf("%s", prompt);

   // For flushing the output of printf before scanf.
   fflush(stdout);

   if (scanf("%d", &num ) != 1)
   {
      print("Unable to read number. Exiting.\n");
      exit(1);
   }
   return num;
}

void display_input_values(float a, float b, float c)
{
  printf("You have entered a: %f, b: %f, c:%f\n", a, b, c);
}

void math(float a, float b, float c)
{
   // Do whatever makes sense to you.
}