C语言入门笔记
C语言入门(1)
目录
#include<stdio.h>
#if 0
// 绘制形状飞行器
void f(){
printf(" *\n");
printf(" * *\n");
printf(" * *\n");
}
int main(int argc,char **argv){
f();
return 0;
}
#endif
#if 0
/*宏定义:#define eg:#define PI 3.1415 则PI是3.1415的名字,预处理会将代码中的PI用3.1415替换。*/
#define PI 3.1415
int main ()
{
float radius = 2.5;
printf("area = %f",PI*radius*radius);
}
#elif 0
int main ()
{
printf("hello else \n");
printf("hello \t \n");
printf("hello \a \n");
float radius = 12;
float area = 3.14*radius*radius;
printf("area=%f\n", area);
printf("半径是%3.1f的圆的面积是:%f\n", radius,area); //%3.1f 占据三个宽度 小数点后一位
radius = 2.5;
area = 3.14*radius*radius;
printf("半径是%4.1f的圆的面积是:%f\n", radius,area);
// printf输出格式:%开头表示格式符,表示格式串后的输出项按照什么样的数据格式进行输出,有几个输出项,有几个输出项,格式串中就有几个格式符,常见的格式符有以下几种:
// %c:表示对应输出项是字符char
// %d:表示对应输出项是整数类型int
// %f:表示对应输出项是单精度实数类型float
// %lf:表示对应输出项是双精度实数类型double
// %s:表示对应输出项是一个字符串,即一串字符
float f; char ch = 'char';
int a = 1; double dou = 3.1415922;
printf("f=%f,ch=%c,a=%d\n",f,ch,a);
printf("dou=%lf\n", dou);
printf("%s\n","hello world");
return 0;
}
/* sizeof() 类型变量占据内存块大小,以字节为单位 一字节八位 */
#elif 0
int main ()
{
int a;
float b;
char c;
double d;
printf("sizeof(a)=%d\n",sizeof(a));
printf("sizeof(int)=%d\n",sizeof(int));
printf("sizeof(b)=%d\n",sizeof(b));
printf("sizeof(float)=%d\n",sizeof(float));
printf("sizeof(c)=%d\n",sizeof(c));
printf("sizeof(char)=%d\n",sizeof(char));
printf("sizeof(d)=%d\n",sizeof(d));
printf("sizeof(double)=%d\n",sizeof(double));
return 0;
}
#elif 0
#include<limits.h> /* for CHAR_MIN CHAR_MAX .etc */
#include<float.h> /* for FLT_DIG DLB_DIG .etc */
/* limits.h 包含了各种类型值的最大值 最小值 或实数精度对的宏含义*/
int main () {
/* 符号 分配空间 最小 最大 */
printf("char %d bytes %d to %d \n",sizeof(char ), CHAR_MIN, CHAR_MAX );
printf("unsigned char %d bytes %d to %d \n",sizeof(unsigned char ), 0 , UCHAR_MAX );
printf("short %d bytes %hi to %hi \n",sizeof(short ), SHRT_MIN, SHRT_MAX );
printf("unsigned short %d bytes %hu to %hu \n",sizeof(unsigned short), 0 , USHRT_MAX );
printf("int %d bytes %i to %i \n",sizeof(int ), INT_MIN , INT_MAX );
printf("unsigned int %d bytes %u to %u \n",sizeof(unsigned int ), 0 , UINT_MAX );
printf("long %d bytes %li to %li \n",sizeof(long ), LONG_MIN, LONG_MAX );
printf("unsigned long %d bytes %lu to %lu \n",sizeof(unsigned long ), 0 , ULONG_MAX );
printf("float %d bytes %e to %e \n",sizeof(float ), FLT_MIN , FLT_MAX );
printf("double %d bytes %e to %e \n",sizeof(double ), DBL_MIN , DBL_MAX );
printf("precision of double %d digts \n", DBL_DIG );
printf("precision of float %d digts \n", FLT_DIG );
return 0
}
#elif 0
/* scanf 输入数据到变量对应的内存块 & 取地址*/
#define PI 3.1415
int main()
{
float radius;
int a,b;
scanf("%f",&radius);
printf("%f",radius);
scanf("%d,%d",&a,&b);
printf("%d,%d",a,b);
return 0;
}
#elif 0
/*运算符 demo 01 */
int main ()
{
int a = 10; int b = 3;
printf("a=%d,b=%d\n",a,b);
printf("a + b = %d\n",a+b);
printf("a - b = %d\n",a-b);
printf("a / b = %d\n",a/b);
printf("a x b = %d\n",a*b);
printf("a %% b = %d\n",a%b);
a++;
printf("a=%d\n",a);
a--;
a--;
printf("a=%d\n",a);
}
#elif 0
#include<math.h>
/*运算符 求一元二次方程的跟 02 */
int main ()
{
double a,b,c,delta,x1,x2;
scanf("%lf %lf %lf",&a,&b,&c);
delta=b*b-4*a*c;
x1=(-b + sqrt(delta))/(2*a);
x2=(-b - sqrt(delta))/(2*a);
printf("方程%lf x^2 + %lf x + %lf = 0 的跟为 %lf, %lf\n",a,b,c,x1,x2);
return 0;
}
#elif 0
/* 比较运算符 逻辑运算符*/
int main ()
{
/* 比较运算 */
int a = 2;int b = 0; int c = 5;
printf("a=%d,b=%d,c=%d\n",a,b,c);
printf("%d>%d:%d\n",a,b,a>b);
printf("%d<%d:%d\n",a,b,a<b);
printf("%d<=%d:%d\n",a,b,a<=b);
printf("%d>=%d:%d\n",a,b,a>=b);
printf("%d!=%d:%d\n",a,b,a!=b);
/* 逻辑运算 */
printf("a=%d,b=%d,c=%d\n",a,b,c);
printf("!a = %d \n",!a);
printf("!b = %d \n",!b);
printf("%d || %d : %d \n",a,b,a||b);
printf("%d && %d : %d \n",a,b,a&&b);
printf("%d || %d : %d \n",a,c,a||c);
printf("%d && %d : %d \n",a,c,a&&c);
printf("%d || %d : %d \n",b,c,b||c);
printf("%d && %d : %d \n",b,c,b&&c);
return 0;
}
#elif 0
#include<stdlib.h>
int main ()
{
/*位运算符 */
int a = 37; int b = 12;
char s[10]; /*10个char(字符)类型的数组 */
itoa(b,s,2);
printf("a=%d,二进制是:%10s\n",a,s);
itoa(b,s,2);
printf("b=%d,二进制是:%10s\n",b,s); /* 10s表示右对齐 -10s表示左对齐 */
itoa(a&b,s,2); /* & 按位与 同真为真 */
printf("a&b=%d,二进制是:%9s\n",a&b,s);
itoa(a|b,s,2); /* | 按位或 一真为真 */
printf("a|b=%d,二进制是:%9s\n",a|b,s);
itoa(a^b,s,2); /* ^ 按位异或 相同为0 不同为1 */
printf("a^b=%d,二进制是:%9s\n",a^b,s);
itoa(~a,s,2); /* ~ 按位取反 */
printf("~a=%d,二进制是:%9s\n",~a,s);
return 0
}
#elif 1
int main ()
{ /*复合运算符 */
int a = 37; int b = 12;
printf("a=%d,b=%d,\n",a,b);
a+=b; /* a=a+b */
printf("a+=b:%d,\n",a);
a%=b; /* a=a%b */
printf("a%%=b:%d,\n",a);
}
#endif
C语言入门(2)
目录
8. 控制语句
9. 数组
10.函数
11.指针
#include<stdio.h>
#if 0
int main ()
{
/* if条件语句 */
float score;
scanf("%f",&score);
if(score>=60)
{
printf("你的分数是:%4.1f\n",score);
printf("你通过了测试");
}
else
{
printf("你的分数是:%4.1f\n",score);
printf("你没有通过测试");
}
return 0;
}
#elif 0
int main ()
{
/* switch条件语句 计算器*/
float a,b;
char operate;
scanf("%f %c %f",&a,&operate,&b);
switch (operate){
case '+': printf("%f %c %f 的结果是:%f \n",a,operate,b,a+b); break;
case '-': printf("%f %c %f 的结果是:%f \n",a,operate,b,a-b); break;
case '*': printf("%f %c %f 的结果是:%f \n",a,operate,b,a*b); break;
case '/': printf("%f %c %f 的结果是:%f \n",a,operate,b,a/b); break;
default: printf("输入运算符错误:\n"); break;
}
return 0;
}
#elif 0
int main ()
{
/* while循环 1到100的和*/
int sum = 0, i = 0;
while(i<=100){
sum += i; /*sum = sum + i */
i++;
}
printf("1到100只和是:%d\n",sum);
i = 0;
sum = 0;
do{
sum += i; /*sum = sum + i */
i++;
}while(i<=100);
printf("1到100的和是:%d\n",sum);
return 0;
}
#elif 0
int main ()
{
/* 语句嵌套 */
float money,earn = 0,debt = 0;
while(1){
printf("请输入金额:\n");
scanf("%f",&money);
if(money>0){
earn+=money;
}else if(money<0){
debt+=money;
}else{
break;
}
}
printf("earn: %f , debt: %f ./n",earn,debt);
return 0;
}
#elif 0
int main ()
{
/* for循环 1到100的和*/
int sum = 0;
for(int i=1;i<=100;i++)
{
sum+=i;
}
printf("1到100的和是:%d\n",sum);
return 0;
}
#elif 0
int main ()
{
/* for循环 判断一个数是否是质数*/
int a;
scanf("%d",&a);
/* demo1 */
/*
for(int i = 2; i<a ; i++){
if(a%i==0){
printf("%d不是质数",a);
return 0;
}
}
printf("%d是质数",a);
*/
/* demo2 */
int flag = 0;
for(int i = 2; i<a ; i++){
if(a%i==0){
flag = 1;
break;
}
}
if(flag == 1){
printf("%d不是质数",a);
}
else{
printf("%d是质数",a);
}
return 0;
}
#elif 0
int main ()
{
/* 键盘输入学生成绩 计算平均数*/
float scoreArr[100], score, sum;
int n = 0;
while(1){
scanf("%f",&score);
if(score<0){
break;
}
scoreArr[n] = score;
sum+=score;
n++;
}
for(int i=0;i<n;i++){
printf("%f\t",scoreArr[i]);
}
printf("\n");
printf("平均分数是:%5.2f",sum/n);
return 0;
}
#elif 0
/*函数定义 */
void fun (int n){ /* 形式参数 */
for(int i = 0;i<n;i++){
printf("hello\n");
printf("第%d次\n",i);
}
}
int main(){
fun(5); /* 函数调用 实际参数 */
return 0;
}
#elif 0
/* n的阶乘 */
int fun (int n){
int result = 1;
for(int i = 1;i<=n;i++){
result*=i;
printf("%d\n",result);
}
return result;
}
int main(){
printf("%d\n",fun(5));
return 0;
}
#elif 0
/*递归函数 n的阶乘 n=1 n!=1 n>1 n!=n(n-1)! */
int fun (int n){
if(n == 1){
return 1;
}else{
return n*fun(n-1);
}
}
int fic(int n)
{
if(n<=2){
return 1;
}else{
return fic(n-1)+fic(n-2);
}
}
/*递归函数 斐波那契数列 F(1)=1,F(2)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 3,n ∈ N*) */
int main(){
int n = 5;
printf("%d的阶乘计算\n",n);
printf("%d\n",fun(n));
printf("%d的斐波那契数列计算\n",n);
for(int i = 1;i<=n;i++){
printf("%d\n",fic(i));
}
return 0;
}
#elif 0
/*指针 变量*/
int main()
{
/*指针指向变量 */
int a=1,b=2;
int *p=&a;
printf("a:%d,b:%d\n",a,b);
printf("a的地址是:%p\n",&a);
printf("p的取值是:%p\n",p);
printf("a的变量值是:%d\n",a);
printf("p的指向值是:%d\n",*p);
b=*p; /* *p解引用指向对应变量的值 */
printf("a:%d,b:%d\n",a,b);
return 0;
}
#elif 0
/*指针 数组*/
int main()
{
/*指针指向数组地址即数组第一个元素的地址 */
int arr[5]={3,5,7,1,10}, b;
int *q=arr; /* 等价于int *q=arr[0]; */
printf("arr的地址为:%p\n",arr);
printf("arr[0]的地址为:%p\n",&arr[0]);
printf("q的取值为:%p\n",q);
/*
*q解引用指向对应变量的值
则 *q==arr[0] 则 *[q+2] = arr[2]
arr[i] == *(arr+i) = *(q+i) = q[i]
*/
printf("arr[2]的值为:%d\n",arr[2]);
printf("q[2]的取值为:%d\n",q[2]); /* q[2]==*(q+2) */
b=arr[1];
for(int *p=arr;p<arr+5;p++){
printf("%d\t",*p);
}
return 0;
}
#elif 0
/*指针做参数 两个变量交换 */
void swap1 (int a, int b){
int temp = a;
a=b;
b=temp;
}
void swap2 (int *a, int *b){
int temp = *a;
*a=*b;
*b=temp;
}
int main(){
int x=3,y=4;
printf("x=%d,y=%d\n",x,y);
printf("……执行交换函数swap1……\n");
swap1(x,y);
printf("x=%d,y=%d\n",x,y);
printf("……执行交换函数swap2……\n");
swap2(&x,&y);
printf("x=%d,y=%d\n",x,y);
}
#elif 0
/*数组名作为函数的参数
数组名就是数组第一个元素的地址,因此传递的实际是指针。
void print(char arr,int n)=== void print(char *a,int n)
*/
void print(char arr[],int n)
{
for(int i=0;i<n;i++){
printf("%c\n",arr[i]);
}
}
int main()
{
char crr[]={'h','e','l','l','o'};
print(crr,5);
return 0;
}
#elif 0
#include <string.h>
int main ()
{
char arr[] = {'h','e','l','l','o'};
char str[] = {'h','e','l','l','o','\0'};
char* p="hello";
printf("str的长度为:%d,p的长度为:%d\n",strlen(str),strlen(p));
strcpy(str,"wang");
printf("%s\n",str);
printf("%s\n",p);
}
/*实现自己的 strlen */
int strlen(char *s){
char *p=s;
while(*p!='\0'){
p++;
}
return p-s;
}
C语言入门(3)完
目录
12.结构
13.动态内存分配
14.文件读写
#if 0
#include <string.h>
/* 定义结构学生类型 */
struct Student
{
int age;
float score;
char name[];
};
int main()
{
/*结构变量 */
struct Student stu;
stu.age=12;
stu.score=78.5;
strcpy(stu.name,"liming");
printf("%d\t%f\t%s\t\n",stu.age,stu.score,stu.name);
/*结构指针 */
struct Student *p=0;
p=&stu;
printf("%d\t%f\t%s\t\n",(*p).age,(*p).score,(*p).name);
/*简访问 用 -> */
p->age=18;
p->score=67.2;
strcpy(p->name,"baotao");
printf("%d\t%f\t%s\t\n",p->age,(*p).score,(*p).name);
return 0;
}
#elif 0
#include <string.h>
/* 定义结构学生类型 */
struct Student
{
int age;
float score;
char name[];
};
/*传递参数 错误写法 */
void Scanf (struct Student stu)
{
printf("请输入学生姓名 年龄 分数\n");
scanf("%s",stu.name);
scanf("%d",&stu.age);
scanf("%f",&stu.score);
printf("%d\t%f\t%s\t\n",stu.age,stu.score,stu.name);
}
/*传递参数 地址 */
void ScanfAddress (struct Student *stu)
{
printf("请输入学生姓名 年龄 分数\n");
scanf("%s",stu->name);
scanf("%d",&(stu->age));
scanf("%f",&(stu->score));
printf("%d\t%f\t%s\t\n",stu->age,stu->score,stu->name);
}
int main()
{
if(0)
{
struct Student stu;
Scanf(stu);
printf("%d\t%f\t%s\t\n",stu.age,stu.score,stu.name);/* 函数内部的值没有传递给外面的变量 */
ScanfAddress(&stu);
printf("%d\t%f\t%s\t\n",stu.age,stu.score,stu.name);
}else{
struct Student stuArr[100];
struct Student *p=stuArr;
struct Student *q=0;
int n = 0;
while(1){
ScanfAddress(p);
printf("输入的数据为:序号:%d %d\t%f\t%s\t\n",n,p->age,p->score,p->name);
if(p->age<0) break;
n++;
p++;
}
printf("输入完成,开始遍历结果\n");
q=p;
for(p=stuArr;p<q;p++){
printf("%d\t%f\t%s\t\n",p->age,p->score,p->name);
}
}
return 0;
}
#elif 0
#include<sys/malloc.h> /*mac下 */
#include<stdlib.h> /*mac下 */
int main()
{
int *p=(int *)malloc(3*sizeof(int));
*p=20;
*(p+1)=30;
p[2]=40;
/* p[3]=50; 非法空间访问 */
for(int i=0;i<3;i++){
printf("%d\t",p[i]);
}
printf("\n");
/*重新分配 */
p=(int *)realloc(p,5*sizeof(int));
if(p == 0){
printf("扩充内存失败\n");
return 2;
}else{
printf("扩充内存成功\n");
}
printf("再次访问\n");
p[3]=50;
for(int i=0;i<5;i++){
printf("%d\t",p[i]);
}
/*释放内存 */
free(p);
return 0;
}
#elif 0
#define N 5
#include<stdio.h>
/* 定义结构学生类型 */
struct Student{
int age;
float score;
char name[N];
};
/*传递参数 错误写法 */
void Scanf (struct Student stu){
printf("请输入学生姓名 年龄 分数\n");
scanf("%s",stu.name);
scanf("%d",&stu.age);
scanf("%f",&stu.score);
printf("%d\t%f\t%s\t\n",stu.age,stu.score,stu.name);
}
/*传递参数 地址 */
void ScanfAddress (struct Student *stu){
printf("请输入学生姓名 年龄 分数\n");
scanf("%s",stu->name);
scanf("%d",&(stu->age));
scanf("%f",&(stu->score));
printf("%d\t%f\t%s\t\n",stu->age,stu->score,stu->name);
}
int main(){
if(0)
{
struct Student stu;
Scanf(stu);
printf("%d\t%f\t%s\t\n",stu.age,stu.score,stu.name);
/* 错误 函数内部的值没有传递给外面的变量 */
ScanfAddress(&stu);
printf("%d\t%f\t%s\t\n",stu.age,stu.score,stu.name);
}else
{
struct Student stuArr[100];
struct Student *p=stuArr;
struct Student *q=0;
int n = 0;
while(1){
ScanfAddress(p);
printf("输入的数据为:序号:%d %d\t%f\t%s\t\n",n,p->age,p->score,p->name);
if(p->age<0) break;
n++;
p++;
}
printf("输入完成,开始遍历结果\n");
q=p;
for(p=stuArr;p<q;p++){
printf("%d\t%f\t%s\t\n",p->age,p->score,p->name);
}
}
return 0;
}
#elif 0
#include <string.h>
#include <stdlib.h>
#include<sys/malloc.h> /*mac下 */
/* 动态内存分配 学生成绩表 */
struct Student {
char name[10];
int age;
float score;
};
void Scanf(struct Student *p){
printf("请输入学生姓名 年龄 分数\n");
scanf("%s",p->name);
scanf("%d",&(p->age));
scanf("%f",&(p->score));
printf("学生姓名:%s\t,年龄:%d\t,分数:%f\t\n",p->name,p->age,p->score);
};
int main()
{
struct Student *stuArr=0;
struct Student *p=0;
struct Student *q=0;
int n=0,cap;
printf("请输入要分配的空间\n");
scanf("%d",&cap);
stuArr = (struct Student *)malloc(cap*sizeof(struct Student));
if(!stuArr){
printf("分配内存失败\n");
return 1;
}
p=stuArr;
while(1){
Scanf(p);
if(p->age<0)break;
p++;
n++;
if(n==cap){
cap*=2;
printf("分配内存已满,重新分配二倍内存:%d\n",cap);
stuArr = (struct Student *)realloc(stuArr,(cap)*sizeof(struct Student));
if(!stuArr){
printf("新增内存失败\n");
return 2;
}
printf("分配内存成功,请继续输入:\n");
p=stuArr+n; /* !!!重新指向新地址 */
}
}
printf("输入完成,开始遍历\n");
q=stuArr+n; /*p自增后将最后地址赋给q */
for(p=stuArr;p<q;p++){
printf("学生姓名:%s\t,年龄:%d\t,分数:%f\t\n",p->name,p->age,p->score);
}
free(stuArr);
return 0;
}
#elif 0
/* 文件读写 demo1*/
int main()
{
/* 打开或者创建文件 */
FILE *fp= fopen("test.text","w");
if(fp == 0){
printf("打开失败\n");
return 1;
}
/*写入数据 */
printf("开始写入数据\n");
fprintf(fp,"liping 78.9\n");
fprintf(fp,"%s %f","liming",99.7);
fclose(fp);
printf("写入数据完成,开始读取\n");
/* 读文件 */
char name[10];
float score;
fp= fopen("test.text","r");
fscanf(fp,"%s %f",name,&score);
printf("%s %f\n",name,score);
fscanf(fp,"%s %f",name,&score);
printf("%s %f\n",name,score);
fclose(fp);
return 0;
}
#elif 0
/* 文件读写 demo2*/
int main()
{
struct Student{
char name[20];
float score;
};
struct Student s;
FILE *fr,*fw;
fr= fopen("test.text","r");
fw= fopen("test2.text","w");
while(fscanf(fr,"%s %f",s.name,&s.score) != EOF){
fprintf(fw,"%s %f",s.name,s.score);
fprintf(fw,"\n");
printf("%s %f\n",s.name,s.score);
}
fclose(fr);
fclose(fw);
return 0;
}
#elif 0
/* 文件读写 单个字符读取写入 demo3*/
int main()
{
char ch;
FILE *fp;
printf("请输入要写入的字符:\n");
fp=fopen("get_c.text","w");
/* getchar() 从键盘山读取字符 */
while((ch = getchar()) != EOF){
putc(ch,fp);
};
fclose(fp);
fp=fopen("get_c.text","r");
while((ch = getc(fp)) != EOF){
printf("%c",ch);
};
fclose(fp);
return 0;
}
#elif 0
/* 文件读写 行字符读取写入 demo4*/
int main()
{
char str[100];
FILE *fp;
printf("请输入要写入的字符:\n");
fp=fopen("fget_c.text","w");
/* gets() 从键盘山读取行 */
while( gets(str) != NULL){
fputs(str,fp);
};
fclose(fp);
fp=fopen("get_c.text","r");
while(fgets(str,100,fp) != NULL){
printf("%s",str);
};
fclose(fp);
return 0;
}
#elif 0
/* 文件读写 demo5 二进制读写*/
int main()
{
struct Student{
char name[20];
int age;
float score;
};
struct Student s;
FILE *fp;
printf("请输入学生姓名 年龄 分数\n");
fp= fopen("student.bin","wb");
while(scanf("%s %d %f",s.name,&s.age,&s.score) != EOF){
fwrite(&s,sizeof(struct Student),1, fp);
}
fclose(fp);
fp= fopen("student.bin","rb");
while(fread(&s,sizeof(struct Student),1, fp) == 1){
printf("%s %d %f\n",s.name,s.age,s.score);
}
fclose(fp);
return 0;
}
#elif 1
#include <string.h>
/* 文件读写 demo6 指针定位*/
int main()
{
struct Student{
char name[20];
int age;
float score;
};
FILE *fp;
long filesize;
struct Student s,s2;
strcpy(s.name,"lining");
s.age=12;
s.score=88.7;
/* wb+ 既可以读又可以写 */
fp=fopen("stu_test.text","wb+");
fwrite(&s,sizeof(struct Student),1,fp);
filesize=ftell(fp);
printf("文件当前位置是:%ld\n",filesize);
/* !!!写完之后必须重新定位到开头 才可以读取到数据 */
/* 重新定位指针到开头 */
printf("定位开头\n");
fseek(fp,0,SEEK_SET);
filesize=ftell(fp);
printf("文件当前位置是:%ld\n",filesize);
fread(&s2,sizeof(struct Student),1,fp);
printf("读取结果:%s %d %f%\n",s2.name,s2.age,s2.score);
return 0;
}
#endif