C语言基础习题50例(九)41-45

简介: 文章目录习题41习题42习题43习题44习题45

习题41

学习static定义静态变量的用法。

实现思路:

用static声明和未用static声明变量进行对比,即可得出static的作用。

代码如下:

#include <stdio.h>
int main(){
  void func();
  int i;
  for(i = 0;i < 10; i++){
    func();
  } 
    return 0;
}
void func(){
  int i = 0;
  static int num = 0;
  printf("In func:i = %d, num = %d\n", i, num);
  i++;
  num++;
}

打印:

In func:i = 0, num = 0
In func:i = 0, num = 1
In func:i = 0, num = 2
In func:i = 0, num = 3
In func:i = 0, num = 4
In func:i = 0, num = 5
In func:i = 0, num = 6
In func:i = 0, num = 7
In func:i = 0, num = 8
In func:i = 0, num = 9

习题42

学习使用定义自动变量的用法。

实现思路:

自动变量用auto关键字声明,函数中的形参和在函数中定义的变量(包括在复合语句中定义的变量)都属于自动变量,默认不用关键字修饰即为自动变量,可进行对比分析。

代码如下:

#include <stdio.h>
int main(){
  int i, m = 0, n = 0;
  for(i = 0;i < 10; i++){
    printf("In main: m = %d, n = %d. ", m, n);
    {
      auto int n = 10;
      printf("In block: m = %d, n = %d\n", m, n);
    }
    m++;
    n++;
  } 
    return 0;
}


打印:

In main: m = 0, n = 0. In block: m = 0, n = 10
In main: m = 1, n = 1. In block: m = 1, n = 10
In main: m = 2, n = 2. In block: m = 2, n = 10
In main: m = 3, n = 3. In block: m = 3, n = 10
In main: m = 4, n = 4. In block: m = 4, n = 10
In main: m = 5, n = 5. In block: m = 5, n = 10
In main: m = 6, n = 6. In block: m = 6, n = 10
In main: m = 7, n = 7. In block: m = 7, n = 10
In main: m = 8, n = 8. In block: m = 8, n = 10
In main: m = 9, n = 9. In block: m = 9, n = 10


习题43

学习使用static的另一用法。

实现思路:

static在单独的代码块中也可以实现在一次函数执行结束后变量未被销毁、保留当前的值,下次调用函数时即使用保留在内存中的变量值,直到运行期间结束才释放该变量。

代码如下:

#include <stdio.h>
int main(){
  int i, m = 0, n = 0;
  for(i = 0;i < 10; i++){
    printf("In main: m = %d, n = %d. ", m, n);
    {
      static int n = 10;
      printf("In block: m = %d, n = %d\n", m, n);
      n--;
    }
    m++;
    n++;
  } 
    return 0;
}

打印:

In main: m = 0, n = 0. In block: m = 0, n = 10
In main: m = 1, n = 1. In block: m = 1, n = 9
In main: m = 2, n = 2. In block: m = 2, n = 8
In main: m = 3, n = 3. In block: m = 3, n = 7
In main: m = 4, n = 4. In block: m = 4, n = 6
In main: m = 5, n = 5. In block: m = 5, n = 5
In main: m = 6, n = 6. In block: m = 6, n = 4
In main: m = 7, n = 7. In block: m = 7, n = 3
In main: m = 8, n = 8. In block: m = 8, n = 2
In main: m = 9, n = 9. In block: m = 9, n = 1

习题44

学习使用external的用法。

实现思路:

extern来声明外部变量,以扩展外部变量的作用城,从变量的定义处开始,到本程序文件的末尾。

代码如下:

#include <stdio.h>
int main(){
  int max(int a, int b);
  extern A, B;
  printf("Max = %d\n", max(A, B));
  return 0;
}
int max(int a, int b){
  return a > b ? a : b;
}
int A = 12, B = 20;


习题45

学习使用register定义变量的方法。

实现思路:

C语言允许将局部变量的值放在CPU中的寄存器中,需要用时直接从寄存器取出参加运算,不必再到内存中去存取,从而提高执行效率,这种变量叫做寄存器变量,用关键字register声明。

代码如下:

#include <stdio.h>
int main(){
  long fact(int n);
  int i;
  for(i = 1; i <= 10; i++){
    printf("%2d! = %d\n", i, fact(i));
  }
  return 0;
}
long fact(int n){
  register long f = 1;
  int i;
  for(i = 1; i <= n; i++){
    f *= i;
  }
  return f;
}

打印:

 1! = 1
 2! = 2
 3! = 6
 4! = 24
 5! = 120
 6! = 720
 7! = 5040
 8! = 40320
 9! = 362880
10! = 3628800
相关文章
TU^
|
6月前
|
存储 C语言
C语言习题~day35
C语言习题~day35
TU^
31 1
|
4月前
|
机器学习/深度学习 C语言
【C语言篇】递归详细介绍(基础概念习题及汉诺塔等进阶问题)
要保持最小的步数,每一次汉诺塔问题(无论是最初还是递归过程中的),如果此时初始柱盘子数为偶数,我们第一步是把最上面的盘子移动到中转柱,如果为奇数,我们第一步则是将其移动到目标柱。
96 0
【C语言篇】递归详细介绍(基础概念习题及汉诺塔等进阶问题)
TU^
|
6月前
|
编译器 C语言
C语言习题~day31
C语言习题~day31
TU^
24 2
TU^
|
6月前
|
算法 程序员 C语言
C语言习题~day36
C语言习题~day36
TU^
43 1
TU^
|
6月前
|
存储 C语言
C语言习题~day34
C语言习题~day34
TU^
34 1
TU^
|
6月前
|
算法 C语言
C语言习题~day33
C语言习题~day33
TU^
30 1
TU^
|
6月前
|
C语言
C语言习题~day32
C语言习题~day32
TU^
19 1
TU^
|
6月前
|
C语言
C语言习题~day30
C语言习题~day30
TU^
24 1
TU^
|
6月前
|
自然语言处理 C语言 C++
C语言习题~day29
C语言习题~day29
TU^
25 1
TU^
|
6月前
|
存储 C语言
C语言习题~day28
C语言习题~day28
TU^
21 1