第五章--基本概念
来源:互联网 发布:免费域名邮箱 编辑:程序博客网 时间:2024/06/06 02:26
1.1 赋值语句
例题1:
int i = 1;
int main() {
int i = i;
return 0;
}
//p29 该程序编译通过,但有执行错误:the variable 'i' is being used without being initialized.
例题2:
int x=2, y, z;
int *p = NULL;
p = &x;
x *=(y=z=5);
printf("x1 is %d\n", x);
//print one pointer's value.
printf("p2 is %p\n", p);
z=3;
x==(y=z);
printf("x3 is %d\n", x);
x=(y==z);
printf("x4 is %d\n", x);
x=(y&z);
printf("x5 is %d\n", x);
x=(y&&z);
printf("x6 is %d\n", x);
y=4;
x=(y|z);
printf("x7 is %d\n", x);
x=(y||z);
printf("x8 is %d\n", x);
return 0;
}
由于X3之前是比较表达式,并不会改变X的值,所以其值仍然是10.
X4是条件判断表达式(y==z)的值.
X5是位与操作。X6是与操作。X7是位或操作。X8是或操作。
例题3:
int func(int x) {
int count = 0;
while(x){
count++;
x=x&(x-1);
}
return count;
}
int main() {
int ret=0;
ret=func(5);
printf("ret is %d\n", ret);
return 0;
}
//P31 你可以举出个例子来试下。
1.2 i++
例题1:
int a,x;
//! and ++ is same level priority. but x++ is x used first and ++
for(a=0,x=0;(a<=1)&&(!x++);a++){
a++;
}
printf("a is %d\n", a);
printf("x is %d\n", x);
return 0;
}
// a is 2; x is 1.
int main() {
int a,x;
//! and ++ is same level priority.. but x++ is x used first and ++
for(a=0,x=0;(a<=1)&&(!x++);){
a++;
}
printf("a is %d\n", a);
printf("x is %d\n", x);
return 0;
}
// P31主要几个知识点:优先级的判断;与操作;X++使用与自增的先后顺序;for循环(表达式1;表达式2;表达式3){执行主体}.
与操作:当与操作的前面表达式已经为假时,不执行与操作后面的表达式;当与操作的前面表达式已经为真时,才执行后面的表达式。
for循环:先执行表达式1,当满足表达式2时,才执行主体,然后执行表达式3;当不满足表达式2时,直接执行表达式3,不断循环。
例题2:
int main() {
int b=3, i = 0, len = 0;
int arr[]={6,7,8,9,10};
len = sizeof(arr)/sizeof(int);
//pointer array.
int *m_ptr[5];
//pointer which points to the int array having five elements.
int (*n_ptr)[5];
//arr is arr[0]; &arr is entire array.
printf("the address of arr is %p\n", arr);
printf("the address of &arr[0] is %p\n", &arr[0]);
printf("the address of arr+1 is %p\n", arr+1);
printf("the address of &arr is %p\n", &arr);
printf("the address of &arr+1 is %p\n", &arr+1);
for(i=0; i < len; i++) {
m_ptr[i] = &arr[i];
printf("*m_ptr[i] is %d\n",*m_ptr[i]);
}
//n_ptr points to entire array.
n_ptr = &arr;
printf("*n_ptr is %p\n",*n_ptr);
printf("**n_ptr is %d\n",**n_ptr);
for(i=0; i < len; i++) {
printf("*(*n_ptr+i) is %d\n",*(*n_ptr+i));
}
//one pointer points to first address of array arr.
int *ptr=arr;
*(ptr++)+=123;
printf("*ptr is %d,*(++ptr) is %d\n", *ptr, *(++ptr));
return 0;
}
明白数组arr与&arr的区别;
函数参数的执行顺序:从右往左。
1.3 类型转换
例题1
unsigned int a = 0xfffffff7;
printf("a: %p\n", &a);
// char type conversion.
unsigned char i = (unsigned char)a;
//detail explation is following lines. just pointer conversion(not type conversion).
char* b = (char*)&a;
printf("b: %p\n", b);
printf("b+1: %p\n", b+1);
//following six lines is the same as above one line.
unsigned int *p = &a;
printf("p: %p\n", p);
printf("p+1: %p\n", p+1);
char *c = (char *)p;
printf("c: %p\n", c);
printf("c+1: %p\n", c+1);
printf("i: %08x, *b: %08x", i, *b);
return 0;
}
例题2
int main(){
float a = 1.0f;
printf("(int)a: %d\n", (int)a);
printf("&a: %p\n", &a);
printf("(int &)a: %p\n", (int &)a);
float b = 0.0f;
printf("(int)b: %d\n", (int)b);
printf("&b: %p\n", &b);
printf("(int &)b: %p\n", (int &)b);
return 0;
}
//detail sees the website: http://www.cnblogs.com/from20100719/articles/1845238.html?ADUIN=598235904&ADSESSION=1416051763&ADTAG=CLIENT.QQ.5365_.0&ADPUBNO=26405
1.4 运算符问题
例题1:判断一个数是否是2的N次方
int x = 0;
scanf("%d", &x);
if (!(x&(x-1))) {
printf("input data is 2^N.\n");
}
else {
printf("input data is not 2^N.\n");
}
return 0;
}
例题2
return(x&y)+((x^y)>>1);
}
int main() {
int x = 0, y = 0, z = 0;
scanf("%d%d", &x, &y);
z = f(x, y);
printf("z: %d\n", z);
return 0;
}
1.5 a b 交换
例题1:找出两个数之间比较大的那个数:
int main() {
int x = 0, y = 0, max = 0;
scanf("%d%d", &x, &y);
max = ((x+y)+abs(x-y))/2;
printf("max: %d\n", max);
return 0;
}
例题2:a b 值交换,不使用中间变量
int x = 0, y = 0;
scanf("%d%d", &x, &y);
printf("x= %d, y = %d\n", x, y);
x = x^y;
y = x^y;
x = x^y;
printf("x= %d, y = %d\n", x, y);
return 0;
}
- 第五章--基本概念
- 程序员面试宝典第五章 程序设计基本概念
- 第五章-程序设计基本概念(1)
- 第五章-程序设计基本概念(2)
- 第五章 STL 基本概念、相关函数使用总结
- 程序员面试宝典(第四版)——读书笔记-1、第五章:程序设计基本概念
- 《机器学习实战》第五章:Logistic回归(1)基本概念和简单实例
- 第五章
- 第五章
- 第五章
- 第五章
- 第五章
- 第五章
- 第五章
- 第五章
- 第五章
- 第五章
- 第五章
- 再谈 X-UA-Compatible 兼容模式
- 《与曹仁超对话》——MILK
- 直接拿来用!最火的Android开源项目(一)
- OSGi 和 Spring,第 1 部分: 使用 Apache Felix 构建和部署 OSGi 包
- Servlet技术开发
- 第五章--基本概念
- php 编码规范
- 《Machine Learning(Tom M. Mitchell)》读书笔记——4、第三章
- 《Java Generics and Collections》笔记-Queue II
- 【Android工具类】DES加密时代的终结者——AES加密算法
- 应聘时最漂亮的回答(1)
- 应聘时最漂亮的回答(2)
- 第1篇:swift语言笔记:switch关键点
- [iOS-GQiang]AsyncSocket