当前位置:网站首页>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
边栏推荐
- 733. image rendering
- Nacos调用微服务两个问题:1.Load balancer does not contain an instance for the service 2.Connection refused
- NestJS环境变量配置,解决如何在拦截器(interceptor)注入服务(service)的问题
- Games101 job 7 improvement - implementation process of micro surface material
- 如何系统学习LabVIEW?
- Basic functions of promise [IV. promise source code]
- [BJDCTF2020]The mystery of ip
- Interview-01
- [数组]BM94 接雨水问题-较难
- 为什么 C# 访问 null 字段会抛异常?
猜你喜欢

JMeter takes the result of the previous request as the parameter of the next request

Network structure and model principle of convolutional neural network (CNN)

MySql的开发环境

Anaconda3安装过程及安装后缺失大量文件,没有scripts等目录

Implementation of window encryption shell

WPF open source control library extended WPF toolkit Introduction (Classic)
![Promise source code class version [III. promise source code] [detailed code comments / complete test cases]](/img/51/e1c7d5a7241a6eca6c179ac2cb9088.png)
Promise source code class version [III. promise source code] [detailed code comments / complete test cases]

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

电商产品如何在知乎上进行推广和打广告?

Agile development - self use
随机推荐
SAI钢笔工具如何使用,入门篇
2021:AdaVQA: Overcoming Language Priors with Adapted Margin Cosine Loss∗自适应的边缘余弦损失解决语言先验
USB DRIVER
与STM32或GD32替换说明
Kotlin compose compositionlocalof and staticcompositionlocalof
2021:Greedy Gradient Ensemble for Robust Visual Question Answering
面试-01
fplan-布局
Usage knowledge of mobile phones in new fields
jmeter分布式压测
Career outlook, money outlook and happiness outlook
MATLAB | 三个趣的圆相关的数理性质可视化
Pat grade a 1025 pat ranking
promise源码-class版本【三、Promise源码】【代码详细注释/测试案例完整】
Quickly master asp Net authentication framework identity - reset password by mail
从某种意义来讲,互联网业已成为了一个孵化器,一个母体
Pat class a 1024 palindromic number
2021:passage retrieval for outside knowledgevisual question answering
文旅夜游|以沉浸式视觉体验激发游客的热情
苹果唯一图谱架构常识