C宏定义的小结

实现代码实例

程序代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>     

/***** cplusplus *****/
#if 0
#include <iostream>
using namespace std;
#endif     

// 得到指定地址上的一个字节或字
#define MEM_B(x) (*((byte *)(x)))
#define MEM_W(x) (*((word *)(x)))     

// 得到一个field在结构体(struct)中的偏移量
#define FPOS(type, field) ((dword)&((type *)0)->field)     

// 将一个字母字符转换为大写
#define UPCASE(c) (((c)>='a' && (c)<='z') ? ((c)-0x20) : (c))     

// 判断字符是否为十进制的数字
#define DECCHECK(c) ((c)>='0' && (c)<='9')     

// 判断字符是否为十六进制的数字
#define HEXCHECK(hex) (((hex)>='0' && (hex)<='9')||((hex)>='A' && (hex)<='F')||

((hex)>='a' && (hex)<='f'))
// 防止溢出的一个方法
#define INC_SAT(val) (val=((val)+1 > (val)) ? (val)+1 : (val))     

// 计算数组元素的个数
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))     

int
main(void)
{
    int x = 0x1234abcd;
    char c = 'a';
    char dec = '5';
    char hex = 'e';
    char array[10] = {'1'};     

    //printf("MEM_B(x): 0x%p/n", MEM_B(x));
    //printf("MEM_W(x): 0x%p/n", MEM_W(x));     

    printf("UPCASE(c): %c -> %c/n", c, UPCASE(c));
    printf("DECCHECK(dec): %c -> %d/n", dec, DECCHECK(dec));
    printf("HEXCHECK(hex): %c -> %d/n", hex, HEXCHECK(hex));
    printf("ARRAY_SIZE(array): array[10] -> %d/n", ARRAY_SIZE(array));     

    printf("/n/****** MACRO ******//n");
    printf("__LINE__: %d/n", __LINE__);
    printf("__FILE__: %s/n", __FILE__);
    printf("__DATE__: %s/n", __DATE__);
    printf("__TIME__: %s/n", __TIME__);
    printf("__func__: %s/n", __func__);
#ifdef __cplusplus
    cout <<"hello __cplusplus"<<endl;
#endif
#ifdef __STDC__
    printf("hello __STDC__/n");
#endif     

    printf("/n/****** sizeof() ******//n");
    //printf("sizeof(byte): %d/n", sizeof(byte));
    printf("sizeof(char): %d/n", sizeof(char));
    printf("sizeof(signed char): %d/n", sizeof(signed char));
    printf("sizeof(unsigned char): %d/n", sizeof(unsigned char));
    printf("sizeof(short): %d/n", sizeof(short));
    printf("sizeof(signed short): %d/n", sizeof(signed short));
    printf("sizeof(unsigned short): %d/n", sizeof(unsigned short));
    printf("sizeof(int): %d/n", sizeof(int));
    printf("sizeof(signed int): %d/n", sizeof(signed int));
    printf("sizeof(unsigned int): %d/n", sizeof(unsigned int));
    printf("sizeof(long): %d/n", sizeof(long));
    printf("sizeof(signed long): %d/n", sizeof(signed long));
    printf("sizeof(unsigned long): %d/n", sizeof(unsigned long));
    printf("sizeof(long long): %d/n", sizeof(long long));
    printf("sizeof(signed long long): %d/n", sizeof(signed long long));
    printf("sizeof(unsigned long long): %d/n", sizeof(unsigned long long));
    printf("sizeof(float): %d/n", sizeof(float));
    printf("sizeof(double): %d/n", sizeof(double));

    exit(EXIT_SUCCESS);
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索printf
, include
, array
, char
, hex
, define
c++printf
c语言带参数的宏定义、c语言的宏定义、c语言中的宏定义、c的宏定义、c 中的宏定义,以便于您获取更多的相关知识。

时间: 2024-08-03 10:10:35

C宏定义的小结的相关文章

Velocity宏定义的坑与解决办法

使用Velocity,当然就免不了要使用宏,或者说使用Velocity而不使用其宏,就相当于废了Velocity一半以上的武功,非常可惜的. 怎么使用Velocity的宏呢,才最大程度的发挥其作用但是又避免掉入其中的坑呢?且听悠然乱弹乱弹: 官方文档中,关于Macro是这么说的: #macro - Allows users to define a Velocimacro (VM), a repeated segment of a VTL template, as required Format:

宏定义中的##操作符和... and _ _VA_ARGS_ _

1.Preprocessor Glue: The ## Operator 预处理连接符:##操作符 Like the # operator, the ## operator can be used in the replacement section of a function-like macro.Additionally, it can be used in the replacement section of an object-like macro. The ## operator co

C语言中的内联函数(inline)与宏定义(#define)详细解析_C 语言

先简明扼要,说下关键:1.内联函数在可读性方面与函数是相同的,而在编译时是将函数直接嵌入调用程序的主体,省去了调用/返回指令,这样在运行时速度更快. 2.内联函数可以调试,而宏定义是不可以调试的.内联函数与宏本质上是两个不同的概念如果程序编写者对于既要求快速,又要求可读的情况下,则应该将函数冠以inline.下面详细介绍一下探讨一下内联函数与宏定义. 一.内联函数是什么?内联函数是代码被插入到调用者代码处的函数.如同 #define 宏(但并不等同,原因见下文),内联函数通过避免被调用的开销来提

宏定义的黑魔法 - 宏菜鸟起飞手册

宏定义在C系开发中可以说占有举足轻重的作用.底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可以说底层开发离开define将寸步难行.而在更高层级进行开发时,我们会将更多的重心放在业务逻辑上,似乎对宏的使用和依赖并不多.但是使用宏定义的好处是不言自明的,在节省工作量的同时,代码可读性大大增加.如果想成为一个能写出漂亮优雅代码的开发者,宏定义绝对是必不可少的技能(虽然宏本身可能并不漂亮优雅XD).但是因为宏定义对于很多人来说,并不像业务逻辑那样是每天会接触的东西.即使是能偶尔使

C语言宏定义使用技巧

写好C语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性 等等.下面列举一些成熟软件中常用得宏定义-- 1,防止一个头文件被重复包含 #ifndef COMDEF_H #define COMDEF_H //头文件内容 #endif 2,重新定义一些类型,防止由于各种平台和编译器的不同,而产生的类型字节数差异,方便移植. typedef unsigned char boolean: /* Boolean value type. */ typedef unsigned l

C++:内置类型的最大值宏定义

C++中, 经常会使用, 某些类型的最大值, 如int的最大整数(INT_MAX), C的函数中, 包含了这些宏定义. 头文件: #include <climits> 具体参见: 作者:csdn博客 Spike_King 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

VC预处理指令与宏定义

一个经典的例子 使用预处理与宏定义最经典的例子莫过于加在一个头文件中以避免头文件被两次编译.试想这种的情况,有一个文件headerfile.h 它被包含在headerfile1.h中,同时在headerfile2.h 中也被包含了,现在有一个CPP文件,implement.cpp 包含了headerfile1.h 和headerfile2.h: #include "headerfile1.h" #include "headerfile2.h" 假设headerfil

单元测试-C语言 应该怎么修改宏定义 在函数5执行之后,打印错误error 函数6也能执行

问题描述 C语言 应该怎么修改宏定义 在函数5执行之后,打印错误error 函数6也能执行 这是函数5static char * test_replace(){Link_t head;head = LinkTable_new();Link_t temp3 =Link_replace(head101""cpp101"");int number4;if(temp3 != NULL){number4 =2;}else{number4 =0;}if(number4 == 1

c++-这个错误提示是什么啊。。我明明宏定义了这个变量的啊。。mfc

问题描述 这个错误提示是什么啊..我明明宏定义了这个变量的啊..mfc 这个错误提示是什么啊..我明明宏定义了这个变量的啊..mfc..求助 解决方案 宏定义里不要用; 分号 解决方案二: 对呀,宏定义不是语句