初始C语言(三)(C语言初阶)

目录

10、操作符(运算符)

算数操作符

移位操作符

位操作符

赋值操作符

单目操作符

 关系操作符

逻辑操作

条件操作符

逗号表达式

下标引用、函数调用和结构成员

11、常见关键字

11.1、关键字typedef

11.2、关键字static


10、操作符(运算符)

算数操作符

+  -  *  /  %


#include <stdio.h>

int main()
{
	int a = 7 / 2;       //'/'为除法号
	int b = 7 % 2;       //'%'为取余号
	float c = 7 / 2.0;
	printf("%d\n", a);   //打印值为3
	printf("%d\n", b);   //打印值为1
	printf("%f\n", c);   //打印值为3.500000
// %f——打印浮点数   %d——打印整数
	return 0;
}

移位操作符

//>> —— 右移操作符   << —— 左移操作符

#include<stdio.h>
int main()
{
	int a = 12;
//并不是以10进制去移动位置,而是以二进制去移动位置。
	int b = a >> 1;
//00000000000000000000000000001100 —— 移动之前 —— 值为12 —— 1*2*2*2+1*2*2+1*2+1
//00000000000000000000000000000110 —— 移动之后 —— 值为6  —— 1*2*2+1*2+1
//int 占空间 4个字节 —— 每个字节为占8个bit位 —— int 占 32 个 bit 位
	int c = a << 1;
//00000000000000000000000000001100 —— 移动之前 —— 值为12
//00000000000000000000000000011000 —— 移动之后 —— 值为24
	printf("%d\n", b);   //打印值为6
	printf("%d\n", c);   //打印值为24
	return 0;
}

注:a >> 1 等价与 a / 2 ———— a << 1 等价与 a * 2

a >> 2 等价与 a / 2 / 2 ———— a << 2 等价与 a * 2 * 2 —— 依次类推

位操作符

&  ^  |

#include<stdio.h>
int main()
{
	int a = 5;
	int b = 3;
	int c = 5 & 3;   //按(二进制)位与
//00000000000000000000000000000101 —— a
//00000000000000000000000000000011 —— b
//00000000000000000000000000000001 —— c
//对应的二进制位只要有0就为0、全部为1才为1
	int d = 5 | 3;   //按(二进制)位或
//00000000000000000000000000000101 —— a
//00000000000000000000000000000011 —— b
//00000000000000000000000000000111 —— c
//对应的二进制位只要有1就为1、全部为0才为0
	int e = 5 ^ 3;   //按(二进制)位异或
//00000000000000000000000000000101 —— a
//00000000000000000000000000000011 —— b
//00000000000000000000000000000110 —— c
//对应的二进制位只要有不同就为1,相同为0;
	printf("%d\n", c);   //打印值为1
	printf("%d\n", d);   //打印值为7
	printf("%d\n", e);   //打印值为6
	return 0;
}

赋值操作符

= += -= *= /= &= ^= |= >>= <<= 

#include<stdio.h>
int main()
{
	float a = 1;
	float b = 2.5f;
//直接定义的浮点数是double类型如(b  =  2.5)需要在后面加上一个f(如上一行所示)
	a += b;
//a += b  和  a = a + b  等价
//其他的赋值操作符也一样,依次类推
	printf("%f\n", a);
	return 0;
}

单目操作符

!           逻辑反操作
-           负值
+           正值
&           取地址
sizeof      操作数的类型长度(以字节为单位)
~           对一个数的二进制按位取反
--          前置、后置--
++          前置、后置++
*           间接访问操作符(解引用操作符)
(类型)       强制类型转换


//   !           逻辑反操作
//计算机如何(判断真假)
//0为假  非零为真
#include<stdio.h>
int main()
{
	int num = 0;
	scanf("%d", &num);
//输入0,打印hehe
	if (num)
		printf("hehe\n");
//输入非零,打印haha
	if (!num)
		printf("haha\n");
	return 0;
//注意计算机中的非零为真 = 1
}


//sizeof      操作数的类型长度(以字节为单位
#include<stdio.h>
int main()
{
	int arr[10] = { 0 };
	int sz = sizeof(arr) / sizeof(arr[0]);  //求解数组中元素的个数
	printf("%d\n", sizeof(arr));    //打印值为40
	printf("%d\n", sizeof(arr[0]));    //打印值为4
	printf("%d\n", sz);    //打印值为10
	return 0;
}


//~           对一个数的二进制按位取反
//计算机识别二进制语言 —— 内存存储的也是二进制语言。
//对一个数按位取反也是二进制。
//
//整数有三种二进制表达方式;
//原码 —— 直接按照数字大小正负转换成二进制
//反码 —— 原码的符号位不变,其它位按位取反
//补码 —— 反码 + 1;
//
//正整数的原码,反码,补码相同
//负数的原码,补码,反码需要计算
//
//二进制的最高位表示正负
//0表示正数   1表示负数
#include<stdio.h>
int main()
{
	int a = 0;
//计算机中储存的是补码
//00000000000000000000000000000000 —— a的补码 —— a是正整数所以原码,反码,补码相同
    int b = ~a
//11111111111111111111111111111111 —— b的补码
//printf打印的是原码,所以要把b的补码转换成原码
//11111111111111111111111111111110 —— b的反码
//10000000000000000000000000000001 —— b的原码
//整数的二进制最高位表示正负号所以b = 1
	printf("%d\n", b);   //打印值为1
	return 0;
}



//   --     前置、后置--
//   ++     前置、后置++
#include<stdio.h>
int main()
{
	int num1 = 10;
	int num2 = num1++;
	int num = 11;
	int num3 = --num;
	printf("%d\n", num1);   //打印值为11
	printf("%d\n", num2);   //打印值为10
	printf("%d\n", num);   //打印值为10
	printf("%d\n", num3);   //打印值为10
	return 0;
}



//(类型)       强制类型转换
#include<stdio.h>
int main()
{
	int a = (int) 4.56;
	printf("%d\n", a);
	return 0;
}

 关系操作符

>
>=
<
<=
!=   用于测试“不相等”
==   用于测试“相等”

逻辑操作

&&     逻辑与
||     逻辑或

条件操作符

//exp1? exp2 : exp3


//条件操作符(三目操作符)。
//比较a和b的大小。
#include<stdio.h>
int main()
{
	int a = 10;
	int b = 20;
	int c = a > b ? a : b;
	printf("%d\n", c);
	return 0;
}

逗号表达式

//exp1, exp2, exp3, …expN

//逗号表达式的应用
#include<stdio.h>
int main()
{
	int a = 1;
	int b = 2;
	int c = 3;
	int d = (a = a + b, c = b - c, b = a + c);
//逗号表达式从左到右依次执行,整个表达式的结果是最后一个表达式的结果。
	printf("%d\n", d);   //打印值为2
	return 0;
}

下标引用、函数调用和结构成员

//下标引用操作符[]
#include<stdio.h>
int main()
{
	int arr[10] = { 1,2,3,4 ,5 };
	int a = arr[2];   //其中的[]为下表引用操作符
//其中的操作数为 arr和2
	printf("%d\n",a);   //打印值为3
	return 0;
}


//函数调用操作符()
#include<stdio.h>

int ADD(int x, int y)   //这个()是函数的语法,不是函数调用操作符
{
	return (x + y);
}

int main()
{
	int num1 = 10;
	int num2 = 20;
	int num3 = ADD(num1, num2);
//上面一行中的()是函数引用操作符,引用函数过程中不可少
//其中的操作数为 ADD、num1、num2
	printf("%d\n", num3);   //打印值为30
	return 0;
}

11、常见关键字

auto  break   case  char  const   continue  default  do   double else  enum   
extern float  for   goto  if   int   long  register    return   short  signed
sizeof   static struct  switch  typedef union  unsigned   void  volatile  while

auto —— 自动关键字,可以省略,一般用于数据类型前面
循环语句:break continue do for while
选择语句:case continue default else if
数据类型:char double  float int long short signed unsigned void 
const —— const 修饰的常变量
自定义类型:enum struct union
extern —— 外部声明
goto语句
register —— 寄存器
return —— 函数返回
size —— 求数据所占空间的大小
static —— 静态
typedef —— 重新定义类型

11.1、关键字typedef

//将unsigned int 重新命名为 uint ,两者所表达的类型相同。
#include<stdio.h>

typedef unsigned int uint;  //unsigned int 重新命名为 uint

int main()
{
	uint age = 22;
	return 0;
}

11.2、关键字static

static修饰局部变量

 是局部变量出了自己的作用域,但是没有被销毁,

增加了局部变量的生命周期,但是作用域没有发生改变。

#include<stdio.h>
void test2()
{
	int b = 0;
	b++;
	printf("%d\n", b);
}
void test1()
{
	static int a = 0;   //static修饰的局部变量
	a++;
	printf("%d\n", a);
}
int main() 
{
	int i = 0;
	while (i < 10)
	{
		test1();    // 打印值为 1 2 3 4 5 6 7 8 9 10
		i++;
	}
	i = 0;
	while (i < 10)
	{
		test2();    // 打印值为 1 1 1 1 1 1 1 1 1 1
		i++;
	}
	return 0;
}

static修饰全局变量

全局变量有外部链接属性,作用域是整个工程

当static修饰全局变量时,全局变量没有了外部链接属性,只能在一个文件中使用,在其他文件中不能被引用。

static修饰函数

函数本身就具有外部链接属性

当static修饰函数时,函数没有了外部链接属性,只能在一个文件中使用,在其他文件中不能引用。

  • 16
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT技术博主-方兴未艾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值