zoukankan      html  css  js  c++  java
  • 输入/输出的格式和方法

    1.char s[][];

    scanf("%s",s[i]);

    s[i]就是地址,不用加&

    2.char c;

    scanf("%c",&c);

    像long,double等都要加&

    &是取地址,把数输入到这个地址的存储单元中,存储x个单元(根据%d,%lf,%c等而定,如果是%s,以输入‘'结束)

    3.%(num)ld/s

    long n;

    scanf("%4ld",&n);

    printf("%ld ",n);

    Input:12345

    Output:1234

    %(num)ld 只读取前num个数字

    同样的:

    char s[100];

    scanf("%5s",s);

    printf("%s ",s);

    Input:abcdefg

    Output:abcde

    当然,如果遇到空格还是得停止

    Input:abc de

    Output:abc

    用于字符处理

    4.scanf中的空格

        long n;
        char c,e;
        scanf("%ld %c",&n,&c);
        scanf(" %c",&e);
        printf("%ld %c %c ",n,c,e);

    Input:

    10 x

    y

    Output:

    10 x y

    空格” ”:如果即将输入的字符为' ',' ',' '时把该字符忽略,否则不忽略

    第一个空格忽略x之前的空格,

    第二个空格忽略回车键。

    如果没有第二个空格,则回车后就结束输入,然后e=' '。

    //////////////////////////////////

    Input:

    10_x

    y

    Output:

    10 _ x

    (科普:

    是换行,英文是New line,表示使光标到行首
    是回车,英文是Carriage return,表示使光标下移一格)

    5.scanf中的其它字符

    对日期进行格式的转换

        char a,b,c,d,e,f;
        scanf("%c%c-%c%c-%c%c",&a,&b,&c,&d,&e,&f);
        printf("%c%c:%c%c:%c%c ",a,b,c,d,e,f);

    Input:

    17-04-13

    Output:
    17:04:13

    //////////////////////////////////

        long n;
        scanf("123%ld",&n);
        printf("%ld ",n);

    Input:

    1234

    Output:
    4

    对于scanf中的其它字符,如果键盘输入与输入格式相同,则忽略那些字符,否则输出

    ////////////////////////////////////////////////////////////////

    对于scanf中的其它字符,如果键盘输入与输入格式不相同,则输出一些奇奇怪怪的东西,所以最好不要这样做

        long n;
        scanf("123%ld",&n);
        printf("%ld ",n);

    Input:

    111

    Output:

    49

    ////////////////////////////////

        char c;
        scanf("a%c",&c);
        printf("%c ",c);

    Input:

    dc

    Output:

    6.printf  %.(num)f  保留小数点后num位,要是不够则补0

        double f=123.4567;
        printf("%.3f",f);

    Output:123.456

    ////////////////////////////////

        double f=123.4567;
        printf("%.5f",f);

    Output:123.45670

    ////////////////////////////////

        double f=123.4567;
        printf("%.f",f);

    Output:123(%.(num)f:默认保留小数点后0位)

    ////////////////////////////////

        double f=123.4567;

        printf("%f",f);

    Output:123.456700(默认保留小数点后6位)

    7.printf  %(num)ld/s/lf  输出num位  若输出小于num位,前面用" "补齐,否则不用理会

        long a=12345;
        printf("%6ld ",a);

    Output: 12345(1前面有一个空格)

    ////////////////////////////////

        long a=12345;
        printf("%3ld ",a);

    Output: 12345

    ////////////////////////////////

        double f=123.4567;
        printf("%6.1f ",f);

    Output: 123.5(1前面有一个空格)

    8.sscanf(我觉得第一个s指的是string)

    (来自百度)

    9.sprintf

    (来自百度)

  • 相关阅读:
    Log Explorer的使用
    Windows消息大全
    Devepress LayoutControl的使用
    IIS7虚拟目录出现HTTP错误500.19(由于权限不足而无法读取配置文件)
    检索参数信息并填充指定的 SqlCommand 对象的 Parameters 集合
    Failed to access IIS metabase.
    SQL Server FOR XML PATH 语句的应用
    C#调用API:mouse_event 模拟鼠标事件
    C#执行SQL脚本
    ADO.NET 中的表达式
  • 原文地址:https://www.cnblogs.com/cmyg/p/6704576.html
Copyright © 2011-2022 走看看