C语言基础--强制类型转换
在C语言中,所有用到的数据都必须指定其数据类型。如果参与运算的数据类型不一致,首先就要将数据的类型转换为相同的类型才能进行运算。
数据类型转换一般可以分为隐式转换和显式转换,他们分别可以称为自动转换和强制转换。
一、隐式数据类型转换(自动类型转换)
(1)一般算数转换:系统自己通过某些运算符将操作数的值从一种数据类型自动 转换 成另一种类型。
若参与运算变量的类型不同,侧先转换成同一类型,然后进行运算。转换按照数据长度增加的方向进行,以保证精度不降低。如 int 类型和 long 类型运算时,先把 int 类型转换成 long 类型后再进行运算。
转换规则如图一:
代码:
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//在此处输入Objective-C代码
float a = 3.14;
int b = 2;
printf("a + b = %f\n",a + b);
[pool drain];
return 0;
}
结果为:
(2)所有的浮点类型数据运算时都是以双精度进行的,即使仅含 float 单精度变量运算的表达式,也要先转换成 double 类型数据再作运算。
代码:
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//在此处输入Objective-C代码
float a = 3.14;
float b = 2.55;
printf("a + b = %f\n",a + b);
[pool drain];
return 0;
}
结果为:
(3)char 类型和 int 类型数据参与运算时,必须先转换成int类型。
以%d格式输出时代码:
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//定义变量
int a = 10;
char b = 'a';
printf("a + b = %d\n",a + b);
[pool drain];
return 0;
}
结果为:
(2)输出转换:输出转换指的是待输出的数据和输出的格式不一致时进行的数据类型转换。系统会把符号作为数值的一部分处理,然后输出转换后的值。
以%c格式输出时代码:
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//在此处输入Objective-C代码
int a = 10;
char b = 'a';
printf("a + b = %c\n",a + b);
[pool drain];
return 0;
}
结果为:
此时将取出字符a对应的ASCII值97,然后和10 相加,再输出对应的ASCII值为107对应的字符k
(3)赋值转换:赋值转换是指在赋值运算过程中将赋值运算符右侧的操作数的类型转换成左侧操作数的数据类型。具体的转换有:
代码为:
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//在此处输入Objective-C代码
float a = 3.14f;
int b = 2;
b = a;
printf("b = %d\n",b);
[pool drain];
return 0;
}
结果为:
代码:
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//在此处输入Objective-C代码
float a = 3.14f;
int b = 2;
a = b;
printf("a = %f\n",a);
[pool drain];
return 0;
}
结果为:
二、强制类型转换(显示型转换)
强制类型转换格式:
(要转换的类型)要转换的变量或者表达式
代码:
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//在此处输入Objective-C代码
float a = 3.14f;
int b = 2;
a + b;
printf("a + b = %d\n",a + b);
[pool drain];
return 0;
}
结果:
转换后代码:
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//在此处输入Objective-C代码
float a = 3.14f;
int b = 2;
a + b;
printf("a + b = %d\n",(int)(a + b));
[pool drain];
return 0;
}
结果为:
注意事项:
(1)如果用一个很大的整型数加上一个特别小的数以float格式输出时会造成数据丢失,因为float单精度的有效 数位为7位
代码为:
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//在此处输入Objective-C代码
float a = 0.000002f;
int b = 8888;
float c = a + b;
printf("c = %f\n",c);
[pool drain];
return 0;
}
结果为:
(2)强制数据类型转换是临时转换
代码:
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//在此处输入Objective-C代码
float f1 = 3.14f;
int b = 12;
printf("f1 = %d\n",(int)f1);
printf("f1 = %f\n",f1);
[pool drain];
return 0;
}
结果为: