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
相关推荐
自信1504130575920 分钟前
初学者小白复盘23之——联合与枚举
c语言·1024程序员节
秃秃秃秃哇1 小时前
C语言实现循环链表demo
linux·c语言·链表
小曹要微笑5 小时前
STM32H7系列全面解析:嵌入式性能的巅峰之作
c语言·stm32·单片机·嵌入式硬件·算法
松涛和鸣8 小时前
14、C 语言进阶:函数指针、typedef、二级指针、const 指针
c语言·开发语言·算法·排序算法·学习方法
星期天210 小时前
3.0 C语⾔内存函数:memcpy memmove memset memcmp 数据在内存中的存储:整数在内存中的存储 ⼤⼩端字节序和字节序判断
c语言·数据结构·进阶·内存函数·数据内存存储
代码雕刻家16 小时前
C语言的左对齐符号-
c语言·开发语言
star learning white18 小时前
xmC语言8
c语言·开发语言·算法
赖small强19 小时前
【Linux C/C++开发】第16章:多线程编程基础
linux·c语言·c++·多线程编程·进程和线程的本质区别
nono牛19 小时前
Android Binder C/C++ 层详解与实践
android·c语言·binder
雨落在了我的手上21 小时前
C语言入门(十九):指针(5)
c语言