有意思的一些东西

1 #include <stdio.h>
2 
3 int i = 1;
4 int main(){
5    int i = i;
6    printf("%d\n", i);
7 }

上面这一段小代码,展示了C语言定义的一些东西,打印出来的值应该是未定义的值。main函数之外的定义为全局变量,而main函数内的定义是局部变量。

 

2.

#include <iostream>
using namespace std;

int func(int x){
        int count = 0;
        while(x){
                count++;
                x = x & (x - 1);

        }

        return count;
}

int main(){
        cout << func(9999) << endl;
        return 0;
}

 这段代码其实就是检测输入值中包含二进制1的个数。

posted @ 2016-05-26 19:02  专属9号  阅读(161)  评论(0编辑  收藏  举报