当前位置:网站首页>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
边栏推荐
- WPF open source control library extended WPF toolkit Introduction (Classic)
- 语义化版本 2.0.0
- Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf
- PostgreSQL基础命令教程:创建新用户admin来访问PostgreSQL
- ERP demand and sales management Kingdee
- Pat class a 1024 palindromic number
- Anaconda3 is missing a large number of files during and after installation, and there are no scripts and other directories
- Kotlin compose custom compositionlocalprovider compositionlocal
- Cache comprehensive project - seckill architecture
- Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot
猜你喜欢

PostgreSQL basic command tutorial: create a new user admin to access PostgreSQL
![[BJDCTF2020]The mystery of ip](/img/f8/c3a7334252724635d42c8db3d1bbb0.png)
[BJDCTF2020]The mystery of ip

fplan-Powerplan实例

Fplan power planning

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

Products change the world

Argo workflows - getting started with kubernetes' workflow engine

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

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

文旅灯光秀打破时空限制,展现景区夜游魅力
随机推荐
mysql数据库基础:DQL数据查询语言
Installing MySQL on Windows
Anaconda3安装过程及安装后缺失大量文件,没有scripts等目录
Ldr6028 OTG data transmission scheme for mobile devices while charging
iOS开发:对于动态库共享缓存(dyld)的了解
Is the money invested in financial products guaranteed? Is there no more?
2021:passage retrieval for outside knowledgevisual question answering
Système de collecte des journaux
Static timing analysis OCV and time derive
Cache comprehensive project - seckill architecture
Nestjs environment variable configuration to solve the problem of how to inject services into interceptors
WPF open source control library extended WPF toolkit Introduction (Classic)
Kotlin compose compositionlocalof and staticcompositionlocalof
SAI钢笔工具如何使用,入门篇
Kotlin Compose 自定义 CompositionLocalProvider CompositionLocal
1.5 conda的使用
fplan-电源规划
ERP demand and sales management Kingdee
[array]bm94 rainwater connection problem - difficult
静态时序分析-OCV和time derate