从零开始学习C语言(第二天)

  今天我学习了C语言的常量分为:字面常量、const修饰的常量、#define定义的标识符常量、枚举常量。字符串、strlen、while

字面常量:指的是输入程序中的值 。表示数字如:3、5、100、3.14.....

#include<stdio.h>

int main()
{
	int a = 4;//a就是int类型的变量,而4就是常量。
	printf("%d\n", a);
	return 0;
}

const修饰的常量:使变量无法改变

#include<stdio.h>

int main()
{
    const int a = 2;//让变量a无法改变(本质上还是变量,只是const赋予了常属性)
	printf("%d\n", a);
    a=8;
    printf(“%d\n”,a);

	return 0;
		
}
//加入const之前,打印出来的结果是:2,8
//加入cosnt之后,运行起来会报错。因为const使的a的值无法改变,就是2而不是8.

#define定义的标识符常量:把标识符定义为其后的常量

#include<stdio.h>
#define MAX 10
int main()
{
	int arr[MAX] = { 0 };//arr数组,[]里的只能输入常量,但是define让MAX来表示10这个常量
	printf("%d\n", MAX);
	return 0;
 }

枚举常量:表示一一列举的意思

#include<stdio.h>

enum Color//enum就是枚举常量
{
	RED,
	YELLOW,
	BLUE
//常量值在没有赋值时系统会默认给它的第一个变量赋值0,后面的依次为1、2......
};

int main()
{
    enum Color color = BLUE;
    printf("%d\n", color);//2
	printf("%d\n", RED);//0
	printf("%d\n", YELLOW);//1
	printf("%d\n", BLUE);//2printf("%d\n", color);
//枚举常量(RED、YELLOW、BLUE)是不可以改的,但是通过枚举类型所创建出来的变量(color)是可以改的	
	return 0;
}

字符串:由双引号引起来的一串字符称为字符串 %s打印字符串

#include<stdio.h>

int main()
{
	char arr1[] = "abc";//"abc"='a','b','c','\0'——'\0'表示字符串的结束标志
	printf("%s\n", arr1);
	return 0;
}

strlen——计算字符串长度的

#include<stdio.h>

int main()
{
	char arr1[] = "abc";
	printf("%d\n", strlen(arr1));
	printf("%d\n", strlen("c:\test\32\test.c"));//转义字符为1个长度,/t,/32
	return 0;

}

while——循环

#include<stdio.h>

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int a = 0;
	while (a < 10)//()里写的是判断条件
	{
		printf("%d\n", arr[a]);
		a++;
	}
	return 0;
	
}

 还有许多学习的东西我就不一一列举了,学好C语言关键还是要多写代码。ps:这还只是初始C语言的阶段,还没深入了解,有些错误的地方还请多多执教!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值