当前位置:网站首页>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 从mysql加载基础数据,广播出去2 从kafka加载基础数据的变更
- Design e-commerce seckill system
- Viewing Chinese science and technology from the Winter Olympics (II): when snowmaking breakthrough is in progress
- Reinforcement learning - learning notes 1 | basic concepts
- 全网都在疯传的《老板管理手册》(转)
- Basic number theory -- Chinese remainder theorem
- Pytorch sets the weight and bias of the model to zero
- UI automation test: selenium+po mode +pytest+allure integration
- JVM JNI and PVM pybind11 mass data transmission and optimization
- How to handle wechat circle of friends marketing activities and share production and release skills
猜你喜欢
![抓包整理外篇——————autoResponder、composer 、statistics [ 三]](/img/bf/ac3ba04c48e80b2d4f9c13894a4984.png)
抓包整理外篇——————autoResponder、composer 、statistics [ 三]

Memory analyzer (MAT)

MDM mass data synchronization test verification

Hcie security Day11: preliminarily learn the concepts of firewall dual machine hot standby and vgmp

Reinforcement learning - learning notes 1 | basic concepts

The "boss management manual" that is wildly spread all over the network (turn)
![[Tang Laoshi] C -- encapsulation: member variables and access modifiers](/img/be/0b38c0f1a27f819f7c79bcf634fbd4.jpg)
[Tang Laoshi] C -- encapsulation: member variables and access modifiers

Software testing skills, JMeter stress testing tutorial, obtaining post request data in x-www-form-urlencoded format (24)

全网都在疯传的《老板管理手册》(转)

Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
随机推荐
Nmap and masscan have their own advantages and disadvantages. The basic commands are often mixed to increase output
Kubernetes 通信异常网络故障 解决思路
Borui data and Sina Finance released the 2021 credit card industry development report
Deep search DFS + wide search BFS + traversal of trees and graphs + topological sequence (template article acwing)
Design e-commerce seckill system
Gauss elimination solves linear equations (floating-point Gauss elimination template)
Install and use Chrony, and then build your own time server
强化学习-学习笔记1 | 基础概念
[gd32l233c-start] 5. FLASH read / write - use internal flash to store data
Strange way of expressing integers (expanding Chinese remainder theorem)
Cannot load driver class: com. mysql. cj. jdbc. Driver
University of Electronic Science and technology | playback of clustering experience effectively used in reinforcement learning
Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
C 10 new feature [caller parameter expression] solves my confusion seven years ago
18、 MySQL -- index
Pytorch sets the weight and bias of the model to zero
Recommendation of books related to strong foundation program mathematics
Compilation Principle -- syntax analysis
Wireless network (preprocessing + concurrent search)
XAI+网络安全?布兰登大学等最新《可解释人工智能在网络安全应用》综述,33页pdf阐述其现状、挑战、开放问题和未来方向