当前位置:网站首页>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
边栏推荐
- Interview-01
- Common sense of Apple's unique map architecture
- Matlab | drawing of three ordinate diagram based on block diagram layout
- 高等数学(第七版)同济大学 习题1-10 个人解答
- Pat grade a 1019 general palindromic number
- Mobilenet series (4): mobilenetv3 network details
- 733. image rendering
- NestJS环境变量配置,解决如何在拦截器(interceptor)注入服务(service)的问题
- 2020:MUTANT: A Training Paradigm for Out-of-Distribution Generalizationin Visual Question Answering
- Office VR porn, coquettish operation! The father of Microsoft hololens resigns!
猜你喜欢
Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
How to systematically learn LabVIEW?
Why does C throw exceptions when accessing null fields?
PostgreSQL基础命令教程:创建新用户admin来访问PostgreSQL
Pat grade a 1025 pat ranking
math_数集(数集符号)和集合论
Baidu PaddlePaddle's "universal gravitation" first stop in 2022 landed in Suzhou, comprehensively launching the SME empowerment plan
Ldr6028 OTG data transmission scheme for mobile devices while charging
promise源码-class版本【三、Promise源码】【代码详细注释/测试案例完整】
Pat grade a 1026 table tennis
随机推荐
Ledrui ldr6035 usb-c interface device supports rechargeable OTG data transmission scheme.
PostgreSQL basic command tutorial: create a new user admin to access PostgreSQL
Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot
乐得瑞LDR6035 USB-C接口设备支持可充电可OTG传输数据方案。
Static timing analysis OCV and time derive
Il manque beaucoup de fichiers et de répertoires tels que scripts pendant et après l'installation d'anaconda3
Ldr6028 OTG data transmission scheme for mobile devices while charging
Is the truth XX coming? Why are test / development programmers unwilling to work overtime? This is a crazy state
Products change the world
文旅灯光秀打破时空限制,展现景区夜游魅力
FastDDS的服务器记录-译-
GAMES101作业7提高-微表面材质的实现过程
jmeter将上一个请求的结果作为下一个请求的参数
Argo workflows - getting started with kubernetes' workflow engine
Why does C throw exceptions when accessing null fields?
手机新领域用法知识
2016Analyzing the Behavior of Visual Question Answering Models
如何系统学习LabVIEW?
语义化版本 2.0.0
[BJDCTF2020]The mystery of ip