C语言第二课——运算符与格式化输出

复习巩固提高

  1. C语言的基本书写规则:(1)语句以“;”结束;(2)main函数是程序入口,每个应用程序有且只有一个main函数;(3)括号必须前后对应;(4)一般一个语句一行。
  2. 标识符 程序中的名字。必须以字母或者下划线开头,可以包含字母、下划线、数字,区分大小写。注意避开关键字:auto, break, case, char, const, continue, default, do, double, else,enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof,static, struct, switch, typedef, unsigned, union, void, volatile, while, _Bool,_Complex, _Imaginary, inline, restrict, _Alignas, _Alignof, _Atomic _Generic,_Noreturn, _Static_assert, _Thread_local
  3. C语言中的基本变量类型:int, float, double, char。不常用void。新增auto。扩展类型后续学习。
  4. 输入输出函数:scanf()printf()
  5. 引用库——包含头文件:#include <>,#include ""
  6. 扩展练习 计算三角函数。
    #include 
    #include 

    int main()
    {
        float jiaoDu, huDu;         //用于存储角度
        float sinF, cosF, tanF;     //用于存储结果
        const float PI = 3.1415926; //作为常数π

        printf("请输入一个角度:"); //提示用户输入
        scanf("%f", &jiaoDu);       //读入用户输入

        huDu = PI * jiaoDu / 180; //将角度转换为弧度
        sinF = sin(huDu);         //计算正弦函数
        cosF = cos(huDu);         //计算余弦函数
        tanF = tan(huDu);         //计算正切函数

        printf("sin(%f) = %f\n", jiaoDu, sinF); //输出正弦函数结果
        printf("cos(%f) = %f\n", jiaoDu, cosF); //输出余弦函数结果
        printf("tan(%f) = %f\n", jiaoDu, tanF); //输出正切函数结果

        //  下面的代码是限制小数位数输出的
        printf("sin(%1.2f) = %1.3f\n", jiaoDu, sinF);
        printf("cos(%1.2f) = %1.3f\n", jiaoDu, cosF);
        printf("tan(%1.2f) = %1.3f\n", jiaoDu, tanF);
        return 0;
    }

运算符与格式化输出

运算符

  1. 算术运算符 +, -, *, /, %(取余), ++(自增1), --(自减1)。
  2. 关系运算符 ==(等于), !=(不等于), >, >=, <, <=
  3. 逻辑运算符 &&(与), ||(或), !(非)。
  4. 位运算符 &, |, ^(位异或), ~(位取反), <<(位左移), >>(位右移)。
  5. 赋值运算符 =, +=, -=, *=, /=, %=, <<=, >>=, &=, |=, ^=
  6. 杂项运算符 sizeof()(返回变量所占内存大小), &(递址符), *(指针), ?:(条件表达式)。

格式化输出

  1. 各种数据的格式化输出 %d 十进制有符号整数占位符,%u 十进制无符号整数,%f小数占位符,%c字符占位符,%s字符串占位符,%e 指数形式的浮点数占位符,%x%X 无符号以十六进制表示的整数,%g 自动选择合适的表示法
  2. 位宽与小数数量与对齐 %和类型符号中间的第一组数为位宽,.后的数位小数宽度。位宽前加-为左对齐。
  3. 特殊字符 \n 换行,\f 清屏并换页,\r 回车,\t 制表符。

算术运算符练习

#include 

int main()
{
    int a = 21;
    int b = 10;
    int c;

    printf("a = %d, b = %d\n", a, b);

    c = a + b;
    printf("a + b = %d\n", c);

    c = a - b;
    printf("a - b = %d\n", c);

    c = a * b;
    printf("a * b = %d\n", c);

    c = a / b;
    printf("a / b = %d\n", c);

    c = a % b;
    printf("a %% b = %d\n", c);

    a++;//a自增1
    printf("a++ = %d\n", a);

    b--;//b自减1
    printf("b-- = %d\n", b);

    return 0;
}

关系运算练习

#include 

//下面这个函数的作用是返回真(T)假(F)
char isTrue(int v)
{
    if (v)
        return 'T';
    else
        return 'F';
}


int main()
{
    int a = 21;
    int b = 0;
    int c = -1;
    char d = 'T';
    float e = 3.14;
    double f = 0.02;
    int r;
    printf("a = %d\tb=%d\tc=%d\td=%c\te=%f\tf=%f\n\n", a, b, c, d, e, f);

    //下面的例子说明了各类类型的数据与布尔数据的对应关系
    printf("各类类型的数据与布尔数据的对应关系:\n");
    printf("a = %c\n", isTrue(a));
    printf("b = %c\n", isTrue(b));
    printf("c = %c\n", isTrue(c));
    printf("d = %c\n", isTrue(d));
    printf("e = %c\n", isTrue(e));
    printf("f = %c\n", isTrue(f));

    printf("数据比较:\n");
    r = a == b;
    printf("a = b ? %c\n", isTrue(r));

    r = a >= b;
    printf("a >= b ? %c\n", isTrue(r));

    r = a > b;
    printf("a > b ? %c\n", isTrue(r));

    r = a <= b;
    printf("a <= b ? %c\n", isTrue(r));

    r = a < b;
    printf("a < b ? %c\n", isTrue(r));

    r = a != b;
    printf("a != b ? %c\n", isTrue(r));

    return 0;
}

逻辑运算符练习

#include 

int main()
{
    int a = 999;
    int b = 0;

    printf("a = %d\tb=%d\n\n", a, b);

    printf("a && b = %d\n", a && b);
    printf("a || b = %d\n", a || b);
    printf("!a = %d\n", !a);
    printf("!b = %d\n\n", !b);

    int i = 5, j = 8, k = 10, r;
    printf("i = %d\tj = %d\tk = %d\n", i, j, k);
    r = (i + j) > k && k > j;
    printf("i + j > k 且 k > j ? %d",r);
    return 0;
}

位运算符练习

#include 

int main()
{
    int a = 3;
    int b = 8;
    int c;

    printf("a = %d\tb = %d\n\n", a, b);

    c = a & b;
    printf("a & b = %d\n", c);

    c = a | b;
    printf("a | b = %d\n", c);

    c = a ^ b;
    printf("a ^ b = %d\n", c);

    c = ~a;
    printf("~a = %d\n", c);

    c = a << 2;
    printf("a << 2 = %d\n", c);

    c = a >> 1;
    printf("a >> 1 = %d\n", c);

    return 0;
}

赋值运算符练习

#include 

int main()
{
    int a = 3;
    int b = 8;
    int c;

    printf("a = %d\tb = %d\n\n", a, b);

    printf("a += b = %d\n", a += b);
    printf("a -= b = %d\n", a -= b);
    printf("a *= b = %d\n", a *= b);
    printf("a /= b = %d\n", a /= b);
    printf("a %= b = %d\n", a %= b);

    return 0;
}

杂项运算符练习

#include 

int main()
{
    int a = 3;
    int b = 8;
    char c = 'k';
    float f = 0.618;
    double d = 2019.0415;

    printf("a = %d\tb = %d\tc = %c\tf = %f\td = %f\n\n", a, b, c, f, d);
    printf("a = %8d\n", a);
    printf("b = %8d\n", b);
    printf("b = %-8d\n", b);
    printf("c = %8d\n", c);
    printf("f = %8.3f\n", f);
    printf("d = %8.2f\n", d);
    printf("d = %g\n\n", d);

    printf("size of a :%d\n", sizeof(a));
    printf("size of c :%d\n", sizeof(c));
    printf("size of f :%d\n", sizeof(f));
    printf("size of d :%d\n\n", sizeof(d));

    c = a > b ? 'T' : 'F';
    printf("a > b ?\t%c\n", c);

    c = a < b ? 'T' : 'F';
    printf("a < b ?\t%c\n", c);

    return 0;
}

你可能感兴趣的:(C语言第二课——运算符与格式化输出)