栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言基础语法【2】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言基础语法【2】

目录

1、continue、break、goto关键字、数组、字符串数组处理函数

2、二维数组、指针变量、指针与数组

3、数组与指针的关系


1、continue、break、goto关键字、数组、字符串数组处理函数
 //1
 #include 

int main()
{
        int ch;
        while((ch=getchar())!='n')
        {
                if(ch=='C')
                {
                continue;
                }
        putchar(ch);
        }
        putchar('n');
        return 0;
}

 [wlp@192 class_1]$ gcc test5.c && ./a.out
l love fishC
l love fish
//2
#include 

int main()
{
        int i=5;
        while(i++)
        {
                if(i>10)
                {
                        goto a;

                }
        }
a:      printf("Here,i=%dn",i);
        return 0;
}
[wlp@192 class_1]$ vi test5.c

//输入并求出10位同学的平均分
#include 
#define NUM 10
int main()
{
        int s[NUM];//定义一个数组 []里面可以省略
        int i,sum=0;

        for(i=0;i<10;i++)
        {
                printf("请输入第%i位同学的成绩:",i+1);//%i与%d效果相同
                scanf("%d",&s[i]);//向数组里存数据
                sum+=s[i];
        }
        printf("评均分是%.2fn",(double)sum/NUM);

        return 0;
}
//创建输入自定义长度的数组
#include 
int main()
{
        int n,i;
        printf("输入数组的个数:");
        scanf("%d",&n);

        char a[n+1];//+1是为了增加

        printf("开始输入字符:");
        getchar();//接受回车
        for(i=0;i
#include 

int main()
{
        char str[]="I love you";
        printf("sizeof str=%dn",sizeof(str));
        printf("strlen str=%un",strlen(str));//unsigned型的

        return 0;
}
===============================================
//2
#include 
#include 

int main()
{
        char str1[]="Original String";
        char str2[]="New String";
        char str3[100];

        strcpy(str1,str2);
        strcpy(str3,"Copy Successful");

        printf("str1:%sn",str1);
        printf("str2:%sn",str2);
        printf("str3:%sn",str3);

        return 0;
}

[wlp@192 class_1]$ gcc test5.c && ./a.out
sizeof str=11 //包含
strlen str=10
//3
#include 
#include 

int main()
{
        char str1[]="To be or not to be";
        char str2[40];

        strncpy(str2,str1,5);
        str2[5]='';

        printf("str2:%sn",str2);

        return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
str2:To be
=====================================================
//4
#include 
#include 

int main()
{
        char str1[]="I love";
        char str2[]="FishC.com";

        strcat(str1," ");
        strcat(str1,str2);

        printf("str2:%sn",str1);

        return 0;
}

[wlp@192 class_1]$ gcc test5.c && ./a.out
str2:I love FishC.com
=========================================================
//6
#include 
#include 

int main()
{
        char str1[]="FishC.com";
        char str2[]=".cn";
        char str3[]=".cn";

        if(strcmp(str1,str2))
        {
                printf("两个字符串一致n");
        }
        else
        {
                printf("两个字符串有差异n");
        }
        return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
两个字符串一致

2、二维数组、指针变量、指针与数组

 //1
 #include 

int main()
{
        int a[3][4]={{1,2,3,4},//3可以省略
                        {5,6,7,8},
                        {9,10,11,12}
                        };
        int i,j;

        for(i=0;i<3;i++)
        {
                for(j=0;j<4;j++)
                {
                        printf("%d ",a[i][j]);
                }
                printf("n");
        }

        return 0;
}

 [wlp@192 class_1]$ gcc test5.c && ./a.out
1 2 3 4 
5 6 7 8 
9 10 11 12 

#include 

int main()
{
        char *cBooks[]={//定义指针数组
        "C程序设计语言",
        "C专家编程",
        "C陷阱与缺陷",
        "C和指针",
        "带你学C带你飞",
        "C Primer Plus"
        };
        char **byFishC;//byFishC相当于pp
        char **jiayuloves[4];
        int i;

        byFishC=&cBooks[5];//指向指针的指针指向指针数组 【pp=&p】
        jiayuloves[0]=&cBooks[0];
        jiayuloves[1]=&cBooks[1];
        jiayuloves[2]=&cBooks[2];
        jiayuloves[3]=&cBooks[3];

        printf("FishC出版的书有:%sn",*byFishC);
		printf("小甲鱼喜欢的书有:n");
		
        for(i=0;i<4;i++)
        {
                printf("%sn",*jiayuloves[i]);
        }

        return 0;
}                                                     
[wlp@192 class_1]$ gcc test5.c && ./a.out
FishC出版的书有:C Primer Plus
小甲鱼喜欢的书有:
C程序设计语言
C专家编程
C陷阱与缺陷
C和指针
//1
#include 

int main()
{
        char a='F';
        int f=123;

        char *pa=&a;
        int *pf=&f;

        printf("a=%cn",*pa);
        printf("f=%dn",*pf);

        printf("the addr of a is :%pn",pa);
        printf("the addr of f is:%pn",pf);//地址值占位符%p

        return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
a=F
f=123
the addr of a is :0x7fffdc7ff16f
the addr of f is:0x7fffdc7ff168

//指针变量间接访问变量
#include 

int main()
{

        int a;
        int *pa=&a;

        printf("请输入一个整数:");
        scanf("%d",&a);
        printf("a=%dn",a);

        printf("请重新输入一个整数:");
        scanf("%d",pa);//通过指针间接访问变量[取地值操作符&在定义指针变量已经写过了]
        printf("a=%dn",a);
        return 0;
}
==
//数组地址与数组元素地址关系
#include 

int main()
{
        char str[120];

        printf("请输入字符:");
        scanf("%s",str);//这里没有加取地址符,因为直接打印数组名,就是打印数组地址%p接收

        printf("输入的是:%sn",str);
        printf("str地址是:%pn",str);
        printf("str第一个元素地址是:%pn",&str[0]);

        return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
请输入字符:aa
输入的是:aa
str地址是:0x7fffe120ed80
str第一个元素地址是:0x7fffe120ed80

#include 

int main()
{
        char a[]="FishC";
        int b[5]={1,2,3,4,5};
        float c[5]={1.1,1.2,1.3,1.4,1.5};
        double d[]={1.1,1.2,1.3,1.4,1.5};

        char *p=a;

        printf("*p=%c,*(p+1)=%c,*(p+2)=%cn",*p,*(p+1),*(p+2));

        printf("a[0] ->%p,a[1] ->%p,a[2] ->%pn",&a[0],&a[1],&a[2]);
        printf("b[0] ->%p,b[1] ->%p,b[2] ->%pn",&b[0],&b[1],&b[2]);
        printf("c[0] ->%p,c[1] ->%p,c[2] ->%pn",&c[0],&c[1],&c[2]);
        printf("d[0] ->%p,d[1] ->%p,d[2] ->%pn",&d[0],&d[1],&d[2]);
        return 0;
}
[wlp@192 class_1]$ gcc test5.c && ./a.out
*p=F,*(p+1)=i,*(p+2)=s
a[0] ->0x7fff7b827980,a[1] ->0x7fff7b827981,a[2] ->0x7fff7b827982
b[0] ->0x7fff7b827960,b[1] ->0x7fff7b827964,b[2] ->0x7fff7b827968
c[0] ->0x7fff7b827940,c[1] ->0x7fff7b827944,c[2] ->0x7fff7b827948
d[0] ->0x7fff7b827910,d[1] ->0x7fff7b827918,d[2] ->0x7fff7b827920
==
//通过指针变量遍历字符串
#include 
#include 

int main()
{
        char *str="I love you!";//通过指针变量间接初始化字符内容
        int i,length;

        length=strlen(str);

        for(i=0;i

int main()
{
	int array[10]={0,1,2,3,4,5,6,7,8,9};
	int *p=array;
	int i;
	
	for(i=0;i<10;i++)
	{
		printf("%dn",*(p+i));
	}

	return 0;	
}

//指针变量遍历二维数组
#include 

int main()
{
	int array[3][4]={
		{0,1,2,3},
		{4,5,6,7},
		{8,9,10,11}}:
		
	int i,j;
	
	for(i=0;i<3;i++)
	{
		for(j=0;j<4;j++)
		{
			printf("%2d",*(*(array+i)+j)):
		}
		printf("n");
	}
	return 0;
}
==
//数组指针遍历一维数组
#include 

int main()
{
        int temp[5]={1,2,3,4,5};
        int (*p2)[5]=&temp;//()为p,存放地址
        int i;

        for(i=0;i<5;i++)
        {
                printf("%dn",*(*p2+i));//输出的是值,所以外面加取值符
        }

        return 0;
}                       
//数组指针遍历二维数组
#include 

int main()
{
	int array[][4]={
		{0,1,2,3},
		{4,5,6,7},
		{8,9,10,11}}:
		int (*p)[4]=array;//看成一维数组指针变量;
		int i,j;
		
		for(i=0;i<3;i++)
		{
			for(j=0;j<4;j++)
			{
				printf("%2d",*(*(p+i)+j));//指针变量遍历二维数组
			}
			printf("n");
		}
		
	return 0;
}

3、数组与指针的关系


 //1、通过指针变量计算字符串有多少字符
#include 

int main()
{
        char str[]="I love you";
        char *target=str;//定义指针变量指向数组,这样while里就为左值,可变的了
        int count=0;

        while(*target++ !='')//字符数组指针指向结束位为止
        {
                count++;
        }
        printf("总共有%d个字符n",count);


        return 0;
}

[wlp@192 class_1]$ gcc test5.c && ./a.out
总共有10个字符
==
//2.1、初始化指针数组
#include 

int main()
{
        int a=1;
        int b=2;
        int c=3;
        int d=4;
        int e=5;
        int *p1[5]={&a,&b,&c,&d,&e};
        int i;

        for(i=0;i<5;i++)
        {
                printf("%d",*p1[i]);
        }

        return 0;
}
12345
//注意与下一个对比

#include 

int main()
{
        char a[]="FishC";
		char *p=a;

        printf("*p=%c,*(p+1)=%c,*(p+2)=%cn",*p,*(p+1),*(p+2));
		return 0;
}		
[wlp@192 class_1]$ gcc test5.c && ./a.out
*p=F,*(p+1)=i,*(p+2)=s

==
#include 

int main()
{
        char *p1[3]={
                "加油吧",
                "少年",
                "此时不搏,何时搏"
        };
        int i;

        for(i=0;i<3;i++)
        {
                printf("%sn",p1[i]);//这里如果加*,则是打印的字符串地址
        }

        return 0;
}

[wlp@192 class_1]$ gcc test5.c && ./a.out
加油吧
少年
此时不搏,何时搏
//2.2、
/
#include 

int main()
{
        int array[4][5]={0};
        printf("sizeof int:%dn",sizeof(int));
        printf("array:%pn",array);
        printf("array+1:%pn",array+1);

        return 0;
}

[wlp@192 class_1]$ gcc test5.c && ./a.out
sizeof int:4
array:0x7fff03b1a010
array+1:0x7fff03b1a024//跨越了5个元素

 #include 

int main()
{

        int array[2][3]={{0,1,2},{3,4,5}};
        int (*p)[3]=array;

        printf("%dn",**(p+1));
        printf("%dn",**(array+1));
        printf("%dn",array[1][0]);
        printf("%dn",*(*(p+1)+2));
		printf("%dn",*(*(array+1)+2));
        printf("%dn",array[1][2]);

        return 0;
}

[wlp@192 class_1]$ gcc test5.c && ./a.out
3
3
3
5
5
5

 #include 
 void main()
 {
	 int a[10];
	 int i;
	 for(i=0;i<10;i++)
	 {
		 scanf("%d",&a[i]);
	 }
	 printf("n");
	 //2、
	 for(i=0;i<10;i++)
		 printf("%d",*(a+i));
	 //3、
	 int *p;
	 for(p=a;p<(a+10);p++)
	 {
		 printf("%d",*p);
	 }
	 
 }





C语言基础语法【1】_z输关的博客-CSDN博客

​​​​​​​C语言基础语法【3】_z输关的博客-CSDN博客

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/850435.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号