函数与指针
#include <stdio.h>
double min(double x, double y);
void chline(char ch[], int i, int j);
void to_binary(int i);
long fibonacci(int n);
void swap(int* a, int* b);
int main()
{
printf("计算最小值(2.0,2.01) = %5.2f
", min(2.0, 2.01));
char ss[50] = "beautiful";
printf("指定输出 beautiful 1,3 = ");
chline(ss, 1, 3);
printf("
10进制转2进制: 100 =");
to_binary(100);
printf("
打印斐波那契数列第n个:");
long fb = fibonacci(5);
printf("fb = %ld
", fb);
printf("通过指针交换
");
int a = 1, b = 2;
printf("before, a=%d ,b=%d
", a, b);
swap(&a, &b);
printf("after , a=%d ,b=%d
", a, b);
return 0;
}
double min(double x, double y)
{
return x < y ? x : y;
}
void chline(char ch[], int i, int j)
{
for (; i <= j; i++) {
printf("%c", ch[i]);
}
printf("
");
}
void to_binary(int i)
{
int mod = i % 2;
int shang = i / 2;
if (1 == shang)printf("%d", shang);
else if (shang > 1)to_binary(shang);
printf("%d", mod);
}
long fibonacci(int n)
{
if (n > 2)
return fibonacci(n - 1) + fibonacci(n - 2);
return 1;
}
void swap(int * a, int * b)
{
int* temp;
temp = *a;
*a = *b;
*b = temp;
}
数组与指针
#include <stdio.h>
printf_array(int array[][3], int row, int col);
printf_vla(int rows, int cols, int ar[rows][cols]); // 变长数组VLA ,C99支持
int main()
{
int days[] = { 1,3,10 };
for (int i = 0; i < sizeof days / sizeof days[0]; i++)
printf("index : %d , value : %d
", i, days[i]);
int matrix1[2][3] = {
{1,2,3},
{1,4,3}
};
// 二维数组打印
printf_array(matrix1, 2, 3);
return 0;
}
printf_array(int array[][3], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf("%d,", *(*(array + i) + j));
}
printf("
");
}
}