C语言入门笔记代码(第二天)

C语言入门笔记代码如下所示:

主要包括:

1.整数上溢,浮点上溢和下溢;

2.如何使用转义序列;

3.scanf()读取机制;

4.strlen()函数与sizeof语句区别;

5.关键字:const;

6.如何创建字符串和存储字符串;

7.print()修饰符和返回值

#include <stdio.h>

/************************1********************************/
//整数上溢,浮点上溢与浮点下溢
/* #include <limits.h>         //INT_MAX,INT_MIN
#include <float.h>          //FLT_MAX,FLT_MIN
//limits.h和float.h提供了相关整数类型和浮点类型的大小限制的详细信息

int main(void)
{

    // int i = 2147483647;      //i = INT_MAX
    float a = 3.402823e38 * 100.0f;     //浮点上溢->打印INF0
    float b = -1.175494e-38 /100.0f;     //浮点下溢->精度缺失

    // printf("INT_MAX = %d,INT_MIN = %d.\n",INT_MAX,INT_MIN);     //INT_MAX = 2147483647,INT_MIN = -2147483648.
    // printf("INT_MAX = %d,INT_MAX + 1 = %d,INT_MAX + 2 = %d.\n",i,i+1,i+2);

    // printf("FLT_MAX = %e,FLT_MIN = %e.\n",FLT_MAX,FLT_MIN);     //FLT_MAX = 3.402823e+038,FLT_MIN = 1.175494e-038.
    printf("FLT_MAX * 100.0f = %e,FLT_MIN / 100.0f = %e.\n",a,b);   //FLT_MAX * 100.0f = 1.#INF00e+000,FLT_MIN / 100.0f = -1.175493e-040.

    return 0;
} */
/************************1********************************/

/************************2********************************/
//输入一个ASCII码数值(例如65),输出相应字符(65->A)
/* int main(void)
{
    int ASCII;

    printf("Please enter an ASCII code:");
    scanf("%d",&ASCII);
    printf("%d is the ASCII code for %c\n",ASCII,ASCII);

    return 0;    
} */
/************************2********************************/

/************************3********************************/
//发出警报,如何打印双引号“”
/* int main(void)
{

    printf("\a");       //cmd指令操作可听到报警声

    printf("Startled by the sudden sound,sally shouted,");
    printf(" \"By the great pumkin.what was that!\"\n");

    return 0;    
} */
/************************3********************************/

/************************4********************************/
//一年有3.156x10^7s,输入年龄,显示年龄合该多少秒
/* int main(void)
{

    int years;
  
    printf("Pleasr enter your years old : ");
    scanf("%d",&years);
    printf("%d years old is %le second",years,3.156e7*years);

    return 0;
} */
/************************4********************************/

/************************5********************************/
//scanf()读取机制
/* int main(void)
{

    char name[40];

    printf("what's your name?\n");
    scanf("%s",name);   //字符串即数组本身代表的就是地址,不需要操作符
    printf("hello,%s\n",name);  // scanf会在遇到第一个空白字符(blank),制表符或者换行符处会停止读写。
    //输入songzuer,打印“hello,songzuer”
    //输入song zuer,打印“hello,song”
    return 0;
} */
/************************5********************************/

/************************6********************************/
//数组最后一位显示字符\0,\0是一个非打印空字符,即屏幕打印PRAISE字符串时,实际上所占空间大小为PRAISE\0(28 +1 = 29)
/* #include <string.h> //string包含许多与字符串相关的函数原型,其中包括strlen()
#define PRAISE "what a super marvelous name!"   //预处理,全局变量
//宏定义一定使用大写字母做全局变量

int main(void)
{

    char name[40];

    //const关键字使MONTHS成为了一个只读值,意味着不可改变MONTHS的值。
    const int MONTHS = 12;//const关键字把一个变量声明转换成常量声明,与#define宏定义类似,因此变量名称大写

    printf("what's your name?\n");
    scanf("%s",name);  //songzuer
    printf("hello,%s\n",name);

    //sizeof()可以使用圆括号,也可以不使用,是否使用圆括号取决于你是想获取一个类型的大小,还是想获取某个具体量的大小,一般建议都是用圆括号,以免混淆
    //圆括号对于类型是必须的,例如sizeof(int)获取int类型的大小,但sizeof name却不行
    //圆括号对于具体量大小是可选的,例如sizeof name和sizeof(name)都是可以的
    printf("your name of %d letters occupies %d memory\n",strlen(name),sizeof(name));
    //your name of 8 letters occupies 40 memory

    //strlen()函数返回值是字符串大小,而sizeof()运算符返回值是字符串变量占用空间大小
    printf("the PRAISE has %d letters occupies %d memory\n",strlen(PRAISE),sizeof(PRAISE));
    //the PRAISE has 28 letters occupies 29 memory
    return 0;
} */
/************************6********************************/

/************************7********************************/
//printf()修饰符
//printf()修饰符转换意义:转换实际上是翻译说明,不存在用转换后的值代替原值,是将存储在计算机中二进制格式数值转换为一系列字符以便显示
/* #define PAGES 931
#define BLURB "Hello world!"

int main(void)
{

    const double RENT = 3852.99;

    printf("PAGES = *%d*\n",PAGES);   //PAGES = *931*

    printf("PAGES = *%2d*\n",PAGES);//PAGES = *931*,字段宽度的最小值,如果该字段不能容纳要打印的数或字符串,系统会自动使用更宽的字段

    printf("PAGES = *%10d*\n",PAGES);//PAGES = *       931*,右对齐

    printf("PAGES = *%-10d*\n",PAGES);//PAGES = *931       *,左对齐

    
    printf("RENT = *%f*\n",RENT);//RENT = *3852.990000*,默认小数点后6位

    printf("RENT = *%e*\n",RENT);//RENT = *3.852990e+003*,小数点后6位有效数字

    printf("RENT = *%4.2f*\n",RENT);//RENT = *3852.99*,4代表数据宽度,不够系统自动使用更宽字符,.2代表数据精度

    printf("RENT = *%3.1f*\n",RENT);//RENT = *3853.0*,发生四舍五入动作,导致数据精度缺失

    printf("RENT = *%10.3f*\n",RENT);//RENT = *  3852.990*,右对齐

    printf("RENT = *%10.3e*\n",RENT);//RENT = *3.853e+003*,发生四舍五入动作,导致数据精度缺失

    printf("RENT = *%+4.2f*\n",RENT);//RENT = *+3852.99*

    printf("RENT = *%010.2f*\n",RENT);//RENT = *0003852.99*

    
    printf("31 = *%x*\n",31); //0x31 = *1f*

    printf("31 = *%#x*\n",31);//31 = *0x1f*

    printf("31 = *%X*\n",31);//31 = *1F*

    printf("31 = *%d*,*% d*,*% d*\n",31,31,-31);//31 = *31*,* 31*,*-31*,有符号的值若为正,显示时带前导空格,若为负值,则带减号符号

    //如果0标志和精度说明符同时出现,产生冲突时,那么0标志就会被忽略,如下例的*%05.3d*
    printf("6 = *%5d*,*%5.3d*,*%05d*,*%05.3d*\n",6,6,6,6);//6 = *    6*,*  006*,*00006*,*  006*

    
    printf("BLURB = *%s*,*%2s*\n",BLURB,BLURB);//BLURB = *Hello world!*,*Hello world!*

    printf("BLURB = *%24s*\n",BLURB);//BLURB = *            Hello world!*,右对齐

    printf("BLURB = *%24.5s*\n",BLURB);//BLURB = *                   Hello*,有效数据5位,所以只打印hello
    
    printf("BLURB = *%-24.5s*\n",BLURB);//BLURB = *Hello                   *,左对齐

    return 0;
} */


/************************8********************************/
//printf()的返回值是其打印输出用途的附带功能,它返回的的值是所打印的字符的数目
/* int main(void)
{

    int i;
    i = printf("hello world!\n");

    printf("%d\n",i);//13

    return 0;
} */

猜你喜欢

转载自blog.csdn.net/sinat_41653350/article/details/109503655