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
相关推荐
norlan_jame11 分钟前
C-PHY与D-PHY差异
c语言·开发语言
czy87874751 小时前
除了结构体之外,C语言中还有哪些其他方式可以模拟C++的面向对象编程特性
c语言
m0_531237171 小时前
C语言-数组练习进阶
c语言·开发语言·算法
Z9fish5 小时前
sse哈工大C语言编程练习23
c语言·数据结构·算法
代码无bug抓狂人5 小时前
C语言之单词方阵——深搜(很好的深搜例题)
c语言·开发语言·算法·深度优先
CodeJourney_J5 小时前
从“Hello World“ 开始 C++
c语言·c++·学习
枫叶丹47 小时前
【Qt开发】Qt界面优化(七)-> Qt样式表(QSS) 样式属性
c语言·开发语言·c++·qt
with-the-flow7 小时前
从数学底层的底层原理来讲 random 的函数是怎么实现的
c语言·python·算法
Sunsets_Red8 小时前
P8277 [USACO22OPEN] Up Down Subsequence P 题解
c语言·c++·算法·c#·学习方法·洛谷·信息学竞赛
小刘爱玩单片机8 小时前
【stm32简单外设篇】- 测速传感器模块(光电)
c语言·stm32·单片机·嵌入式硬件