C语言中易错和难理解的题目

前言

这是一些给专升本的同学讲题目的时候遇到的一些比较难于理解的题。总结在此,便于查阅和应试。

二进制和原反补码

//=========================int 16==================================
	//计算机中的存储(补码):[1]111 1111 1111 1111
    //反码:                   1 111 1111 1111 1110
	//原码:                   1 000 0000 0000 0001
	//=========================int 32==================================
	//补码:0000 0000 0000 0000 1111 1111 1111 1111
	//转八进制:
	//0000 0000 0000 00(00 1)(111) (111)(111)(111)(111)
	//                    1    7
	//
	//看十进制:+65535
	//a= 65535, 177777, ffff, 65535
	//unsigned int a = 65535; 
	//printf("a = %d, %o, %x, %u\n", a, a, a, a);
	//2: 10
	//原码:[]000 0000 0000 0000 0000 0000 0000 0010
	//反码:[]111 1111 1111 1111 1111 1111 1111 1101
	//补码: 1111 1111 1111 1111 1111 1111 1111 1110
	//-2的补码(计算机内存中存储形式):1111 1111 1111 1111 1111 1111 1111 1110
	//二进制转八进制:(011)(111)(111)(111)(111)(111)(111)(111)(111)(111)(110)
	//                   3       7                                                              6 
	//二进制转十六进制:
	//二进制转十进制:按权展开
	//b = -2, 37777777776, fffffffe, 4294967294
	//int b = -2;
	//printf("b = %d, %o, %x, %u\n", b, b, b, b);

二进制移位

	printf("%d\n", 010);
	// 1000  10000
	printf("%d\n", 010 << 1);
	//10000 00001 == 10001
	printf("%d\n", 010 << 1 ^ 1);
	//17
	printf("%o\n", 010 << 1 ^ 1);
	//17
	printf("%x\n", 010 << 1 ^ 1);

指针常量

//int a[4];
	//printf("%d\n", *a);
	//printf("%d\n", a[0]);
	//printf("%d\n", a);
	//a + 1;
	//a是指针常量,本身是不能做++操作的
	a++;
	//printf("%d\n", a);

转义符

1

	char s[] = "\\ 1 4 1 \141 a b c \t";
	//缺少右引号
	//char s1[] = "\";
	char s2[] = "\\";
	char s3[] = "\141";
	char s4[] = "\t";

	printf("%d\n", strlen(s));//9
	printf("%d\n", strlen(s2));//1
	printf("%d\n", strlen(s3));//1
	printf("%d\n", strlen(s4));//1
	//这里的141是八进制,所以就是十进制的97,即字符a
	printf("%c\n", '\141'); //(a)

2

printf("ab c\t de\rf\tg\n");
printf(" h\ti\b\bj k\n");
f       gde
 h     j k

解析:
\r 回车,是到行首
\b 退格

字符串、数组和指针

(*p)++和 *(p++)和*p++的区别

例如

int arr[5] = { 1,3,5,7,9 };
int *p = arr;
*++p:p先自+,然后*p,最终为3
++*p:先*p,即arr[0]=1,然后再++,最终为2
*p++:值为arr[0],即1,该语句执行完毕后,p指向arr[1]
(*p)++:先*p,即arr[0]=1,然后1++,该语句执行完毕后arr[0] =2
*(p++):效果等同于*p++

再例如

    const char* a[2] = { "one", "two" }, **p = a;
	printf("the result of *p is : %s\n", *p);
	printf("the result of **p is : %c\n", **p);
	printf("the result of (*p)++ is : %s\n", (*p)++);
	printf("the result of *(p+1) is : %s\n", *(p+1));
	printf("the result of *p+1 is : %s\n", *p + 1);
	//(1) *p + 1
	//(2) p++
	//printf("the result of *(p++) + 1 is : %s,", *(p++) + 1);
	printf("the result of *(++p) + 1 is : %s,", *(++p) + 1);
	printf("the result of **p-1 is : %c\n", **p-1);    

结构体和枚举

1

  • 题目
    在这里插入图片描述
  • 解析
#include <stdio.h>
#include <iostream>
using namespace std;

enum color 
{ 
	red, yellow, blue = 4, green, white 
}c1;


int main()
{
	c1 = red;
	printf("%d", c1);
	c1 = yellow;
	printf("%d", c1);

	c1 = blue;
	printf("%d", c1);

	c1 = green;
	printf("%d", c1);

	c1 = white;
	printf("%d", c1);

	cin.get();
	return 0;
}

输出结果

01456
原创文章 289 获赞 52 访问量 33万+

猜你喜欢

转载自blog.csdn.net/kaikai_sk/article/details/105919845