初识C语言-常量,字符串,转义字符,注释

常量(Constant)

常量是固定值,在程序执行期间不会改变。这些固定的值,又叫做字面量。

常量可以是任何的基本数据类型,比如整数常量、浮点常量、字符常量,或字符串字面值,也有枚举常量。

常量就像是常规的变量,只不过常量的值在定义后不能进行修改。

C语言中的常量分为以下以下几种:

1、字面常量

#include <stdio.h>

int main()
{
	//字面常量
	3.14;
	9999;
    'a';//字符
    "hello";
	return 0;
}
2、const修饰的常变量
#include <stdio.h>

int main()
{
	//const修饰的常变量
	const int a = 10;  //这里的a是const修饰的常变量
	
	//在C语言中,const修饰的a,本质是变量,但是不能直接修改,有常量的属性。
	
    a = 20;  //a是不能直接修改的!
	printf("%d", a);  //10
	return 0;
}

注:
上面例子上的 a 被称为 const 修饰的常变量, const 修饰的常变量在C语言中只是在语法层面限制了
变量 a不能直接被改变,但是 a本质上还是一个变量的,所以叫常变量。

3、define 定义的标识符常量
#include <stdio.h>
#define MAX 100
#define MIN 0

int main()
{
	printf("%d\n", MAX);//100
	int a = MAX;
	printf("%d\n", a);//100
	printf("%d\n", MIN);//0
	//MAX = 10; //err
	//define定义的标识符常量不能修改
	return 0;
}
4、枚举常量
#include <stdio.h>
//注:枚举常量的默认是从0开始,依次向下递增1的
enum Color  //颜色
{
	//枚举常量
	RED,
	BULE,
	GREEN,
	BLACK
};
enum SEX  //性别
{
	MALE,
	FEMALE,
	SECRET
};
int main()
{
	int num = 10;
	enum Color c = RED;
	//只能用枚举常量为枚举变量赋值
	//RED = 20;//err
	//MALE = 10;//err
	return 0;
}

字符串(Character string)

"Hello World\n"

这种由双引号(Double Quote)引起来的一串字符称为字符串字面值(String Literal),或者简称字符串。
注:
字符串的结束标志是一个 \0 的转义字符。在计算字符串长度的时候 \0 是结束标志,不算作字符串内容

#include <stdio.h>

int main()
{
	char arr1[]="hello world";
	char arr2[] = { 'h','e','l','l','o'};
	char arr3[] = { 'h','e','l','l','o','\0' };
	
	//注:字符串的结束标志是一个 \0 的转义字符。在计算字符串长度的时候 \0 是结束标志,不算作字符串内容。
	
	printf("%s\n", arr1);//hello world
	printf("%s\n", arr2);//hello烫烫烫烫烫烫烫烫烫烫烫烫烫蘦ello
	printf("%s\n", arr3);//hello
	return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
	char arr1[] = "hello world";
	char arr2[] = { 'h','e','l','l','o','\0' };

	printf("%d\n", strlen(arr1));//11
	printf("%d\n", strlen(arr2));//5

	int len = strlen("hello world");
	//求字符串长度的一个函数,string length  头文件 string.h
	printf("%d\n", len);//11

	printf("%s\n", arr1);//hello world
	printf("%s\n", arr2);//hello
	return 0;
}

int main()
{
	printf("hello\n");//hello
	printf("hel\0lo\n");//hel
	return 0;
}

转义字符(Escape character)

转义字符顾名思义就是转变的意思
打印c:\code\test.c

#include <stdio.h>

int main()
{
	printf("c:\code\test.c");//c:code  est.c
	return 0;
}

运行结果:
在这里插入图片描述
运算结果与c:\code\test.c不同,这是由于转义字符的原因

转义字符
转义字符释义
\ ?在书写连续多个问号时使用,防止他们被解析成三字母词
\ ’用于表示字符常量’
\“用于表示一个字符串内部的双引号
\ \用于表示一个反斜杠,防止它被解释为一个转义序列符
\a警告字符,蜂鸣
\b退格符
\f进纸符
\n换行
\r回车
\t水平制表符
\v垂直制表符
\ddd\ddd ddd表示1~3个八进制的数字。 如: \130 X
\xdddd表示2个十六进制数字。 如: \x30 0

三字母词:

//三字母词不是所有的编译器都适用
int main()
{
	printf("??)");//]
	printf("??(");//[
	return 0;
}

格式说明由“%”和格式字符组成:

格式说明释义
%d打印整型
%c打印字符
%s打印字符串
%f打印float类型的数据
%lf打印double类型的数据
%zu打印sizeof的返回值
#include <stdio.h>
#include <string.h>

int main()
{
	printf("%s\n","hello world\?\?" );//hello world??
	printf("%c\n", '\'');//'
	printf("%c\n", '\"');//"
	printf("hello\n");//hello
	printf("%s\n", "hello");//hello
	printf("hel\\0lo\n"); //hel\0lo
	printf("c:\\test\\test.c\n");//c : \test\test.c
	printf("\a\a");
	printf("abc\nd\tef");//abc
	printf("%c\n", '\130');
	printf("%c\n", '\x063');//c
	printf("%d\n", strlen("qwer t"));//6
	printf("%d\n", strlen("c:\test\628\test.c"));//14
	return 0;
}

注释

  1. 代码中有不需要的代码可以直接删除,也可以注释掉
  2. 代码中有些代码比较难懂,可以加一下注释文字

例:

int ADD(int x, int y)
{
	return x + y;
}
/*C语言风格注释
int SUB(int x, int y)
{
	return x - y;
}*/
int main()
{
	int n1 = 0;
	int n2 = 0;
	//C++注释风格
	//int n3 = 0;
	scanf("%d %d", &n1, &n2);
	printf("%d\n", ADD(n1,n2));
	return 0;
}

注释有两种风格:

  • C语言风格的注释 /* xxxxxx*/
    缺陷:不能嵌套注释
  • C++风格的注释 // xxxxxxxx
    可以注释一行也可以注释多行
  • 23
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bowen…

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值