(c语言基础笔记)字符串,数组,跳转语句

跳转语句

在这里插入图片描述

#include <stdio.h>
int main()
{
 for (int i = 0; i < 10; i++)
 {
  for (int j = 0; j < 10; j++)
  {
   if (j == 5)
   {
    break;
   }
   printf("%d %d\n", i, j);
  }
 }
 return 0;
}

#include <stdio.h>
int main(void)	//只打印偶数
{
  for(int i=10;i<100;i++)
	{
	if(i%2==1)
	  {
	    continue;
	  }
	  printf("%d\n",i);
	}
  return 0;
}

goto语句:无条件跳转

#include <stdio.h>
int main(void)
{
  printf("hello world1\n");
  printf("hello world2\n");
  goto FLAG;
  printf("hello world3\n");
  printf("hello world4\n");
  goto FLAG;
  printf("hello world5\n");
  printf("hello world6\n");
  return 0;
}
运行结果:
hello world1
hello world2
hello world5
hello world6

数组
在这里插入图片描述

#include <stdio.h>
int main()
{
 int arr[10] = { 9,4,2,1,8,5,3,6,10,7 };
 for (int i = 0; i < 10; i++)
 {
  printf("%d ",arr[i]);
 }
 return 0;
}
运行结果;
9 4 2 1 8 5 3 6 10 7

数组下标越界(参考指针内容)
例:十只野狗称体重:

#include <stdio.h>
int main()
{
 int wild_dog_weight[10];
 printf("请输出所称野狗们的体重!");	
 for (int i = 0; i < 10; i++)
  scanf_s("%d", &*(wild_dog_weight + i));
 int max = *wild_dog_weight;	
 for (int i = 1; i < 10; i++)
 {
  if (max < *(wild_dog_weight + i))//*(数组名+i)表示该数组的第i个元素
  {
   max = *(wild_dog_weight + i);
  }
 }
 printf("the max weight of wild dog is:%d", max);
 return 0;
}

例:冒泡排序法:

#include <stdio.h>
int main()
{
 int i, j, t;
 int a[10];
 printf("please input datas\n");
 for (i = 0; i < sizeof(a)/sizeof(int); i++)
 {
  scanf_s("%d", &*(a + i));
 }
 for (j = 0;j< sizeof(a) / sizeof(int)-1;j++)
 {
  for (i = 0; i < sizeof(a) / sizeof(int) -1;i++)
  {
   if (*(a + i) > * (a + i + 1))
   {
    t = *(a + i); *(a + i) = *(a + 1 + i); *(a + 1 + i) = t;
   }
  }
 }
 printf("\n");
 for (i = 0; i < 10; i++)
 {
  printf("%d ", *(a + i));
 }
 return 0;
}

二维数组
二维数组常称为"矩阵"。把二维数组写出行和列的排列方式,可以有助于形象化地理解二维数组的逻辑结构。
在这里插入图片描述
注意:
用矩阵形式(如3行4列形式)表示二维数组,是逻辑上的概念,能形象地表示出行列关系,而在内存中,各元素是连续存放的,不是二维的,是线性的。

例:

#include <stdio.h>
int main()
{
 int a[2][3] = { {1,2,3},{4,5,6} };
 int b[3][2], i, j;
 printf("array a:\n");
 for (i = 0; i <= 1; i++)
 {
  for (j = 0; j <= 2; j++)
  {
   printf("%5d", a[i][j]);
   b[j][i] = a[i][j];
  }
  printf("\n");
 }
 printf("arrray b:\n");
 for (i = 0; i < 2; i++)
 {
  for (j = 0; j <= 1; j++)
  {
   printf("%5d", b[i][j]);
  }
  printf("\n");
 }
 return 0;
}
运行结果:
array a:
    1    2    3
    4    5    6
arrray b:
    1    4
    2    5

多维数组
例:

#include <stdio.h>
int main()
{
 //无论是几维数组,其在计算机内部均是线性排布,只不过分段的来表示罢了
 int arr[2][3][4] =
 {
  {
   {1,2,3,4},
   {2,3,4,5},
   {3,4,5,6}
  },
  {
   {4,5,6,7},
   {5,6,7,8},
   {6,7,8,9}
  }
 };
 for (size_t i = 0; i < 2; i++)
 {
  for (size_t j = 0; j < 3; j++)
  {
   for (size_t k = 0; k < 4; k++)
    printf("%d\t", arr[i][j][k]);
  }
 }
 return 0;
}
运行结果:
1       2       3       4       2       3       4       5       3       4       5       6       4       5       6       7
5       6       7       8       6       7       8       9

字符数组和字符串
字符数组:

#include <stdio.h>
int main()
{
 //定义字符数组
 char arr[6] = {'h','e','l','l','o','\0'};
 //字符
 //char ch='a'
 //字符串 字符串结束标志\0
 printf("%s", arr);
 return 0;
}
运行结果:
hello

字符串的拼接:

#include <stdio.h>
int main()
{
 //字符串拼接
 char ch1[]="hello";
 char ch2[]="world";
 char ch3[20];
 
 int i=0;
 int j=0;
 while(ch1[i]!='\0')
 {
  ch3[i]=ch1[i];
  i++;
 }
 while(ch2[j]!='\0')
 {
  ch3[i+j]=ch2[j];
  j++;
 }
 printf("%s",ch3);
 return 0;
}
运行结果:
helloworld

字符数组和字符串区别:
1:c语言中没有字符串这种数据类型,可以通过char的数组来替代
2:字符串一定是一个char数组,但char的数组未必是字符串
3:数字0(和字符’\0’等价)结尾的char数组就是一个字符串,但是如果char数组没有以数字0结尾,那么就不是一个字符串,只是一个普通字符数组,所以字符串是一种特殊的char型数组。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <stdio.h>
int main(void)
{//fgets获取字符串少于元素个数会有\n,多余则无
 char ch[10];  //字符串中含有w o r l d \n
 fgets(ch,sizeof(ch);stdin); //fgets可以接收回车
 printf("%s",ch);
 return 0;
}
运行结果:
world
world
请按任意键继续...//此处fgets函数将回车也做字符串打出

在这里插入图片描述

#include <stdio.h>
int main()
{
 char ch[]="hello world";
 fputs(ch,stdout);
 return 0;
}
运行结果:
hello world

在这里插入图片描述
在这里插入图片描述

#include <stdio.h>
int main()
{
 //计算字符串的有效个数
 char ch[100]="hello world";
 printf("数组大小:%d",sizeof(ch));
 printf("字符串长度:%d",sizeof(ch));
 return 0;
}
运行结果:
数组大小:100字符串长度:11请按任意键继续...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值