C 语言笔记
  • Introduction
  • First Chapter
  • 第一个程序
  • 变量与数据类型
    • puts 与 printf
    • 整数(short,int,long)
    • 小数(float,double)
    • 字符(char)与字符串
    • 数据类型转换
    • 自增(++)和自减(--)
    • 变量和数据类型
  • 输入输出
    • 数据输出函数
    • 从键盘输入数据
  • 附录*关键字及其解释
  • 附录*转义字符
  • 附录*进制转换
  • 附录*单位换算
  • 预处理命令
  • 结构体
    • -> 访问符意思
  • 指针
    • 指针与二维数组
    • 函数指针(指向函数的指针)
    • 彻底攻克C语言指针
    • 指针的总结
  • 资料
Powered by GitBook
On this page
  • putchar()
  • 关于换行

Was this helpful?

  1. 输入输出

数据输出函数

在C语言中,有三个函数可以用来在显示器上输出数据:

  • puts():只能输出字符串,

  • putchar():只能输出单个字符,本节将会介绍。

  • printf():可以输出各种类型的数据,并且可以进行格式化输出。

printf() 是最灵活、最复杂、最常用的输出函数,完全可以替代 puts() 和 putchar(),一定要掌握。

putchar()

putchar() 函数只能用来输出单个字符,例如:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    putchar('a');
    putchar(7);
    putchar('\x46');

    system("pause");
    return 0;
}

运行程序,输出 aF,同时会听到喇叭发出“嘟”的声音。

关于换行

puts() 函数在输出结束时会自动换行,而 printf() 和 putchar() 不会,需要手动添加换行符\n。如下所示:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char *str = "ccc";
    int n = 100;
    char c = 'Z';
    puts(str);
    putchar(c);
    printf("%d", n);
    putchar(c);

    system("pause");
    return 0;
}

运行结果:

c.biancheng.net
Z100Z请按任意键继续. . .
Previous输入输出Next从键盘输入数据

Last updated 5 years ago

Was this helpful?