0
点赞
收藏
分享

微信扫一扫

Stirling PDF:免费PDF开源编辑工具

C Style

在 C 语言中,没有专门的输入输出语句,所有的输入输出操作都是通过对标准输入输出库函数(包含在 stdio.h 头文件中)的调用实现。最常用的输入输出函数有

  • printf()

  • scanf()

printf()

  • 按格式化字符串输出

printf("%[标志][最小输出宽度][.精度][类型]",输出项列表)

Type 类型

  • d/ld: int/long int, 输出十进制有符号整数

  • o/O: unsigned int, 输出八进制无符号整数

  • x/X: unsigned int, 输出十六进制无符号整数

#include <stdio.h>
int main()
{
    printf("%d\n", 33); 
    printf("%D\n", 33); 

    printf("%o\n", 33);
    printf("%O\n", 33);

    printf("%x\n", 33);
    printf("%X\n", 33);


    printf("%ld\n", 10208489003884);
    return 0;
}
  • f/lf: float/double, 单精度/双精度

  • e/E: double, 科学计数法

#include <stdio.h>
int main()
{
    printf("%f\n", 3.1415916); 
    printf("%lf\n", 3.1415916);
    printf("%e\n", 0.00000314159267366487);
    printf("%E\n", 0.00000314159267366487);

    return 0;
}
  • c: char, 字符型

  • s: char*, 字符串, 以空字符 '\0' 结尾

#include <stdio.h>
int main()
{
    printf("%c\n", 'a');
    printf("%s", "Hello World\n");
    return 0;
}
  • p: 输出 16 进制的指针地址

#include <stdio.h>
int main()
{
    int a = 10;
    printf("address of a: %p\n", &a);

    return 0;
}

精度

#include <stdio.h>
int main()
{
    float a = 3.1415926;   
    float b = 0.0018004887367;   
    printf("%.2f\n", a);
    printf("%.3f\n", a);
    printf("%.5f\n", b);

    printf("%e\n", b);
    printf("%.4E\n", b);
    
    return 0;
}

标志

  • -: 左对齐,右边填空格(默认是右对齐,左边填空格)

  • #: Type 为 o、O、x、X 时,增加前缀0、0x、0X

  • *: 用 * 代替 精度/最小输出宽度,需要在输出列表给出

#include <stdio.h>
int main()
{
    float a = 3.1415926;   
    float b = 0.0018004887367; 

    printf("%6.2f\n", a); // 输出宽度 6, 保留 2 小数点
    printf("%6.3f\n", a); // 输出宽度 6, 保留 3 小数点

    printf("%-6.2f\n", a); // 靠左对齐, 输出宽度 6, 保留 2 小数点
    printf("%-6.3f\n\n", a); // 靠左对齐, 输出宽度 6, 保留 3 小数点

    printf("%14e\n", b); // 输出宽度 14
    printf("%14.4E\n", b); // 输出宽度 14, 保留 4 位小数

    printf("%-14e\n", b);
    printf("%-14.4E\n\n", b);

    // 控制字符串最小输出宽度
    printf("%-5s: %d bytes.\n", "int", sizeof(int));
    printf("%-5s: %d bytes.\n", "char", sizeof(char));
    printf("%-5s: %d bytes.\n\n", "float", sizeof(float));

    printf("%5s: %d bytes.\n", "int", sizeof(int));
    printf("%5s: %d bytes.\n", "char", sizeof(char));
    printf("%5s: %d bytes.\n", "float", sizeof(float));

    return 0;
}
#include <stdio.h>
int main()
{
    printf("%o\n", 65);
    printf("%O\n", 65);
    printf("%x\n", 65);
    printf("%X\n\n", 65);

    printf("%#o\n", 65);
    printf("%#O\n", 65);
    printf("%#x\n", 65);
    printf("%#X\n", 65);

    return 0;
}

 

scanf()

  • 从键盘输入取得数据,所获得的数据按指定输入格式被赋给相应的输入项。

  • 修饰符: 可选,表示输入字段宽度(数字)、约定整数是否是长整型(l)

  • 格式字符数据类型

<div align=center

<image src="imgs/scanf.png" width=500>

</div>

  • 输入项列表

    • 一个或多个变量地址,变量地址有多个时,用逗号","分隔

    • 变量要加取地址操作符 &, 只有这样函数才能改变其值

    • 输入类型与变量类型应尽量一致,以避免类型不匹配而造成的错误

#include <iostream>
using namespace std;

int main()
{
    // 示例1:
    long a;            //声明一个长整型变量
    scanf("%9ld", &a); //输入一个字段宽度为 9 位有效数字的十进制长整型数据,保存在变量a中

    /*程序运行时,会要求在终端上输入数据,输入后按 Enter 键,程序继续运行。假如输入的数据是 1234567890,那么前 9 位数字将作为有效的输入,变量 a 保存的数值为 123456789。*/

    // 示例2:
    int a, b, c; //声明 3个整型变量
    //输入3个整型数据,分别保存在变量 a、b、c中, 注意用逗号隔开
    scanf("%d, %d, %d", &a, &b, &c); 
    printf("%d, %d, %d\n", a, b, c);
}

C++ Style

C++ 的 iostream 函数库

  • cout 标准输出流,通常被定向到屏幕

  • cin 标准输入流,通常被定向到键盘

  • *cerr 专门用来显示出错信息,可以被重新定向到标准输出设备

  • *clog 专门用来显示出错信息,可以被重新定向到一个日志文件(log file)

#include <iostream>

std::cout

  • 输出流 cout 与运算符 << 一起使用

#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    int age = 21;
    int zipcode = 88888;
    cout << "output sentence";
    cout << 120;  
    cout << x;     
    cout << "Hello, I am " << age 
         << " years old and my zipcode is " << zipcode;
}

cout 并不会在末尾加换行符,换行符可以写作\n,也可以用操作符 endl 来换行

#include <iostream>
using namespace std;

int main()
{
    cout << "First sentence.\n";
    cout << "Second sentence.\nThird sentence.";
    cout << "First sentence." << endl;
    cout << "Second sentence." << endl;
}

输出流 cout 的一个格式控制函数,是在 iostream 中的一个成员函数。precision()返回当前的浮点数的精度值,而 cout.precision(val) 其实就是在输出的时候设定输出值以新的浮点数精度值显示,即小数点后保留 val 位

float val = 3.1415926
cout.precision(3) // 后面的输出保留3位数
cout << val << endl;

std::cin

  • cin 后面必须跟一个变量以便存储读入的数据

  • 使用 cin输入的时候必须考虑后面的变量类型

#include <iostream>
using namespace std;

int main ()
{
    int a, b, c;

    cin >> a ;
    cout << "a: " << a << endl;
    // 输入时,两个变量之间可以以任何有效的空白符号间隔,包括空格、跳跃符(tab)或换行符。
    cin >> b >> c;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl;
    return 0;
} 

 

举报

相关推荐

0 条评论