标识符的认识

重新认识了很多的标识符,其实之前大多的代码里都有所体现,有少数没有用过的,我着重的进行了记忆,并且实现了一些例题;

左移右移标识符

#include <stdio.h>
int main()
{
    int num = 0;
    //找出num中的二进制位中的1的个数
    scanf("%d", &num);
    int i = 0;
    int count = 0;
    for (i = 0; i < 32; i++)
    {
        if (1 == ((num >> i) & 1))

            count++;
    }
    printf("%d", count);
    return 0;   
}

以上表现了右移标识符的一种用法,右移标识符只是将数字对应的二进制位进行右移,左移标识符择优算数左移和逻辑左移,编译器中的是算术左移,会保留符号位,数据存储的时候都是存为补码的形式,正数原码等于补码,负数的补码等于原码按位取反再加一。

结构体构建的标识符

struct Stu
{
    char name[20];
    int age;
    char id[20];
};
#include <stdio.h>
int main()
{
    struct Stu s1 = { "张三",20,"2020012111" };
    printf("%s\n", s1.name);
    printf("%d\n", s1.age);
    printf("%s\n", s1.id);

    struct Stu* ps = &s1;
    printf("%s\n", (*ps).name);

    printf("%s\n", ps->name);
    //->  结构体指针指向操作符
    return 0;
}

其中的指向地址的操作符明显比直接用名称打印方便多了。

标识符的错误用法

#include <stdio.h>
int main()
{
    char a = 127;
    char b = 3;
    char c = a + b;
    printf("%d\n", c);
    int x = 1;
    printf("%d\n", x + --x);//无意义的算式

    return 0;
}

在不同的编译器中会有不同的结果

#include <stdio.h>
int main()
{
    int i = 1;
    int a = (++i) + (++i) + (++i);
    printf("%d  %d\n", a, i);
    return 0;
}

要通过操作符确定唯一的计算路径,那么这个表达式才是正确的。所以上面的代码时错误的,在 Linux系统中的执行结果为10

猜你喜欢

转载自blog.51cto.com/15078858/2601450