当前位置:网站首页>020 C语言基础:C语言强制类型转换与错误处理
020 C语言基础:C语言强制类型转换与错误处理
2022-06-27 04:04:00 【入狱计划进度50%】
一:概述
强制类型转换是把变量从一种类型转换为另一种数据类型。例如,如果您想存储一个 long 类型的值到一个简单的整型中,您需要把 long 类型强制转换为 int 类型。可以使用强制类型转换运算符来把值显式地从一种类型转换为另一种类型。(type_name) expression
实例:
使用强制类型转换运算符把一个整数变量除以另一个整数变量,得到一个浮点数:
#include <stdio.h>
int main(){
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("mean: %f \n", mean);
}
结果:mean: 3.400000
这里要注意的是强制类型转换运算符的优先级大于除法,因此 sum 的值首先被转换为 double 型,然后除以 count,得到一个类型为 double 的值。
类型转换可以是隐式的,由编译器自动执行,也可以是显式的,通过使用强制类型转换运算符来指定。在编程时,有需要类型转换的时候都用上强制类型转换运算符,是一种良好的编程习惯。
二:整数提升
整数提升是把小于int或unsigned int的整数类型转换为int或unsigned int的过程
#include <stdio.h>
int main(){
int i = 17;
char c = 'c'; // ascii值是99
int sum;
sum = i + c;
printf("value of sum: %d \n", sum);
}
结果:value of sum: 116
在这里,sum的值为116,因为编译器进行了整数提升,在执行实际加法运算时,把'c'的值转换为对应的ascii值。
三:常用的算术转换
就是隐式的把值强制转换为相同的类型。编译器首先执行整数提升,如果操作数类型不同,则他们会被转换为下列层次中出现的最高层次的类型:int --> unsigned int --> long --> unsigned long --> long long --> unsigned long long --> float --> double --> long double
常用的算术转换不适用于赋值运算符,逻辑运算符&&和||。
让我们看看下面的实例来理解这个概念:
#include <stdio.h>
int main()
{
int i = 17;
char c = 'c'; /* ascii 值是 99 */
float sum;
sum = i + c;
printf("Value of sum : %f\n", sum );
}
当上面的代码被编译和执行时,它会产生下列结果:
Value of sum : 116.000000
在这里,c 首先被转换为整数,但是由于最后的值是 double 型的,所以会应用常用的算术转换,编译器会把 i 和 c 转换为浮点型,并把它们相加得到一个浮点数。
四:错误处理
4.1:概述
C 语言不提供对错误处理的直接支持,但是作为一种系统编程语言,它以返回值的形式允许您访问底层数据。在发生错误时,大多数的 C 或 UNIX 函数调用返回 1 或 NULL,同时会设置一个错误代码 errno,该错误代码是全局变量,表示在函数调用期间发生了错误。您可以在 <error.h> 头文件中找到各种各样的错误代码。
所以,C 程序员可以通过检查返回值,然后根据返回值决定采取哪种适当的动作。开发人员应该在程序初始化时,把 errno 设置为 0,这是一种良好的编程习惯。0 值表示程序中没有错误。
4.2:errno/perror()/strerror()
C 语言提供了 perror() 和 strerror() 函数来显示与 errno 相关的文本消息。
perror()函数显示传给它的字符串,后跟一个冒号,一个空格和当前errno值的文本表示形式。
strerror()函数,返回一个指针,指针指向当前errno值的文本表示形式。
#include <stdio.h>
#include <errno.h>
#include <string.h>
extern int errno;
int main(){
FILE *pf;
int errnum;
pf = fopen("unexist.txt", "rb");
if(pf == NULL){
errnum = errno;
fprintf(stderr, "Value of errno: %d \n", errno);
perror("Error printed by perror");
fprintf(stderr, "Error opening file: %s \n", strerror(errnum));
}else{
fclose(pf);
}
return 0;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# ./cuowuchuli
Value of errno: 2
Error printed by perror: No such file or directory
Error opening file: No such file or directory
边栏推荐
- Promise [II. Promise source code] [detailed code comments / complete test cases]
- Network structure and model principle of convolutional neural network (CNN)
- JMeter distributed pressure measurement
- I found a JSON visualization tool artifact. I love it!
- Career outlook, money outlook and happiness outlook
- Further exploration of handler (Part 2) (the most complete analysis of the core principles of handler)
- Pat grade a 1023 have fun with numbers
- Fastdds server records - Translation-
- Building lightweight target detection based on mobilenet-yolov4
- IDEA中好用的插件
猜你喜欢

Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot

mysql数据库基础:DQL数据查询语言

Fplan power planning

Cultural tourism night tour | stimulate tourists' enthusiasm with immersive visual experience

Baidu PaddlePaddle's "universal gravitation" first stop in 2022 landed in Suzhou, comprehensively launching the SME empowerment plan

promise源码-class版本【三、Promise源码】【代码详细注释/测试案例完整】

如何系统学习LabVIEW?

2020:MUTANT: A Training Paradigm for Out-of-Distribution Generalizationin Visual Question Answering

Matlab | visualization of mathematical properties related to three interesting circles

Kotlin Compose 自定义 CompositionLocalProvider CompositionLocal
随机推荐
语义化版本 2.0.0
渗透测试-目录遍历漏洞
Method of decoding iPhone certificate file
IDEA中好用的插件
math_数集(数集符号)和集合论
MySQL development environment
Resnet152 pepper pest image recognition 1.0
Matlab | visualization of mathematical properties related to three interesting circles
Anaconda3安装过程及安装后缺失大量文件,没有scripts等目录
2021:Graphhopper: Multi-Hop Scene Graph Reasoning for Visual Question Answering
Pat class a 1024 palindromic number
Mysql database foundation: DQL data query language
Penetration test - directory traversal vulnerability
QChart笔记2: 添加鼠标悬停显示
FastDDS的服务器记录-译-
733. image rendering
面试-01
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-10 personal solutions
解码苹果手机证书文件方法
Learn crypto from Buu (Zhou Geng)