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
相关推荐
(Charon)2 小时前
【C++】手写 MySQL 连接池(三):异步任务队列与工作线程
c语言·c++
加油码2 小时前
共享内存详解:原理、系统调用与高性能进程间通信
linux·c语言·c++
Lzh编程小栈3 小时前
【STM32底层精讲】RCC时钟系统超全详解(时钟树+源码+避坑指南)
c语言·stm32·单片机·嵌入式硬件·面试
Nebula嵌入式14 小时前
【C语言】09-深入解析main函数
linux·c语言·开发语言·嵌入式
天空'之城16 小时前
C 语言工业级通用组件手写 23:卡尔曼滤波(简易版)
c语言·卡尔曼滤波·嵌入式算法·工业级组件
888CC++16 小时前
C语言与C++的区别:从面向过程到面向对象
java·c语言·c++
库玛西18 小时前
C++ 运行时多态 :核心总结与原理图解
c语言·c++·笔记
天空'之城20 小时前
C 语言工业级通用组件手写 21:限幅消抖滤波
c语言·嵌入式算法·工业级组件·限幅消抖滤波
Aurorar0rua20 小时前
CS50 x 2024 Notes Algorithms - 07
c语言·开发语言·学习方法
神罚天下ET21 小时前
典型arm位单片机启动流程(从上电到main.c)
c语言·arm开发·单片机