一、基礎語法和數據類型
1、變數和常量的定義和使用
#include<stdio.h>
int main()
{
int a=10; //定義一個整型變數a並初始化為10
const int b=20; //定義一個常量b(值不能改變)並初始化為20
printf("a=%d\n",a); //輸出a的值
printf("b=%d\n",b); //輸出b的值
a=30; //修改a的值為30
printf("a=%d\n",a); //輸出a新的值
return 0; //程序結束
}
2、運算符號的使用
#include<stdio.h>
int main()
{
int a=10,b=3;
printf("a+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%%b=%d\n",a%b); //取模運算(餘數)
printf("a++=%d\n",a++); //後置自增
printf("a=%d\n",a); //輸出a的新值
printf("--a=%d\n",--a); //前置自減
printf("a=%d\n",a); //輸出a的新值
return 0;
}
3、條件判斷語句的使用
#include<stdio.h>
int main()
{
int a=10,b=20;
if(a>b) //如果a>b成立
printf("a>b\n"); //輸出a>b
else //否則
printf("a<=b\n"); //輸出a<=b
return 0;
}
4、循環語句的使用
#include<stdio.h>
int main()
{
int i=0;
while(i<10) //只要i小於10
{
printf("%d ",i); //輸出i的值
i++; //i自增1
}
return 0; //程序結束
}
二、數組和字元串
1、數組的定義和使用
#include<stdio.h>
int main()
{
int a[5]={1,2,3,4,5}; //定義並初始化一個整型數組
for(int i=0;i<5;i++) //依次輸出數組中的每個元素
printf("%d ",a[i]);
return 0;
}
2、字元串的使用
#include<stdio.h>
int main()
{
char str[100]; //定義一個字元數組
printf("please enter a string:\n");
gets(str); //輸入字元串
printf("your string is: %s\n",str); //輸出字元串
int len=0; //計算字元串長度
while(str[len]!='\0')
len++;
printf("string length is: %d\n",len);
int cnt=0; //計算空格數量
for(int i=0;str[i]!='\0';i++)
if(str[i]==' ')
cnt++;
printf("space count is: %d\n",cnt);
return 0;
}
3、數組和字元串的轉換
#include<stdio.h>
#include<string.h>
int main()
{
char str1[]="hello", str2[]="world";
char str3[100];
strcpy(str3,str1); //將str1複製到str3中
strcat(str3,str2); //將str2連接到str3的末尾
printf("str3=%s\n",str3); //輸出str3
return 0;
}
三、函數和指針
1、函數的定義和使用
#include<stdio.h>
void hello() //定義一個無參數無返回值的函數
{
printf("hello, world!\n");
}
int sum(int a, int b) //定義一個帶參數有返回值的函數
{
return a+b;
}
int main()
{
hello(); //調用hello函數
printf("sum=%d\n",sum(1,2)); //調用sum函數
return 0;
}
2、指針的定義和使用
#include<stdio.h>
int main()
{
int a=10, *p=&a; //定義一個整型變數a和一個指向a的指針p
printf("a=%d, &a=%p\n",a,&a); //輸出a和a的地址
printf("*p=%d, p=%p, &p=%p\n",*p,p,&p); //輸出*p、p、p的地址
*p=20; //修改p指向的變數a的值為20
printf("a=%d, *p=%d\n",a,*p); //輸出a和*p的值
return 0;
}
3、動態內存分配
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n=5;
int *p=NULL;
p=(int*)malloc(n*sizeof(int)); //動態分配內存
if(p==NULL) //檢查內存是否分配成功
printf("memory allocation failed\n");
else
{
for(int i=0;i<n;i++) //輸入數據到動態分配的數組中
scanf("%d",p+i);
for(int i=0;i<n;i++) //輸出動態分配的數組中的數據
printf("%d ",*(p+i));
}
free(p); //釋放分配的內存
return 0;
}
四、文件操作
1、文件的讀取和寫入
#include<stdio.h>
int main()
{
FILE *fp=NULL;
char str[100];
fp=fopen("test.txt","w"); //打開一個文件,以便寫入數據
if(fp==NULL) //打開失敗
printf("file open failed\n");
else
{
printf("please input a string:\n");
gets(str); //輸入字元串
fprintf(fp,"%s\n",str); //將字元串寫入文件中
fclose(fp); //關閉文件
}
fp=fopen("test.txt","r"); //打開同一文件,以便讀取數據
if(fp==NULL) //打開失敗
printf("file open failed\n");
else
{
fgets(str,100,fp); //從文件中讀取一行
printf("string is: %s",str); //輸出讀到的字元串
fclose(fp); //關閉文件
}
return 0;
}
2、二進位文件的讀取和寫入
#include<stdio.h>
int main()
{
int a[10]={0,1,2,3,4,5,6,7,8,9};
FILE *fp=NULL;
fp=fopen("test.bin","wb"); //以二進位方式創建可寫文件
if(fp==NULL) //打開失敗
printf("file open failed\n");
else
{
fwrite(a,sizeof(int),10,fp); //將數組a寫入文件中
fclose(fp); //關閉文件
}
fp=fopen("test.bin","rb"); //以二進位方式打開同一文件,以便讀取數據
if(fp==NULL) //打開失敗
printf("file open failed\n");
else
{
int b[10];
fread(b,sizeof(int),10,fp); //從文件中讀取數據到數組b中
for(int i=0;i<10;i++) //輸出讀到的數組
printf("%d ",b[i]);
fclose(fp); //關閉文件
}
return 0;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/231452.html
微信掃一掃
支付寶掃一掃