C++基础知识(三)函数

  • 函数声明
int sum(int, int);
  • VC++中实参从右向左顺序取值
int i=3;
test(i, ++i, i); //3个参数值分别为4, 4, 3
i=3;
test(i, i++, i); //3个参数值分别为3, 3, 3,执行完后i==4
  • 汉诺塔问题
#include "iostream.h"
#include "stdio.h"
#include "stdlib.h"
int hanoi(int n,char a,char b,char c){
    if(n==1)printf("%c -> %c\n", a, c); //final plot move form a to c
    else{
        hanoi(n-1, a, c, b); //n-1 plots move from a to b by c
        printf("%c -> %c\n", a, c); //final plot move form a to c
        hanoi(n-1, b, a, c); //n-1 plots move from b to c by a
    }
    return 0;
}

int main(int argc, char* argv[])
{
    int n;
    cin>>n;
    hanoi(n, 'a', 'b', 'c');
    return 0;
}
  • 变量存储类型和生命周期

动态存储区:函数形参,函数内局部变量。函数调用时分配内存,结束时释放

静态存储区:全局变量,static修饰的局部变量。程序运行开始时分配内存,执行完毕释放。默认初值为0。

auto:动态存储;

static:静态存储;

register:只能修饰局部变量或形参,存放于寄存器中,不能与static共同修饰一个变量;

external:声明是定义在其他源文件中的变量

猜你喜欢

转载自www.cnblogs.com/chuckle/p/8947202.html