Tag

C

26 posts

C (/ˈsiː/, as in the letter c) is a general-purpose, imperative computer programming language. It supports structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, including operating systems, as well as various application software for computers ranging from supercomputers to embedded systems.

Code

sqlite3 的转义

sqlite 关于 string 类型的数据需要转义。

通过查找规则,可以很容易通过switch拼装一个字符串来搞定。 大致规则是,单引号和双引号要变成两个,其他特殊字符为前面加上'/'。

Code

fix记录:注意返回值

sqlite3使用sqlite3_exec,原型如下:

C
int sqlite3_exec(
	sqlite3*,                                  /* An open database */
	const char *sql,                           /* SQL to be evaluated */
	int (*callback)(void*,int,char**,char**),  /* Callback function */
	void *,                                    /* 1st argument to callback */
	char **errmsg                              /* Error msg written here */
);

其中有个参数是callback类型的。查询到的数据放在callback中处理。

Code

KMP 字符串匹配

现在有一个长字符串,以及一个短的key,从长的字符串中如何快速的找出和key匹配的字符串偏移 -- 或者是存在与否呢?

Code

statfs获取硬盘信息

statfs是一个很有用的linux库函数。在它的帮助下,我们可以很容易得到硬盘信息啥的。 以前全是调用command命令,弱爆了。如果用 man 2 statfs 命令可以得到很多其使用方法。本post就简单写个demo表示下存在感

Code

打印素数

素数,也就是质数.若一个数字,除了它自己和1,不能被任何其它整数整除,则本数字被称为'素数'.

Code

implicit declaration of function ‘xxx’

随手几行测试代码,编译居然报了一排warning,仔细查看,都是类似如下格式的:

Bash
xxx\xx.c||In function 'xxx':|
xxx\xx.c|14|warning: implicit declaration of function 'memset' [-Wimplicit-function-declaration]|
xxx\xx.c|14|warning: incompatible implicit declaration of built-in function 'memset' [enabled by default]|

Code

关于const的两个tip

1. 在输入一个字符串的时候,一般请加上const 修饰

比如 :
int strlen(const char * str);
其中,str字符串在本函数中是不需要进行修改的,所以可以用const修饰 这样,我们引用strlen函数的时候可以这样写:
strlen("abc");
如果没有const,一个warning就会来了

Code

error while loading shared libraries的解决方法

错误提示:

Bash
./prog: error while loading shared libraries: xxx.so.0:
cannot open shared object file: No such file or directory

这个提示的意思是没有找到xxx.so.0这个文件,那么首先应该确定下,本机是否存在这个.so文件?如果存在,那么我们可以作出判断,xxx.so.0这个动态链接库所在的文件夹不在搜寻范围内.

Code

三门问题模拟

问题描述: 三门问题也就是“蒙提霍尔问题”,表述如下: 现在有三扇门,两扇门后是山羊,一扇门后是汽车. 参赛者并不知道内里有什么。 参赛者需要在三扇门中挑选一扇,期望是门后有汽车。 主持人知道每扇门后面有什么。 主持人必须开启剩下的其中一扇门,并且必须提供换门的机会。 主持人永远都会挑一扇有山羊的门。 如果参赛者挑了一扇有山羊的门,主持人必须挑另一扇有山羊的门。 如果参赛者挑了一扇有汽车的门,主持人随机在另外两扇门中挑一扇有山羊的门。 参赛者会被问是否保持他的原来选择,还是转而选择剩下的那一道门。 转换选择可以增加参赛者的机会吗?

Code

m进制转换为n进制的 C++ 实现

要解决的问题是: 将任意(假定为m)进制的一个数字转化为n进制的数据。 基本思路是m进制转化为2进制,再二进制转化为n进制。 相比于许多同仁的代码,解决的范围大了一些(比如数值可以超过long什么的)

Code

C语言写宏注意事项,一次debug的教训

现在有代码如下.

宏定义设置好:

C
#define ERROR_RET(x) error_code=x;return false;

然后期望在以后某处调用如下:

C
void fun()
{
    if(sth) ERROR_RET(eno);
    ....
}

编译护结果始终不理想.逻辑和想的不一样啊...查了别的代码好久,一直以为其它地方数据处理有问题.

Code

“SOCKET” does not name a type

OS 为 Windows 7 X64,IDE 为Code::Blocks, 编译器为 mingw64, 使用 mysql,得到错误提示道:

Bash
"SOCKET" does not name a type