C语言数据类型形式转换与隐式转换,以及宏定义

//宏定义:宏定义只能做一级传参,凡是有#或者##的地方,不在进行替换

//param:#
//
#define INT_MAX 0x7fffffff;
#define A 3
#define _Fun(x,y) (int)(x##e##y)
#define Str(x) (#x)
#define FUN(x,y) _Fun(x,y)
#define SUM(x,y) (x+y)

int main()
{
char* TestChar = Str(hello);
printf(“num value is:%s\n”, TestChar);
//char* num = Fun(“as”, “cd”);
//printf(“num value is:%s\n”,num);
char* testchar2 = Str(INT_MAX);
printf(“num value is:%s\n”, testchar2);
int Testnum2 = FUN(A,A);
printf(“num value is:%d\n”, Testnum2);
Testnum2 = SUM(A, A);//如果不是#或者##,宏会继续展开
printf(“num value is:%d\n”, Testnum2);

int TestNumber = -1;

if (TestNumber < sizeof(int))
{
    printf("Test Numer < sizeof(int)");
}
else
{
    printf(" invalid Test Numer < sizeof(int)%d\n",TestNumber);
    printf(" invalid Test Numer < sizeof(int)%lu\n", TestNumber);
    printf(" invalid Test Numer < sizeof(int)%x\n", TestNumber);
    printf(" invalid Test Numer < sizeof(int)%X\n", TestNumber);
   
}

float num0 = 0.0;
int num1 = 64400;

num0 = (float)num1 / 150;//显式转换
//num0 = num1 / 150;//隐式转换
//num0 = num1 / 150。0;//隐式转换规则:值域较小的转换成值域较大的
printf("float number is %f\n", num0);
//赋值表达式中,左边类型与右边类型不同也会进行隐式转换,
int num2 = 0;
float num3 = 2.5f;

num2 = num3;//赋值表达式会吧左边强制转换成右边,出现截断

printf("打印int %d\n", num2);
printf("打印Float %f\n", (float)num2);

float PI = 3.14159;
int s1, r = 5;
double s2,s3;
s1 = r * r * PI;
s2 = (float)r * (float)r * PI;
s3 = (double)r * (double)r * (double)PI;
printf("s1=%d, s2=%lf,s3 = %lf\n", s1, s2,s3);

int sum = 103;  //总数
int count = 7;  //数目
double average;  //平均数
average = (double)sum / count;
average = sum / count;//这里是除法,两个整型相除,不考虑溢出,
average = sum * count;//这里必须要要提型在计算,否则会有可能出现溢出
average =(double)sum * count;//这里必须要要提型在计算,否则会有可能出现溢出
//count = 7.0;
average = sum / (double)count;
printf("Average is %lf!\n", average);


//强制转换是临时的。
double total = 400.8;  //总价
 count = 5;  //数目
double unit;  //单价
int total_int = (int)total;
unit = total / count;
printf("total=%lf, total_int=%d, unit=%lf\n", total, total_int, unit);
int a = 98;
printf(" ASCII %c\n", (char)a);
printf(" ASCII %d\n", (char)a);
a = 270;
//高级别的类型转换成低级别的类型,有可能会出现溢出,如果出现溢出的话,就把高位舍弃,保留地位
char tempChar = a;
printf(" ASCII %c\n", (char)a);
printf(" ASCII %d\n", (char)a);

printf("..... ASCII %c\n", tempChar);
printf(".... ASCII %d\n", tempChar);

//有符号整型,最高位表示的是符号位,如果转成无符号整型,强制
int bb = -1;
unsigned int cc = bb;
printf(" unsigned value %u\n", cc);
printf(" unsigned value %x\n", cc);

unsigned int dd = 4294967295;
int ee = dd;
printf(" unsigned value %d\n", ee);
printf(" unsigned value %u\n", ee);

short ff = -1;
printf(" unsigned value %d\n", ff);
printf(" unsigned value %u\n", ff);

unsigned short gg = 65535;
printf(" unsigned value %d\n", gg);
printf(" unsigned value %u\n", gg);

//宏定义中遇到#与##是什么意思。遇到#表示将后面的字符作为字符串处理
//##de的意思表示连接符。那么#param##param是什么意思?

运行结果
C语言数据类型形式转换与隐式转换,以及宏定义

上一篇:按键控制不同频率灯的闪烁


下一篇:git 学习笔记6--remote & log