C语言基础学习笔记

来源:互联网 发布:手机淘宝怎么延长收货 编辑:程序博客网 时间:2024/04/23 17:16

1.变量,变量的输出

#include <stdio.h>int main(){//变量:只要有不确定的数据int score = 205;//变量输出要使用占位符,%d\%i是一个格式符(占位符)printf("分数是%d\n", score);float height = 1.78f;//%f用来输出小数,默认是6位小数,可以在前面加.来控制出输出几位小数。//printf("身高是%.2f\n", height);char scoreGrade ='B';//%c用来输出字符//printf("积分等级是%c\n", scoreGrade);return 0;}

字符串占位符是%s,指针占位符为%zd,指针的长度都为8位。

%P是地址占位符,int数组之间地址差值为4

 

2.代码风格:

printf(“积分等级%c\n”, score) 

逗号后面跟空格。

3.一个汉字是3个字节,一个字节占8位。

4.数组

#include <stdio.h>int main(){    // 使用注意    // 都是正确写法    //int ages[5] = {10 , 11, 12, 67, 56};    //int ages[5] = {10, 11};    //int ages[5] = {[3] = 10, [4] = 11};    //int ages[] = {10, 11, 14};        // 错误写法    // int ages[];        // 错误写法    /* 只能在定义数组的同时进行初始化    int ages[5];    ages = {10, 11, 12, 14};    */        // 正确写法    // int ages['A'-50] = {10, 11, 12, 14, 16};    //int size = sizeof(ages);    //printf("%d\n", size);        // 正确写法    /*    int count = 5;    int ages[count];    ages[0] = 10;    ages[1] = 11;    ages[2] = 18;    */        //printf();    // 错误写法    // 如果想再定义数组的同事进行初始化,数组元素个数必须是常量,或者不写    //int ages[count] = {10, 11, 12};            int ages[] = {10, 11, 12, 78};        // 计算数组元素的个数    int count = sizeof(ages)/sizeof(int);        for (int i = 0; i<count; i++)    {        printf("ages[%d]=%d\n", i, ages[i]);    }        return 0;}// 数组的基本使用void arrayUse(){    // 数组的定义格式: 类型 数组名[元素个数];    int ages[5] = {19, 29, 28, 27, 26};    // 19 19 28 27 26]    ages[1] = 29;        /*     ages[0] = 19;     ages[1] = 19;     ages[2] = 28;     ages[3] = 27;     ages[4] = 26;     */        /*     遍历:按顺序查看数组的每一个元素     */    for (int i = 0; i<5; i++)    {        printf("ages[%d]=%d\n", i, ages[i]);    }}

 

0 0
原创粉丝点击