C语言及程序设计提高例程-11 全局变量和局部变量

贺老师教学链接  C语言及程序设计提高 本课讲解


C语言见不怪,变量定义放在外

#include <stdio.h>
void fun1();
void fun2();
int x=1;
int main ()
{
    x++;
    printf("%d\n", x);
    fun1();
    printf("%d\n", x);
    fun2();
    printf("%d\n", x);
    return 0;
}


void fun1()
{
    x+=3;
}

void fun2()
{
    x+=7;
}


类比:函数声明的位置
(1)
#include <stdio.h>
int gcd(int n1, int n2);
int lcm(int n1, int n2);
int main()
{
  int num1 = 56 ,num2 =80;
  int iGcd, iLcm; 
  iGcd=gcd(num1, num2);
  iLcm=lcm(num1,num2);
  printf("%d %d\n",iGcd, iLcm);
  return 0;
}
int lcm(int n1, int n2)
{
  return n1*n2/gcd(n1, n2);
};
int gcd(int n1, int n2)
{
  int r;
  while(n2!=0)
  {... }
  return n1;
}


(2)
#include <stdio.h>
int main()
{
  int num1 = 56 ,num2 =80;
  int iGcd, iLcm; 
  iGcd=gcd(num1, num2);
  iLcm=lcm(num1,num2);
  printf("%d %d\n",iGcd, iLcm);
  return 0;
}
int gcd(int n1, int n2);
int lcm(int n1, int n2);
int lcm(int n1, int n2)
{
  return n1*n2/gcd(n1, n2);
};
int gcd(int n1, int n2)
{
  int r;
  while(n2!=0)
  {... }
  return n1;
}

(3)
#include <stdio.h>
int main()
{
  int gcd(int n1, int n2);
  int lcm(int n1, int n2);
  int num1 = 56 ,num2 =80;
  int iGcd, iLcm; 
  iGcd=gcd(num1, num2);
  iLcm=lcm(num1,num2);
  printf("%d %d\n",iGcd, iLcm);
   return 0;
}
int lcm(int n1, int n2)
{
  return n1*n2/gcd(n1, n2);
};
int gcd(int n1, int n2)
{
  int r;
  while(n2!=0)
  {... }
  return n1;
}



你可能感兴趣的:(C语言及程序设计提高例程-11 全局变量和局部变量)