标签:表达式 www. www char http jpg content tag 笔记
C语言中类型转换一般有强制类型转换与隐式类型转换两种;
强制类型转换语法:
强制类型转换结果:
编程练习:
#include <stdio.h>
struct TS
{
int i;
int j;
};
struct TS ts;
int main()
{
short s = 0x1122;
char c = (char)s; // 0x22
int i = (int)s; // 0x00001122
int j = (int)3.1415; // 3
unsigned int p = (unsigned int)&ts;
// long l = (long)ts; // error
// ts = (struct TS)l; // error
printf("s = %x\n", s);
printf("c = %x\n", c);
printf("i = %x\n", i);
printf("j = %x\n", j);
printf("p = %x\n", p);
printf("&ts = %p\n", &ts);
return 0;
}
输出结果为:
编译器主动进行的类型转换
隐式类型转换的发生条件:
(1)算术运算式中,低类型转换为高类型
(2)赋值表达式中,表达式的值转换为左边变量的类型
(3)函数调用时,实参转换为形参类型
(4)函数返回值,return表达式转换为返回值类型
实例分析:
#include <stdio.h>
int main()
{
char c = ‘a‘;
int i = c; // safe
unsigned int j = 0x11223344;
short s = j; // unsafe
printf("c = %c\n", c);
printf("i = %d\n", i);
printf("j = %x\n", j);
printf("s = %x\n", s);
printf("sizeof(c + s) = %d\n", sizeof(c + s));
return 0;
}
输出结果为:
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">
标签:表达式 www. www char http jpg content tag 笔记
原文地址:https://www.cnblogs.com/chen-ace/p/9838189.html