一、基本数据类型之int 类型
//
// Created by Lin Wang on 2022/10/31.
//
#include <stdio.h>
int main() {
// 有符号基本数据类型 short int long
short short_int = 0;
int i = 100;
long long_int = 0;
// 无符号基本数据类型 unsigned
unsigned short unsigned_short = 1;
unsigned int unsigned_int = 123;
unsigned long unsigned_long = 111;
size_t size_of_int = sizeof(int);
//hd%: short decimal
//%d: decimal 有符号十进制
//%ld: long decimal
//%lld:long long deciaml
//%n: new line
//%hu:unsigned short decimal
//%u: unsigned decimal 无符号十进制
//%x: hex 十六进制
//%o: oct 八进制
printf("short int: %hd\n", sizeof(short_int));
printf("int: %d\n", sizeof(i));
printf("long: %d\n", sizeof(long_int));
printf("unsigned short int: %hu\n", sizeof(unsigned_short));
printf("unsigned int: %u\n", sizeof(unsigned_int));
printf("unsigned long: %u\n", sizeof(long_int));
return 0;
}
二、基本数据类型之char 类型
//
// Created by Lin Wang on 2022/11/2.
//
#include <stdio.h>
#include <wchar.h>
int main() {
// 字符集 ASCII ,占据一个字节,8位
char a = 'a'; // 97
char char_1 = '1'; // 49
char char_0 = '0'; // 48
char i = 0; //\0,NULL
char newLine = '\n';
char char_1_oct = '\61'; // 斜杠后面添加一个8进制的数,就是一个字符
char char_1_hex = '\x31';
// 打印整数,%d
// \n :newline
// \b :backspace
// \r :return
// \t :table
// \' : ' 字符的字面量
// \":" 字符串字面量
// 字面量 literal
printf("char a: %d\n", a);
printf("char 1: %d\n", char_1);
printf("char 'i': %d\n", i);
// 打印字符,char类型格式化,%c
printf("char 1:%c\n", char_1);
// C语言中使用了一个新的类型,叫做wchar_t,w 是 wide 的首字母,t 是 type
// 的首字符, wchar_t的意思就是宽字符类型。
// wchar_t的长度由编译器决定,就是上面说到的那样,
// 微软编译器下是2个字节,等价于unsigned
// short,其他编译器是4个字节,相当于unsigned int。
// wchar_t位于wchar.h头文件中
// wchar_t 其实是用 typedef 关键字定义的一个别名。
// ————————————————
// 宽字符 C95标准增加
wchar_t wa = L'A'; // 英文字符(基本拉丁字符)
wchar_t wb = L'9'; // 英文数字(阿拉伯数字)
wchar_t zhong = L'中'; // 中文汉字
printf("中: %d\n",zhong); //中:20013
//Unicode CJK Code point 码点,编码不同会导致处理不同
//比如windows上面是GBK编码
//码点 20013 十六进制HEX值是4E2D
wchar_t zhong_hex = L'\u4E2D';
printf("中: %d\n",zhong_hex); //中:20013
//字符串
char *String ="中";
return 0;
}
三、基本数据类型之float 类型
//
// Created by Lin Wang on 2022/11/16.
//
#include <stdio.h>
int main(){
float a_float = 3.14f;//至少能表示6,7~8位有效数字 范围是:+-10^-37-10^37 单精度浮点型
printf("size of float: %d\n", sizeof(float ));//4个字节
double a_double = 3.14;//15-16 双精度浮点型
printf("size of double :%d\n",sizeof(double));//8个字节
12345;
1.2345e4;//科学计数法1.2345 x 10的4次方
//浮点型是一个科学计数法的数8,23,1
float money = 3.14f; //Error 不能用浮点型修饰钱
return 0;
}
四、变量的用法
//
// Created by billge on 2022/11/17.
//
/**
* 变量的用法: 内存地址分析,可以用打断点的方式分析内存地址
*/
#include <stdio.h>
int main(){
//<type> <name>
int value ;
//<type> <name> = <initialized value>
int value_initial = 24;
value =4;
value_initial = 5;
printf("value: %d\n",value);
value_initial = value;
printf("value: %d\n",value);
//打断点: add watches ,查看内存快捷键 ctrl+enter
printf("size of value: %d \n",value);
//#:0x
printf("address of value: %#x \n",&value); //内存地址是个整数 0x84ffd04
// key words 标识符(identifier) = value
// 变量名规则:1.a-zA-Z0-9_
// 数字不能在第一个
//Google code style, a-z_a-z, person_name
//变量名不能有小数点
float a_falot = 3.14;
return 0;
}
五、常量用法: 也叫只读变量
//
// Created by billge on 2022/11/17.
//
#include "stdio.h"
#define COLOR_READ 0xFF0000
#define COLOR_GREEN 0x00FF00
#define COLOR_BLUE 0x0000FF
/**
* 常量用法: 也叫只读变量
*
* @return
*/
int main() {
//const <type> readonly variable
const int kRED = 0xFF0000;
const int kGreen = 0x00FF00;
const int kBlue = 0x0000FF;
printf("kRed %d \n", kRED); //kRed 16711680
// kRED = 20;//编译器报错,只读变量不能显示的赋值
int *p_k_red = &kRED;
*p_k_red = 0;
printf("kRed %d \n", kRED); //0
//真正的常量,宏定义
printf("COLOR_READ %d \n", COLOR_READ);//kRed 16711680
//取消宏定义
#undef COLOR_READ
//真正的常量:字面量 literal
3;
3u;
3l;
3.9;
'c';
"cs";
L'中';
L"中国";
return 0;
}
六、运算符
//
// Created by billge on 2022/11/18.
//
#include "stdio.h"
/**
* 运算符
*
* @return
*/
int main() {
int first = 0;
int second;
int third;
//赋值运算符=
second = first; //表达式
third = second = first; //表达式
int left, right;
left = 2;
right = 3;
/**
* 四则运算
*/
int sum = left + right;//5
int diff = left - right;//-1
int product = left * right;//6
int quotient = left / right;//0
float quotient_error = left / right;//0
float quotient_correct = left * 1.f / right; //0.666667
int remainder = left % right;//2 取余数
int quotient_1 = 100 / 30; //3
printf("sum: %d\n", sum);
printf("diff: %d\n", diff);
printf("product: %d\n", product);
printf("quotient: %d\n", quotient);
printf("quotient_error: %d\n", quotient_error);
printf("quotient_correct: %f\n", quotient_correct);
printf("remainder: %d\n", remainder);
printf("quotient_1: %d\n", quotient_1);
//关系运算符> < >= <= == != true:1 false:0
//c99 _Bool
printf("3>2: %d\n", 3 > 2); //1
printf("3<2: %d\n", 3 < 2); //0
printf("3<=3: %d\n", 3 <= 3);//1
printf("3>=3: %d\n", 3 >= 3);//1
// && || 逻辑运算符
printf("3>2 && 3<2 %d\n", 3 > 2 && 3 < 2);
printf("3>2 || 3<2 %d\n", 3 > 2 || 3 < 2);
//++ --
int i = 1;
int j = i++;
int k = ++i;
int m = j;
printf("i: %d\n", i); //3
printf("j: %d\n", j); //1
printf("k: %d\n", k); //3
printf("m: %d\n", m); //3
int number = 10;
int new_number = number++;
printf("new_number = %i\n",new_number);//10
printf("number = %i\n",number); //11
++number;
printf("number = %i\n",number); //12
// bit operators & | ^ ~
#define FLAG_VISIBALE 0X1 //2^0 ,0001
#define FLAG_TRANSPARENT 0X2//2^1 ,0010
#define FLAG_RESIZABLE 0X4 // 2^2 ,0100
int window_floags = FLAG_RESIZABLE | FLAG_TRANSPARENT; //0110
int RESIZABLE = window_floags & FLAG_RESIZABLE; //0100
int visible = window_floags & FLAG_VISIBALE; //0000
// << >>
int x = 1000;
x * 2;
x << 1; //左移就是乘以2
x / 2;
x >> 1; //右移就是除以2
x *= 2;
x /= 2;
x -= 2;
x += 2;
x >>= 1;
x <<= 1;
//逗号运算符: 结果是取后边x = x +3的,
x = x * 2, x = x + 3;// 不推荐使用
return 0;
}
七、条件分支语句用法
//
// Created by billge on 2022/11/19.
//
#include <stdio.h>
#include <stdbool.h>
/**
* 条件分支语句用法
*/
int main() {
//Bool
//true:1 , false:0
//_Bool , bool
_Bool is_enabled = true;
is_enabled = 10;
printf("is_enabled: %d\n", is_enabled); //1
is_enabled = false;
printf("is_enabled: %d\n", is_enabled); //0
bool is_visible = false;
//if else
/**
* if(<condition>){
* ...true statement
* }else if(<condition1>){
* .....false statement
* }else {
* ......false statement
* }
*
*
*
*/
#define MAGIC_NUMBER 0
int user_input;
printf("please input a number:\n");
scanf("%d", &user_input);
if (user_input > MAGIC_NUMBER) {
printf("Your number is bigger !");
} else if (user_input < MAGIC_NUMBER) {
printf("Your number is smaller !");
} else {
printf("Yes! You got it");
}
if (is_enabled) {
if (is_visible) {
printf("is_visible");
} else {
printf("is_invisible");
}
}
//三元运算符 ?:-> <expr1> : <expr2>
//expr1 ==true,expr1
//expr2 ==true,expr2
int is_open = is_enabled && is_visible ? 1 : 0;
printf("is_open: %d\n", is_open);
//一定要用单引号,因为operator是char类型
#define ADD '+'
#define SUB '-'
#define MULTIPLY '*'
#define DIVIDE '/'
#define REM '%'
int left;
int right;
char operator;
printf("Please inout an expression: \n");
scanf("%d %c %d", &left, &operator, &right);
//switch语句的用法
int result;
switch (operator) {
case ADD:
result = left + right;
break;
case SUB:
result = left - right;
break;
case MULTIPLY:
result = left * right;
break;
case DIVIDE:
result = left / right;
break;
case REM:
result = left % right;
break;
default:
printf("Unsupported operation: %c\n", operator);
return 1;
}
printf("Result: %d\n", result);
return 0;
}
八、循环
//
// Created by billge on 2022/11/19.
//
#include <stdio.h>
int main() {
int a = -1;
scanf("%d", &a);
switch (a) {
case 101:
case 102:
case 103:
case 104:
case 105:
printf("大于\n");
break;
default:
printf("不大于\n");
break;
}
int b = -1;
scanf("%d", &b);
if (b >= 90 && b < 100) {
printf("分数等级:A");
} else if (b >= 80 && b < 89) {
printf("分数等级:B");
} else if (b >= 70 && b < 79) {
printf("分数等级:C");
} else if (b >= 60 && b < 69) {
printf("分数等级:D");
} else {
printf("分数等级:E");
}
int count = 0;
while(count<4){
printf("test while loop.....count=%d\n",count);
count++;
}
printf("while循环执行完毕\n");
//do while循环执行流程:
//1.不管while中的条件是否成立,都先去执行一次循环体
//2.执行完一次循环体,再次进入while进行条件判断,如果为true继续进入循环体,如果为false则跳出循环体
//3.重复以上操作,直到循环控制条件为false为止
int count_1 = 0;
do{
printf("test do while loop.....count_1=%d\n",count_1);
count_1++;
} while (count_1<0) ;
printf("do...while循环执行完毕\n");
/**
* 四大跳转 return , break , continue, goto
*
* break: 立即跳出switch语句;
* 立即跳出循环;
* 在多层循环中,break只向外跳出一层;
* break下面不能有语句,因为执行不到;
*
* continue: 结束本轮循环,进入一轮循环
*
*/
//嵌套循环
for(int i=0;i<4;i++){
printf("好友列表:%d\n",i);
for(int j=0;j<3;j++){
printf(" 角色%d\n",j);
}
}
return 0;
}
网友评论