c语言入门教程 第3讲 运算符及表达式

// 算术运算符

#include <stdio.h>

main()
{
    int i = 7, j = 8;

    printf("i+j=%dn", i + j ); //15
    printf("i-j=%dn", i - j ); //0
    printf("i*j=%dn", i * j ); //56
    printf("i/j=%dn", i / j ); //0
    printf("i%%j=%dn", i % j );//7

    i++;
    printf("i++后i的值为%dn",i);  //8
    printf("++i后i的值为%dn",++i);//9

    printf("j--后j的值为%dn",j--);//8
    printf("--j后j的值为%dn",--j);//6
}
// 关系与逻辑运算符

#include <stdio.h>

main()
{
    int Alan = 18, Sam = 18, Jack = 16;

    printf("%dn", Alan > Jack ); // 1
    printf("%dn", Sam  < Jack ); // 0     
    printf("%dn", Alan != Sam ); // 0
    printf("%dn", Alan == Sam ); // 1
    printf("%dn", Alan >= Sam ); // 1
    printf("%dn", (Sam>Jack) && (Sam>Jack) ); //1
    printf("%dn", Alan > Jack );//1
    printf("%dn", ! Sam );    //0
}

 

/*
    数据类型转换
    形式:
    (类型) (表达式)
*/

#include <stdio.h>

main()
{
    int chinese, math, english, sum;

    printf("请输入语数英三科的成绩:(以空格隔开)n");
    scanf("%d%d%d", &chinese, &math, &english);

    sum = (chinese + math + english);

    printf("平均分为%dn", sum/3);
    printf("平均分为%fn", (float)sum/3);
}

需要注意的是“=”与“==”之间的区别:
“=”是赋值运算
“==”是逻辑运算

#include <stdio.h>

main()
{
    int i = 5, b = 10;

    printf(" a=b 值为 %dn", a = b ); //10
    printf("a==b 值为 %dn", a == b); //0

}

c语言入门教程 全文索引

下一讲 第4讲 选择结构与循环结构

Advertisements

5 thoughts on “c语言入门教程 第3讲 运算符及表达式

  1. 请问下为什么正确输出是1错误是0呢?可不可以把1和0直接换成正确错误?新人诚心求教

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google+ photo

You are commenting using your Google+ account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s