C语言入门课程学习笔记2
- 第8课 - 四则运算与关系运算
- 第9课 - 逻辑运算与位运算
- 第10课 - 深度剖析位运算
- 第11课 - 程序中的选择结构
本文学习自狄泰软件学院 唐佐林老师的 C语言入门课程,图片全部来源于课程PPT,仅用于个人学习记录
第8课 - 四则运算与关系运算
#include <stdio.h>
int main()
{
int a = 5;
int b = 2;
double c = 3;
c = a / b;
printf("c = %f\n", c);
c = a % b;
printf("c = %f\n", c);
return 0;
}
/*
output:
c = 2.000000
c = 1.000000
*/
#include <stdio.h>
int main()
{
int a = 1;
int b = 2;
int c = 3;
c = a != b;//1
c = a - b >= a + b;//-1>=3 0
printf("c = %d\n", c);
c = (a < b) + (c < b);//1<2 0<2 2
printf("c = %d\n", c);
return 0;
}
/*
output:
c = 0
c = 2
*/
#include <stdio.h>
int main()
{
int a = 1;
int b = 2;
int c = 0;
c = a != b+a*b;//1!=3 1
printf("c = %d\n", c);
c = a == b < c== b;//0<0 0
printf("c = %d\n", c);
return 0;
}
/*
output:
c = 1
c = 0
*/
第9课 - 逻辑运算与位运算
#include <stdio.h>
int main()
{
int a = 1;
int b = 2;
int c = 0;
c = a && b; 1&&2 1
printf("c = %d\n", c);
c = !(a - b) || (c < b); !(-1)||(0<2) 0||1 1
printf("c = %d\n", c);
c = 10000;
c = !!c;
printf("c = %d\n", c); !0 1
return 0;
}
/*
output:
c = 1
c = 1
c = 1
*/
#include <stdio.h>
int main()
{
printf("c = %d\n", 5 | 2); //101|10 111 7
printf("c = %d\n", 7 ^ 8); //111^1000 1111 15
printf("c = %d\n", 2 ^ 3); //10^11 01 1
printf("c = %d\n", (15 >> 2) & 13); //11&1101 1
printf("c = %d\n", 173 ^ 60); //1010 1101^0011 1100 1001 0001 145
return 0;
}
/*
output:
c = 7
c = 15
c = 1
c = 1
c = 145
*/
第10课 - 深度剖析位运算
#include <stdio.h>
int main()
{
short a = 1;
short b = 2;
int c = a - b;//-1 1111 1111 1111 1111 1111 1111 1111 1111
c = c >> 4;// -1
printf("c = %d\n", c);
c = c * -1 * 16 >> 4; //... ... 0001 0000 1
printf("c = %d\n", c);
printf("c = %d\n", 16 << 2); //64
return 0;
}
/*
output:
c = -1
c = 1
c = 64
*/
#include <stdio.h>
int main()
{
char c = 'A';
short a = c;
int b = c;
printf("c = %c\n", c);//A
printf("c = %d\n", c);//65
printf("a = %d\n", a);//65
printf("b = %d\n", b);//65
c = 0x40;
printf("c = %x\n", c);//40
printf("c = %d\n", c);//64
c = c << 1;
printf("c = %d\n", c);//1000 0000 -128
c = c << 1;
printf("c = %d\n", c);// 0000 0000 0
return 0;
}
/*
output:
c = A
c = 65
a = 65
b = 65
c = 40
c = 64
c = -128
c = 0
*/
补充点:原码、反码及补码,十进制、二进制、十六进制之间的转换
第11课 - 程序中的选择结构
#include <stdio.h>
int main()
{
short a = 1;
short b = 2;
int c = a - b;
if( c > 0 )
{
printf("a > b\n");
}
else
{
printf("a <= b\n");
}
return 0;
}
/*
output:
a <= b
*/
#include <stdio.h>
int main()
{
short a = 2;
short b = 2;
int c = a - b;
if( c > 0 )
printf("a > b\n");
else if( c == 0 )
printf("a == b\n");
else
printf("a < b\n");
return 0;
}
/*
output:
a == b
*/