目录
前言
1.c语言编程环境的安装 安装vs2022
2.写程序前的准备
3.初识c语言
3-1第一个程序Hello World
3-2注释的使用
3-3变量
3-4-1 字面常量
3-4-2 用const修饰常量
3-4-3用#define来修饰常量
3-5关键字
3-6标识符
前言
包含代码,代码演示结果,以及便于理解的插图
对于想要c语言入门,于嵌入式c语言的入门的朋友来说,这是一套不可多得的教程
此教程分几篇文章发布,初步计划更新到数组,未来时间允许会继续更新
1.c语言编程环境的安装 安装vs2022
方法1:在官网下载社区版本
方法2:在软件管家官网下载专业版http://www.softgj.com
2.写程序前的准备
3.初识c语言
3-1第一个程序Hello World
#include <stdio.h>
int main(){
printf(“HelloWorld\n”);
return0;
}
3-2注释的使用
#include <stdio.h> //头文件 std:standard标准的意思 io:input output输入输出
// 单行注释
或者 多行注释如下
/*
*/
int main(){ //c语言的入口函数,每个程序都要有
printf(“Hello World\n”);
//输出Hello World printf 格式化输入输出 f:format
return 0; //表示程序执行正常结束,如果执行不正常返回值是不为0的任意数
}
3-3变量
#include <stdio.h>
int main(){
//1.变量的定义
//格式:数据类型 变量名;
int a;
//2.变量的赋值
a =1;
printf(“%d\n”,a);
//3.变量初始化
int b =2;
printf(“%d\n”,b);
//4.多变量的定义
int c =3,d=4,e=5;
int f,h,m=10;
return 0;
}
变量一般定义了,后面的代码就得使用它,不然vs2022会warning你
3-4-1 字面常量
#include <stdio.h>
int main(){
123; //整型常量 0,正整数,负整数
12.12; //实型常量 带小数点的数字
'a'; //字符型常量 单个字母,数字,英文符号
"英雄"; //字符串常量 比如中文
return 0;
}
3-4-2 用const修饰常量
const修饰常量的好处:只要const a =1;那么后续a的值就一直是1;如果需要修改,只需要修改这一行代码就好。
#include <stdio.h>
//const修饰常量
//格式:const 常量名 = 值;
const winWidth = 1080;//窗口的宽度为1080
const winHeight = 720;//窗口的高度为720
int main(){
printf("%d\n",winWidth);
printf("%d\n",winHeight);
return 0;
}
小细节
3-4-3用#define来修饰常量
#include <stdio.h>
//用#ddefine来修饰常量、
//格式 #define 常量名 值 //注意结尾没有分号 一般前面有sharp符号的 也就是# 都不用打分号
#define a 1
#define x (1+2) //有运算符的要记得括号括起来
int main(){
printf("%d\n",a);
printf("%d\n",x*x);
return 0;
}
3-5关键字
#include <stdio.h>
//在vs2022中用紫色或者深蓝色标注的
//for int return
int main(){
for(int i = 0;i<10;++i){
printf("%d\n",i);
}
return 0;
}
下面的表格不用记
3-6标识符
#include <stdio.h>
int main(){
//标识符:人为定义的变量名等
//1.由字母,数字,下划线组成
int a1_ = 1;
printf("%d\n",a1_);
//2.不能以数字开头
//int 1a = 2;
//3.大小写敏感
int b = 2;
int B = 2;
printf("%d\n",b);
printf("%d\n",B);
//4.不能为关键字
//int for = 3; //不合法
//5.尽量一看就能看懂是什么意思
int appcount = 4;
printf("苹果的数量是:%d\n",appcount);
return 0;
}