#include<stdio.h>
#include<string.h>
#include <stdlib.h>
// 传值调用(能更改全局变量的值)
int change1(int a,int b){
int c=a+b;
return c;
}
// 引用调用
int change2(int *a,int *b){
int c=*a+*b;
return c;
}
// C++: & 实参变量所存放存储空间的地址(也就是能更改实参的值 )
// 静态变量 static 存储类指示编译器在程序的生命周期内保持局部变量的存在,
// 而不需要在每次它进入和离开作用域时进行创建和销毁
int fun(){
static int x=1;
x++;
return x;
}
// x--和--x的差别
void f1(int x){
int y=x;
printf("%d\n",y--);
y=x;
printf("%d",--y);
}
// % 两边都是整数
int f2(int x){
int y = x%10;
return y;
}
// 指针函数
int *f3(){
int *p,a[10]={1,2,3,4,5,6,7,8,9,0};
p=a;
return p;
}
// 指针作为形参
int f4(int *p){
int y=*p;
return y;
}
// 回调函数(函数指针)
void f5(void){
printf("hello this is f4");
}
void f6(void (*f5)(void)){
printf("hello this is f5");
}
/**
函数 目的
1 strcpy(s1, s2); 复制字符串 s2 到字符串 s1。
2 strcat(s1, s2); 连接字符串 s2 到字符串 s1 的末尾。
3 strlen(s1); 返回字符串 s1 的长度。
4 strcmp(s1, s2); 如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回小于 0;如果 s1>s2 则返回大于 0。
5 strchr(s1, ch); 返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。
6 strstr(s1, s2); 返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。
*/
// 字符数组和字符串的区别
void f7(){
char s1[6]={'h','e','l','l','o','\0'};
printf("%s\n",s1);
char *s2="hello";
printf("%s\n",s2);
char s3[10];
int i=0;
for(;i<10;i++){
scanf("%c",&s3[i]);
}
printf("%s\n",s3);
printf("\n");
}
void f8(){
char *s4,*p;
s4=(char*)malloc(sizeof(char)*10);
gets(s4);
p=s4;
printf("%s\n",p);
}
// 结构体的相关知识
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book = {"C 语言", "RUNOOB", "编程语言", 123456};
typedef struct
{
char title[50];
char author[50];
char subject[100];
int book_id;
} Books2;
typedef struct Books3
{
char title[50];
char author[50];
char subject[100];
int book_id;
} Books3;
//格式的输出
void f9(){
int a=017; //一个八进制的数
printf("十进制:%d\n",a);
printf("八进制:%#o %o",a,a);
printf("十六进制: %#x %x",a,a);
}
void f10(){
int *p,a,b=9;
p=&a;
scanf("%d",p);
printf("%d\n",*p);
p=NULL; // 防止野指针
p=&b;
printf("%d\n",*p);
}