C语言中一些特殊字符的输出

目录

%的介绍

斜杠与反斜杠

转义字符

%的介绍

int a=1;

1、printf(''%d'',a);//输出1

2、printf(''%%d'',a);//输出%d

3、printf(''%%%d '',a)//输出%1

C语言中,%也是转义符,%%相当于%

斜杠与反斜杠

首先需要明白斜杠与反斜杠,斜杠:/,反斜杠:\

转义字符

常用的转义字符:

'\n',换行

'\r',回车

'\0',空字符,通常用作字符串结束标志

'\b',退格

下面演示这四个常用转义字符的作用

1,正常打印,不用任何转义字符

cpp 复制代码
#include<stdio.h>
int main()
{
pritnf("hello world!");
printf("hello world!");
return 0;
}

输出结果

cpp 复制代码
hello world!hello world!

2,'\n'转义字符

cpp 复制代码
#include<stdio.h>
int main()
{
pritnf("hello world!\n");
printf("hello world!");
return 0;
}

输出结果

cpp 复制代码
hello world!
hello world!

如果两行都加上了'\n'

cpp 复制代码
#include<stdio.h>
int main()
{
pritnf("hello world!\n");
printf("hello world!\n");
return 0;
}

输出结果

cpp 复制代码
hello world!
hello world!

3,'\r'转义字符

cpp 复制代码
#include<stdio.h>
int main()
{
	printf("hello world!\r");
	printf("I love you!");
	return 0;
}

输出结果

cpp 复制代码
I love you!!

再看一个例子

cpp 复制代码
#include<stdio.h>
int main()
{
	printf("123456\r");
	printf("7890");
	return 0;
}

输出结果

cpp 复制代码
789056

'\r'转义字符会把终端界面的输出光标移至当前行的最开头出

4,'\b'转义字符

cpp 复制代码
#include<stdio.h>
int main()
{
	printf("hello world!\b");
	printf("I love you!");
	return 0;
}

输出结果

cpp 复制代码
hello worldI love you!

'\b'会将终端界面的输出光标前移一个元素

cpp 复制代码
#include<stdio.h>
int main()
{
	printf("123456\b");
	printf("7890");
	return 0;
}

输出结果

cpp 复制代码
123457890
相关推荐
ACP广源盛139246256731 小时前
接口适配方案选型|GSV6125@ACP HDMI2.0 转 Type‑C/DP1.4 桥接芯片工程评估
大数据·c语言·开发语言·硬件架构·硬件工程·国产芯片
芯祥联3 小时前
STM32 上跑 MQTT 怎么选?嵌入式 MQTT 客户端 C 语言实现对比
c语言·stm32·单片机·嵌入式硬件·网络协议·开源·broker
Escalating_xu12 小时前
【C 语言】深入理解指针(1·下):指针运算、野指针、assert 与传址实战
java·c语言·开发语言
阿钱真强道13 小时前
20 嵌入式操作系统 | ubus:把自己的程序状态暴露出去
c语言·进程通信·ubus·ubusd·blobmsg
小小龙学IT15 小时前
三菱 PLC MC 协议(SLMP / QnA 兼容 3E 帧)深度解析:从帧结构到 C++/Go 双语言采集实战
c语言·c++·golang
Logic10115 小时前
C语言/数据结构位运算题解:异或XOR找出游戏速通中的“独特初始能量值“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
Chen—LSN16 小时前
C语言——⽂件操作(1)
c语言·开发语言·c++·经验分享·笔记·算法·链表
木由里予17 小时前
GDB 使用手册(详细版)
c语言·vscode
Chen—LSN17 小时前
C语言——文件操作(2)
c语言·开发语言·c++·经验分享·笔记·算法·c#
Escalating_xu18 小时前
【C 语言】深入理解指针(2):数组名、数组传参、二级指针与指针数组
java·c语言·开发语言