zoukankan      html  css  js  c++  java
  • c语言之字符串和格式化输入输出

    字符串和格式化输入输出

     1 #include<stdio.h>
     2 #include<string.h>
     3 #define DENSITY 62.4
     4 
     5 int main(void)
     6 {
     7     float weight, volume;
     8     int size, letters;
     9     char name[40];//数组
    10 
    11     printf("Hi!What's your first name?");
    12     gets(name);//get(sth.)取得地址
    13     printf("%s,What's your weight in pounds?
    ", name);
    14     scanf_s("%f", &weight);
    15     size = sizeof(name);
    16     letters = strlen(name);
    17     volume = weight / DENSITY;
    18     printf("Well,%s, your volume is %2.2f cubic feet.
    ", name, volume);
    19     printf("Also, your fist name has %d letters
    ", letters);
    20     printf("We have %d bytes to store it in.
    ", size);
    21     return 0;
    22 }

    字符串

    用双引号表示,且C语言没有专门的字符串变量类型,而是把它储存在char数组里面。数组的最后一个位置显示空字符,用于标记字符串的结束。如 "The weather is so well!" 

     1 #include<stdio.h>
     2 #define PRAISE "You are so good!"
     3 
     4 int main(void)
     5 {
     6     char name[40];
     7     printf("What's your name?");
     8     gets(name);
     9     printf("Hello,%s,%s", name, PRAISE);
    10     getchar();
    11     return 0;
    12 }

     字符串和字符的区别

    •  'x'是基本类型,而"x"是派生类型(char数组);
    • "x"实际上是由'x'和空字符两部分组成的。

     strlen函数

     1 #include<stdio.h>
     2 #define PRAISE "You are so good!"
     3 
     4 int main(void)
     5 {
     6     char name[40];
     7     printf("What's your name?");
     8     gets(name);
     9     printf("Hello,%s,%s", name, PRAISE);
    10     printf("Your name of %zd letters occupies %zd memory cells.
    ", strlen(name), sizeof name);
    11     /*strlen函数给出字符数,sizeof为给出所占内存数量;但是两者都需要使用"%zd"转换符来打印。另外sizeof(特定量),如sizeof(char),而一般的类型,不使用圆括号也可以。*/
    12     printf("The phraze of PRAISE has %zd letters", strlen(PRAISE));
    13     printf(" and occupies %zd memory cells.
    ", sizeof PRAISE);
    14     getchar();
    15     return 0;
    16 }

     这样在程序运行时,所有的NAME将会被value替代,这样定义的常量也称为明示常量。

     1 #include<stdio.h>
     2 #define PI 3.14
     3 
     4 int main(void)
     5 {
     6     float area, circum, radius;
     7     printf("What's the radius of your pizza?
    ");
     8     scanf_s("%f", &radius);
     9     area = PI * radius*radius;
    10     circum = 2 * PI*radius;
    11     printf("Your basic pizza parameters are as follows:
     ");
    12     printf("circumference = %1.2f,area = %1.2f
    ", circum, area);
    13     system("pause");
    14     return 0;
    15 }

     const限定符

    const int = OLD_YEAR;//OLD_YEAR在程序里面不可修改

     明示常量

    #include<limits.h>
    #include<stdio.h>
    
    int main(void)
    {
        printf("%d
    ", INT_MAX);
        system("pause");
        return 0;
    }

     limits.h

    明示常量含义
    CHAR_BIT char类型的位数
    CHAR_MAX char类型的最大值
    CHAR_MIN char类型的最小值
    SCHAR_MAX signed char类型的最大值
    SCHAR_MIN signed char类型的最小值
    UCHAR_MAX unsiged char类型的最大值
    SHRT_MAX short类型的最大值
    SHRT_MIN short类型的最小值
    USHRT_MAX unsigned short类型的最大值
    INT_MAX int类型的最大值
    INT_MIN int类型的最小值
    UINT_MAX unsiged int的最大值
    LONG_MAX long类型的最大值
    LING_MIN long类型的最小值
    ULONG_MAX unsigned long类型的最大值
    LLONG_MAX long long类型的最大值
    LLONG_MIN long long类型的最小值
    ULLONG_MAX unsigned long long类型的最大值

    float.h

    明示常量含义
    FLT_MANT_DIG float类型的尾数位数
    FLT_DIG float类型的最少有效数字位数(十进制)
    FLT_MIN_10_EXP 带全部有效数字的float类型的最小负指数(以10为底)
    FLT_MAX_10_EXP float类型的最大正指数(以10为底)
    FLT_MIN 保留全部精度的float类型最小正数
    FLT_MAX float类型的最大正数
    FLT_EPSILON 1.00和比1.00大的最小float类型值之间的差值

    把明示常量名中的FLT分别替代成DBL和LDBL,即可分别表示double和long double类型对应的明示常量。

    printf()和scanf()和*修饰符

    如果不想预先指定字段宽度,希望通过程序来指定,那么可以用*修饰符代替字段宽度;如果转换符%*d,那么参数列表中应包含*和d对应的值

     1 #include<stdio.h>
     2 
     3 int main(void)
     4 {
     5     unsigned width, precision;
     6     int number = 256;
     7     double weight = 242.5;
     8     printf("Enter a field 
    ");
     9     scanf_s("%d", &width);
    10     printf("The number is:%*d:
    ", width, number);
    11     printf("Now enter a width and a precision:
    ");
    12     scanf_s("%d %d", &width, &precision);
    13     printf("Weight = %*.*f
    ", width, precision, weight);
    14     printf("Done!
    ");
    15     system("pause");
    16     return 0;
    17 }

     scanf()中*的用法与此不同,把*放在%和转换符之间时,会使得scanf()跳过相应的输入项。

     1 #include<stdio.h>
     2 int main(void)
     3 {
     4     int n;
     5     printf("Please enter three integers:
    ");
     6     scanf_s("%*d %*d %d", &n);
     7     printf("The last integer was %d
    ", n);
     8     system("pause");
     9     return 0;
    10 }
    11 
    12 result:
    13 Please enter three integers:
    14 1 2 3
    15 The last integer was 3

     printf()用法提示

     1 #include<stdio.h>
     2 int main(void)
     3 {
     4     int val_1 = 12, val_2 = 345, val_3 = 1222;
     5     printf("%9d %9d %9d
    ", val_1, val_2, val_3);//%nd设置字段宽度
     6     system("pause");
     7     return 0;
     8 }
     9 
    10 result:
    11        12       345      1222
  • 相关阅读:
    【BZOJ 3754】: Tree之最小方差树
    【cogs 775】山海经 ——Segment Tree
    【BZOJ 3626】 [LNOI2014]LCA【在线+主席树+树剖】
    【BZOJ 2004】: [Hnoi2010]Bus 公交线路
    开启22端口
    将MySQL数据库表结构,自动生成PDM方法
    linux环境 创建多版本php
    mysql 数据类型选择浅谈
    int(5) 到底是多长
    (记)小程序如何发布
  • 原文地址:https://www.cnblogs.com/MingleYuan/p/10588778.html
Copyright © 2011-2022 走看看