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
相关推荐
小志biubiu1 小时前
linux_缓冲区及简单libc库【Ubuntu】
linux·运维·服务器·c语言·学习·ubuntu·c
MeowKnight9582 小时前
【C】函数指针
c语言·1024程序员节
胜天半月子3 小时前
嵌入式开发 | C语言 | 单精度浮点数4字节可以表示的范围计算过程
c语言·嵌入式c·1024程序员节·单精度浮点数计算
m0_748233644 小时前
单调栈详解【C/C++】
c语言·c++·算法·1024程序员节
海上Bruce4 小时前
C primer plus (第六版)第十一章 编程练习第14题
c语言
Yupureki5 小时前
从零开始的C++学习生活 14:map/set的使用和封装
c语言·数据结构·c++·学习·visual studio·1024程序员节
一匹电信狗5 小时前
【LeetCode_876_2.02】快慢指针在链表中的简单应用
c语言·数据结构·c++·算法·leetcode·链表·stl
胖咕噜的稞达鸭5 小时前
算法入门---专题二:滑动窗口2(最大连续1的个数,无重复字符的最长子串 )
c语言·数据结构·c++·算法·推荐算法·1024程序员节
Yupureki5 小时前
从零开始的C++学习生活 15:哈希表的使用和封装unordered_map/set
c语言·数据结构·c++·学习·visual studio·1024程序员节
傻童:CPU6 小时前
C语言需要掌握的基础知识点之链表
c语言·1024程序员节