C语言入门课程学习记录4
- 第18课 - signed 与 unsigned
- 第19课 - 再论数据类型
- 第20课 - 经典问题剖析
- 第21课 - 程序中的辅助语句(上)
- 第22课 - 程序中的辅助语句(下)
本文学习自狄泰软件学院 唐佐林老师的 C语言入门课程,图片全部来源于课程PPT,仅用于个人学习记录
第18课 - signed 与 unsigned
#include <stdio.h>
int main()
{
short s = 32767;// 0111 1111 1111 1111
int i = 2147483647;// 0111 1111 1111 1111 1111 1111 1111 1111
short ss = -32768;//1000 0000 0000 0000
int ii = -2147483648;//1000 0000 0000 0000 0000 0000 0000 0000
printf("s = %d\n", s);
printf("i = %d\n", i);
s = s + 1;//1000 0000 0000 0000
i = i + 1;//1000 0000 0000 0000 0000 0000 0000 0000
printf("after:s = %d\n", s);
printf("after:i = %d\n", i);
printf("ss = %d\n", ss);
printf("ii = %d\n", ii);
ss = ss - 1;//0111 1111 1111 1111
ii = ii - 1;// 0111 1111 1111 1111 1111 1111 1111 1111
printf("after:ss = %d\n", ss);
printf("after:ii = %d\n", ii);
return 0;
}
/*
output:
s = 32767
i = 2147483647
after:s = -32768
after:i = -2147483648
ss = -32768
ii = -2147483648
after:ss = 32767
after:ii = 2147483647
*/
#include <stdio.h>
int main()
{
unsigned char uc = 128;
char c = 128;
unsigned ui = 2147483648;
signed i = 2147483648;
printf("uc = %u\n", uc);
printf("c = %d\n", c);
printf("ui = %u\n", ui);
printf("i = %d\n", i);
return 0;
}
/*
output:
uc = 128
c = -128
ui = 2147483648
i = -2147483648
*/
第19课 - 再论数据类型
#include <stdio.h>
int main()
{
int c = sizeof(char);//1
int s = sizeof(short);//2
int i = sizeof(s);//4
int uc = sizeof(unsigned char);//1
int us = sizeof(unsigned short);//2
int ui = sizeof(unsigned int);//4
printf("c = %d\n", c);
printf("s = %d\n", s);
printf("i = %d\n", i);
printf("sizeof(float) = %d\n", sizeof(float));//4
printf("sizeof(double) = %d\n", sizeof(double));//8
printf("uc = %d\n", uc);
printf("us = %d\n", us);
printf("ui = %d\n", ui);
return 0;
}
/*
output:
c = 1
s = 2
i = 4
sizeof(float) = 4
sizeof(double) = 8
uc = 1
us = 2
ui = 4
*/
#include <stdio.h>
int main()
{
long l = -1l;
long long ll = 21474836481ll;
printf("l = %ld\n", l);
printf("sizeof(long) = %d\n", sizeof(long));
printf("ll = %lld\n", ll);
printf("sizeof(long long) = %d\n", sizeof ll);
return 0;
}
/*
output:
l = -1
sizeof(long) = 4
ll = 21474836481
sizeof(long long) = 8
*/
第20课 - 经典问题剖析
a=a+7 √
b=a+b;
a=b; √
a=(int)(12.3/4); √ 强制类型转换
a+7=b+1; × a+7不是变量
D
B
D
输出16
x<0 ,y=-1;
x=0时,y=0;
x>0时,y=1
此为错误答案
x=0,y=-1;
x>0,y=1;
x<0,y=0;
A,此为错误答案
D,else前漏了;
k 2 4
s 1 1+2 1+2+3 6+4 15 1 5 10
j 2 3 4 5 6 4 5 6
B
C
9 6 3
8 5 2
C
第21课 - 程序中的辅助语句(上)
#include <stdio.h>
int main()
{
int a = 0;
int b = 10;
a = b = b + 1; // a=>11 b=>11
printf("a = %d, b = %d\n", a, b);
a += 1;
a -= 2;
a *= 3;
a /= 4;
// a => 7
printf("a = %d\n", a);
/*
复合赋值与连续赋值相结合
*/
a += b -= 1;
printf("a = %d, b = %d\n", a, b);
return 0;
}
/*
output:
a = 11, b = 11
a = 7
a = 17, b = 10
*/
#include <stdio.h>
int main()
{
int a = 0;
int b = 10;
int c = 0;
a = (b < 10) ? 1 : 2;
printf("a = %d\n", a);
(a < 5) ? (a = 5) : (a = 0);
printf("a = %d\n", a);
/*
((a == 5) ? a : b) = 10; // ERROR
*/
a = b = c = 0;
c = (a = 2, b = 3, a + b);
printf("a = %d\n", a);
printf("b = %d\n", b);
printf("c = %d\n", c);
c -= a; // c = c - a;
printf("c = %d\n", c);
return 0;
}
/*
output:
a = 2
a = 5
a = 2
b = 3
c = 5
c = 3
*/
第22课 - 程序中的辅助语句(下)
#include <stdio.h>
int main()
{
int a = 0;
int c = 1;
a = -c++; // c -> 2
printf("a = %d\n", a);
//a = +++c; // +(++c); c -> 3//error: lvalue required as increment operand|
a = +(++c);
printf("a = %d\n", a);
a = -c--; // c -> 2
printf("a = %d\n", a);
printf("c = %d\n", c);
return 0;
}
/*
output:
a = -1
a = 3
a = -3
c = 2
*/
int main()
{
int a = 0;
int c = 1;
if( a + c > 0 )
{
printf("a = %d, c = %d\n", a, c);
goto ELSE;
}
else
{
ELSE: printf("%d + %d = %d\n", a, c, a + c);
goto FOR;
}
for(c=10; c>0; c--)
{
FOR:
printf("c = %d\n", c);
}
return 0;
}
/*
output:
a = 0, c = 1
0 + 1 = 1
c = 1
*/