c - C 中的点或 "reset"变量

标签 c variables

如果我有这样的事情:

printf("\nEnter 2 numbers: \n");
scanf(" %d %d", &a, &b);
    add (a,b)

    int a,b;

{
printf ("%d", a+b);
}

然后想要再次运行该 block ,但使用“无”的新变量,就像输入第一个 printf 语句时一样。有什么建议吗?

最佳答案

首先避免使用K&R C 语法

/* Your function 
add (a,b)
int a,b;
{
   printf ("Sum = %d\n", a+b);
}
*/

/* Use following style*/
void add (int a,int b)
{
  printf ("Sum = %d\n", a+b);
}

int main()
{

int i,a,b; // Declare variables
int n=5; // Call it say n=5 times

for(i=0;i<n;i++)  //Use a for loop to iterate for n times
{ 
  printf("\nEnter 2 numbers: \n");
  if(scanf(" %d %d", &a, &b)==2) // with 2 new inputs
    add(a,b); //Call your add function
 }
}

关于c - C 中的点或 "reset"变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18687262/

相关文章:

c - 部分正弦数据拟合代码 C

java - 为什么字节总和本身不是字节类型?

php - 将 mysql 数组结果分配给 php 变量不起作用

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

c - 在 Lua 中与嵌套函数一起使用 C 变量

c - 在编译时初始化数组

c - 生产者/消费者 : 1 producer, 多个消费者 - 1 个信号量、1 个互斥量、1 个条件变量

c - c中数组指针的解释?

C:如何访问存储在空指针(void *)中的函数指针?

java - 与 while 循环相关的变量声明的位置