C语言的一些细节

MySQL专栏

有没有要学MySQL的后期开个专栏,进行系统性更新.(๑>ڡ<)☆
在这里插入图片描述

细节更新

一 const限定

const可以限制变量只能读,不能写.一些教材说是定义常量实际上是不对的.
const对变量起一个保护作用, C语言是支持修改的.
demo:使用指针间接修改

int main(void)
{
    
    
	const int index = 99;
	int *p = (int*)&index;

	*p = 32;
	printf("%d \n", index);

	return 0;
}插入代码片

运行:
结果
这个操作只有C才可以C++是不支持的但是可以编译通过
那么这个const主要干什么呢?
用来保护函数的参数

int multiplication(const int a,const int b)
{
    
    
	return a * b;
}

//写函数和用函数的是两类人 保证安全使用const

二 比较操作

#include <stdio.h>
int main(void)
{
    
    
	if (-1 < (unsigned int)1)
		printf("<\n");
	else
		printf(">\n");
		
	return 0;
}

运行:
运行
为什么-1比1大 ???
因为在比较的时候不同常量总是向大的类型转换

-1默认是int ,默认无符号高于int 所以在比较时 -1转成了无符号整型

if (-1 < (unsigned char)1)
		printf("<\n");
	else
		printf(">\n");

运行:
运行

-整型比unsigned char大所以转int 比较

三.字符串新写法

int main(void)
{
    
    
	const char* str[] = {
    
    
		"(*^▽^*)"
		"o(´^`)o"
		"( ̄▽ ̄)~*"
	};

	printf("一共有%d个表情\n", sizeof(str) / sizeof(str[0]));
	printf("%s",str[0]);

	return 0;
}

运行:
在这里插入图片描述
不是bug.
没有计算出三个,因为这是一种新试写法,多个字符串常量分行写自动拼接成一个字符串
在以前的老版本中要使用续航符才能连接,写在几行上.

符号 释义
\ 续航
\n 换行
#define 宏定义
int main(void)
{
    
    
	const char* str[] = {
    
      
		"(*^▽^*) \
		o(´^`)o \
		( ̄▽ ̄)~*"
	};
	return 0;
}

**续航符在宏定义中经常出现**
请添加图片描述

四.switch

#include <stdio.h>

int main(){
    
    
 
	int _char = 'C';
	switch (_char)
	{
    
    
	case 'A':
		printf("case 'A'");
		break;
	case 'B':
		printf("case 'B'");
		break;
	defaut:
		printf("default ");
		break;
	}
	
}

运行结果什么都没有…
为什么不默认匹配defaut,因为关键字写错了 是default
把不是关键字的往里面塞 C把它识别成标签,所以不会报错;

类似goto那种:

#include <stdio.h>

int main(){
    
    
 
For:
	printf("hello\n");
goto For;
}


For就是个标签

感谢阅读

猜你喜欢

转载自blog.csdn.net/qq_46530073/article/details/123386097