当前位置:网站首页>C程序设计的初步认识
C程序设计的初步认识
2022-07-03 21:04:00 【Java学不会】
C程序设计的初步认识
1、简单的C程序
1.1、C程序简介
#include <stdio.h> //编译预处理命令
int main(){
//主函数
printf("HelloWorld");
return 0;
}
- 一个C程序由
函数
组成,函数
是组成C程序的基本单位
,语句
是组成C程序的最小单位。 - 一个C程序总是由
主函数
开始执行。 - 一行可写一条或多条语句,一条语句也可写成一行或多行。
- 每条语句最好加
;
结束。
2、C语言表示符,常量与变量
标识符的组成:
- 标识符由
数字、字母、下划线
组成 - 开头只能是
字母或下划线
- 标识符由
标识符的分类:
注意:
1、关键字不能
用做用户标识符
2、 关键字均为小写
字母
3、 标识符区分大小写
4、预定义标识符可以
用做用户标识符- 用户标识符
- 预定义标识符:
scanf(输入函数)、pringf(输出函数)
include(文件包含)、define(宏定义) - 关键字:32个
2.1、常量
- 常见的基本数据类型包括
整型、实型、字符型
。 - 常量: 在程序运行中其值 不可改变 的量。
2.2、变量
- 变量: 在程序运行中,其值 可以改变 的量。
- 变量名: 由 标识符 组成。
- 未赋值的变量默认为随机值。
3、整型数据
整型数据以补码的形式存储
%d -->十进制格式输出
%o -->八进制格式输出(不带前导)
%x -->十六进制格式输出(不带前导,大小写看输出格式%x
大小写)
3.1、整型常量
- 十进制
- 八进制: 前导0(0123)
- 十六进制: 前导0x或0X(0X10)
- C语言中数据没有二进制
3.2、整型变量
int | 基本型 | TC2个字节/VC4个字节 |
---|---|---|
short | 短整型 | 2个字节 |
long | 长整型 | 4个字节 |
unigned | 无符号型 | — |
4、实型数据
%f输出格式:默认保留6位小数,不足6位补零,超过四舍五入
4.1、实型常量
- 小数点式: 由
数字、正负号、小数点
组成- 组成规则:
- 必须有
小数点
- 小数点
至少一边有数字
- 必须有
- 组成规则:
- 指数形式:由E或e组成
- 组成规则:
- 两边都要有数字
指数
部分必须是整数(E/e右边为整数)
- 组成规则:
4.2、实型变量
单精度 | float | 4个字节 |
---|---|---|
双精度 | double | 8个字节 |
5、字符型数据
5.1、字符常量
合法的字符常量:
单引号括起来一个字符
- 常规字符常量:单引号包裹起来的一个字符 ‘a’ ‘W’ ‘c’ ‘F’
- 转义字符常量(6+2 六个基本+两个扩展)
- ‘\n’ —— 回车换行(Enter)
- ‘\t’ —— 横向跳格(Tab)
- ‘\b’ —— 退格(BackSpace)
- ‘\\’ —— 反斜杠
- ‘\’’ —— 单引号
- ‘\"’ —— 双引号
- ‘\ddd’:
1至3位
八 进制整数表示的1个
字符 - ‘\xhh’:
1至2位
十六 进制整数表示的1个
字符
- 字符串常量:使用双引号包裹起来的字符,结束标志’\0’
5.2、字符变量
- char: 占1个字节
6、算术表达式
6.1、基本的运算符
- +、-、*、/、%
a/b
- 若a与b均为整型,结果为整型
- 若a与b其中任意一个为实型,结果为实型
- %(求余):只能用于整型
- 余数的正负取决于
被除数
(%左边)
- 余数的正负取决于
6.2、运算符的优先级和结合性
- 优先级:
次序
() > +(取正)、-(取负) > *、/、% > +(加)、-(减) - 结合性:方向
6.3、强制类型转换符
- 形式:
(类型名)
表达式 - 求值:
- (int)2.2 == 2
- (int)5.5/(int)2.5 == 2
- (int)5.5+2.5 == 7.5
7、赋值表达式
7.1、赋值运算符
- 形式: 变量名
=
表达式 - 优先级: 仅高于逗号运算符
- 结合性: 自右向左
7.2、复合赋值运算符
- +=、-=、*=、/=、%=
- 优先级: 仅高于逗号运算符
- 结合性: 自右向左
8、自加自减与逗号运算符
8.1、自加自减运算符
- 形式: ++变量、- -变量、变量++、变量- -
- 注意: 自加自减的运算对象必须是
变量
- 前值用法:++i或- -i
- 先将变量i的值加1或减1,然后再使用i的值。(立刻变化,用新值)
- 后值用法:i++或i- -
- 先使用变量i的值,然后再将变量i的值加1或减1。(先用旧值,再变化)
- 总结:
变量 表达式 变量 i=2 ++i==3 i==3 i=2 - -i==1 i==1 i=2 i++==2 i==3 i=2 i- -==2 i==1
8.2、逗号运算符
- 形式: 表达式1,表达式2,···,表达式n
- 优先级: 最低
- 结合性:自左向右
自左向右顺序求值,将
表达式n
的值作为整个逗号表达式的值
边栏推荐
- 强化学习-学习笔记1 | 基础概念
- Pytorch sets the weight and bias of the model to zero
- leetcode-540. A single element in an ordered array
- Basic knowledge of dictionaries and collections
- Hcie security Day11: preliminarily learn the concepts of firewall dual machine hot standby and vgmp
- Strange way of expressing integers (expanding Chinese remainder theorem)
- 电子科技大学|强化学习中有效利用的聚类经验回放
- Q&A:Transformer, Bert, ELMO, GPT, VIT
- 如临现场的视觉感染力,NBA决赛直播还能这样看?
- SQL injection - Fundamentals of SQL database operation
猜你喜欢
Xai+ network security? Brandon University and others' latest "interpretable artificial intelligence in network security applications" overview, 33 page PDF describes its current situation, challenges,
2022 high voltage electrician examination and high voltage electrician reexamination examination
LabVIEW training
Study diary: February 14th, 2022
Selenium has three waiting methods (forced waiting, implicit waiting, and display waiting)
强化学习-学习笔记1 | 基础概念
2022 safety officer-c certificate examination and safety officer-c certificate registration examination
Experience summary of database storage selection
18、 MySQL -- index
Pengcheng cup Web_ WP
随机推荐
[Yugong series] go teaching course 002 go language environment installation in July 2022
University of Electronic Science and technology | playback of clustering experience effectively used in reinforcement learning
Preliminary practice of niuke.com (11)
Scientific research document management Zotero
Recommendation of books related to strong foundation program mathematics
MySQL 8.0 data backup and recovery
(5) Web security | penetration testing | network security operating system database third-party security, with basic use of nmap and masscan
How to handle wechat circle of friends marketing activities and share production and release skills
Borui data and Sina Finance released the 2021 credit card industry development report
2022 high voltage electrician examination and high voltage electrician reexamination examination
Yyds dry goods inventory TCP & UDP
如临现场的视觉感染力,NBA决赛直播还能这样看?
MySQL——SQL注入问题
The 12th Blue Bridge Cup
leetcode-540. A single element in an ordered array
Design e-commerce seckill system
From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
Offset related concepts + drag modal box case
Qt6 QML Book/Qt Quick 3D/基础知识
Mysql database ----- common commands of database (based on database)