C语言常用方法技巧

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/

table th {
font-weight: bold;
}

table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}

table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}

table tr:nth-child(2n) {
background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

除法向上取整


#define DIV_ROUND_UP(n, d) (((n)+(d)-1) / (d))

大端小端选择

low-endian or high-endian


typedef union {
short W; /* Word access */
struct { /* Byte access */
#ifdef LOW_ENDIAN
byte low, high; /* in low-endian arch */
#else
byte high, low; /* in high-endian arch */
#endif
} B;
} word;

求余数运算

a = a % 8;
=>
a = a & 7; 说明:位运算只需一个指令周期;取余通常需要调用子程序。

平方运算

a = pow(a, 2.0);
=>
a = a * a; 说明:内置乘法运算器的处理器中,乘法运算比求平方运算更快;即使没有内置乘法运算器,乘法运算的子程序也比平方运算子程序效率高。

移位实现乘除法运算

a = a * 4;
b = b / 4;
=>
a = a << 2;
b = b >> 2; 说明:通常乘以或者除以2n,可使用移位方法代替。 a = a * 9;
=>
a = (a << 3) + a;

while 循环和 do...while 循环

说明:do...while 循环编译后生成的代码长度短于 while 循环。

重定义类型,扩展移植性

typedef unsigned char       boolean; /* Boolean value type. */
typedef unsigned long int uint32; /* Unsigned 32 bit value */
typedef unsigned short uint16; /* Unsigned 16 bit value */
typedef unsigned char uint8; /* Unsigned 8 bit value */
typedef signed long int int32; /* Signed 32 bit value */
typedef signed short int16; /* Signed 16 bit value */
typedef signed char int8; /* Signed 8 bit value */

得到指定地址上的一个字节或字

typedef unsigned char   byte; /* Unsigned 8 bit value type */
typedef unsigned short word; /* Unsigned 16 bit value type */ #define MEM_B(x) (*((byte*)(x)))
#define MEM_W(x) (*((word*)(x)))

求取极值

#define MAX(x, y)   ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))

得到一个 field 在结构体(struct)中的偏移量

typedef unsigned long   dword; /* Unsigned 32 bit value type */

#define FPOS(type, field)\
( (dword)&((type*)0)->field )

得到一个结构体中 field 所占用的字节数

#define FSIZE(type, field)\
( sizeof(((type*)0)->field) )

按照 LSB 格式把一个 word(16 bit) 转换成两个字节

#define FLOPW(ray, val)\
do {\
(ray)[0] = ((val)>>8);\
(ray)[1] = ((val)&0xFF);\
}while(0);

得到一个变量的地址

typedef unsigned char   byte; /* Unsigned 8 bit value type */
typedef unsigned short word; /* Unsigned 16 bit value type */ #define B_PTR(var) ((byte*)(void*)&(var))
#define W_PTR(var) ((word*)(void*)&(var))

得到一个字节的低位和高位

typedef unsigned char   byte; /* Unsigned 8 bit value type */
typedef unsigned short word; /* Unsigned 16 bit value type */ #define WORD_L(var) ((byte)(word)(var)&(0xFF))
#define WORD_H(var) ((byte)(word)(var)>>(8))

返回一个比 X 大的接近 8 的倍数

#define RND8(x) ((((x) + 7) >> 3) << 3)

防止溢出的方法

#define INC_SAT(val)\
((val) = ( ((val) + 1) > (val)) ? ((val) + 1):(val) )

返回数组元素的个数

#define ARR_SIZE(a)\
( (sizeof(a)) / (sizeof(a[0])) )

返回一个无符号数的后 n 位数

typedef unsigned long   dword; /* Unsigned 32 bit value type */ 

#define MOD_BY_POWER_OF_TWO(val, mod_by)\
((dword)(val)&(dword)(2<<(mod_by) - 1))

IO 空间映射在存储空间中的结构

typedef unsigned char   byte;   /* Unsigned 8 bit value type */
typedef unsigned short word; /* Unsigned 16 bit value type */
typedef unsigned long dword; /* Unsigned 32 bit value type */ #define outp(port) (*((volatile byte *)(port)))
#define outpw(port) (*((volatile word *)(port)))
#define outpdw(port) (*((volatile dword *)(port))) #define inp(port, val) (*((volatile byte *)(port))) = (byte)(val)
#define inpw(port, val) (*((volatile word *)(port))) = (word)(val)
#define inpdw(port, val) (*((volatile dword *)(port))) = (dword)(val)

宏中 "#" 和 "##" 的用法

一、使用 "#" 把宏参数变为一个字符串, 用 "##" 把两个宏参数贴合在一起。
#define STR(val) (#val)
#define CONS(a, b) (int)(a##e##b)
->
STR(hello) ==> "hello"
CONS(2, 3) ==> 2000 // 2e3 二、当宏参数是另一个宏的时候
需要注意的是凡宏定义里有用 '#' 或 '##' 的地方宏参数是不会再展开.
1, 非 '#' 和 '##' 的情况
#define TOW (2)
#define MUL(a,b) (a*b) printf("%d*%d=%d\n", TOW, TOW, MUL(TOW,TOW));
==>
printf("%d*%d=%d\n", (2), (2), ((2)*(2))); MUL里的参数TOW会被展开为(2)。 2, 当有 '#' 或 '##' 的时候
#define A (2)
#define STR(s) #s
#define CONS(a,b) int(a##e##b) printf("int max: %s\n", STR(INT_MAX)); // INT_MAX #include
==>
printf("int max: %s\n", "INT_MAX"); printf("%s\n", CONS(A, A)); // compile error
==>
printf("%s\n", int(AeA)); INT_MAX和A都不会再被展开, 然而解决这个问题的方法很简单,加多一层中间转换宏,
加这层宏的用意是把所有宏的参数在这层里全部展开, 那么在转换宏里的那一个
宏(_STR)就能得到正确的宏参数. #define A (2)
#define _STR(s) (#s)
#define STR(s) _STR(s) // 转换宏
#define _CONS(a,b) int(a##e##b)
#define CONS(a,b) _CONS(a,b) // 转换宏 printf("int max: %s\n", STR(INT_MAX)); // INT_MAX,int型的最大值,为一个变量 #include 输出为: int max: 0x7fffffff STR(INT_MAX) --> _STR(0x7fffffff) 然后再转换成字符串;
printf("%d\n", CONS(A, A)); 输出为:200 CONS(A, A) --> _CONS((2), (2)) --> int((2)e(2)) 三、'#' 和 '##' 的一些应用特例
1、合并匿名变量名
#define ___ANONYMOUS1(type, var, line) type var##line
#define __ANONYMOUS0(type, line) ___ANONYMOUS1(type, _anonymous, line)
#define ANONYMOUS(type) __ANONYMOUS0(type, __LINE__) 例:ANONYMOUS(static int);
即: static int _anonymous70; 70表示该行行号;
第一层:ANONYMOUS(static int);
--> __ANONYMOUS0(static int, __LINE__);
第二层: --> ___ANONYMOUS1(static int, _anonymous, 70);
第三层: --> static int _anonymous70;
即每次只能解开当前层的宏,所以__LINE__在第二层才能被解开; 2、填充结构
#define FILL(a) {a, #a}
enum IDD {OPEN, CLOSE};
typedef struct MSG{
IDD id;
const char * msg;
}MSG; MSG _msg[] = {FILL(OPEN), FILL(CLOSE)};
==>
MSG _msg[] = {{OPEN, "OPEN"}, {CLOSE, "CLOSE"}}; 3、记录文件名
#define _GET_FILE_NAME(f) #f
#define GET_FILE_NAME(f) _GET_FILE_NAME(f) static char FILE_NAME[] = GET_FILE_NAME(__FILE__); 4、得到一个数值类型所对应的字符串缓冲大小
#define _TYPE_BUF_SIZE(type) sizeof #type
#define TYPE_BUF_SIZE(type) _TYPE_BUF_SIZE(type) char buf[TYPE_BUF_SIZE(INT_MAX)];
--> char buf[_TYPE_BUF_SIZE(0x7fffffff)];
--> char buf[sizeof "0x7fffffff"];
这里相当于: char buf[11];

关于平方根倒数速算法(雷神之锤3,牛B)

C 语言常用方法技巧的更多相关文章

  1. 一些有意思的面试题(持续更新) .C语言编程技巧札记

    一些有意思的面试题(持续更新) http://blog.csdn.net/wangyuling1234567890/article/details/38565239 C语言编程技巧札记 http:// ...

  2. go语言json技巧

    go语言json技巧 本文总结了在项目中遇到的那些关于go语言JSON数据与结构体之间相互转换的问题及解决办法. 基本的序列化 首先我们来看一下Go语言中json.Marshal()(系列化)与jso ...

  3. ASP.NET MVC 多语言实现技巧 最简、最易维护和最快速开发

    说说传统做法的缺点 1.做过多语言的都知道这玩意儿太花时间 2.多语言架构一般使用资源文件.XML或者存储数据库来实现.这样就在一定程序上降低了性能 3.页面的可读性变差,需要和资源文件进行来回切换 ...

  4. Kotlin语言编程技巧集

    空语句 Kotlin 语言中的空语句有 {} Unit when (x) { 1 -> ... 2 -> ... else -> {} // else -> Unit } Wh ...

  5. 转:ASP.NET MVC 多语言实现技巧 最简、最易维护和最快速开发

    说说传统做法的缺点 1.做过多语言的都知道这玩意儿太花时间 2.多语言架构一般使用资源文件.XML或者存储数据库来实现.这样就在一定程序上降低了性能 3.页面的可读性变差,需要和资源文件进行来回切换 ...

  6. 第14讲:嵌入式SQL语言(基本技巧)

    一.交互式SQL的局限 & 嵌入式SQL的必要性 专业人员(如DBA)可以熟练地运用交互式SQL语言,但普通用户却不是那么容易上手,所以需要通过数据库应用程序来使用数据库.编写一个可以与数据库 ...

  7. C语言编程技巧-signal(信号)[转]

    自 http://www.uml.org.cn/c++/200812083.asp 信号是Linux编程中非常重要的部分,本文将详细介绍信号机制的基本概念.Linux对信号机制的大致实现方法.如何使用 ...

  8. C语言小技巧

    /* 求阶乘时设置最大调用层数,防止栈占满 当从函数进入另一个函数时当前函数的内容会入栈,另一个函数调用完时在出栈 */ int factorial(int n, int level) { //pri ...

  9. javascript语言使用技巧及注意事项总结

    1.首次为变量赋值时务必使用var关键字 变量没有声明而直接赋值得话,默认会作为一个新的全局变量,要尽量避免使用全局变量. var a=b=10;//其中a是局部变量,b是全局变量 2.使用===比= ...

随机推荐

  1. C++ new malloc realloc

    int* a = new int;          分配了存储空间,但没有赋初值 int* a = new int(10)     分配了存储空间,并赋初值,即*a = 10 int* a = ne ...

  2. java 中public 类

    java 中的文件名是以这个文件里面的public 的那个类命名的(也就是说文件名和这个文件里面的那个public 属性的class 名称一样), 同一个文件中不能放多个(超过2个)的pulic 类. ...

  3. URAL 1822. Hugo II&#39;s War 树的结构+二分

    1822. Hugo II's War Time limit: 0.5 second Memory limit: 64 MB The glorious King Hugo II has declare ...

  4. 【翻译自mos文章】将expdp的dmp文件从asm磁盘组里边放到本地文件系统里边

    将expdp的dmp文件从asm磁盘组里边放到本地文件系统里边 參考原文: How To Extract Datapump File From ASM Diskgroup To Local Files ...

  5. 大话设计模式宏观总结——创建型&amp;结构型&amp;行为型

    师傅验收项目的时候.问大话设计模式那三种类型的差别和联系是什么,顿时我傻了眼.由于除了知道这三种类型分别如何称呼以外.从来都没想过为什么这样划分?于是,我便回答:我没想过这个问题.若是从字面上来理解的 ...

  6. Codeforces Round #273 (Div. 2) B . Random Teams 贪心

    B. Random Teams   n participants of the competition were split into m teams in some manner so that e ...

  7. SoapUI报ClientProtocolException错误

    在SoapUI中出现了这个错误 org.apache.http.client.ClientProtocolException 检查后发现是SoapUI安装目录下lib中多了httpclient-*** ...

  8. linux用户态和内核态通信之netlink机制【转】

    本文转载自:http://blog.csdn.net/zcabcd123/article/details/8272360 这是一篇学习笔记,主要是对<Linux 系统内核空间与用户空间通信的实现 ...

  9. C# 数据库访问

    C# 数据库访问 分类: C#学习笔记2011-07-05 11:26 515人阅读 评论(0) 收藏 举报 数据库c#datasettextboxcommandexception   目录(?)[+ ...

  10. Application.StartupPath获取执行文件路径substring()取特定长度字符串取得根目录

    Application.StartupPath获取执行文件路径substring()取特定长度字符串取得根目录 2012-07-20 10:48 257人阅读 评论(0) 收藏 举报 path usi ...