C discovery

本文探讨了C语言中的多种实用技巧,包括特殊输出行为、整数运算类型转换、非法函数名定义、标准输出刷新问题、编译错误解析及结构体初始化等核心内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编程环境:
uname -a: Linux thinkhy-desktop 2.6.22-14-generic #1 SMP Tue Feb 12 07:42:25 UTC 2008 i686 GNU/Linux
gcc -v: gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)

1 printf("%c",0); 什么也不输出
printf("%s",0); 输出为 (null)

2 int 与unsigned int 做运算时,int会自动转换成unsigned int类型。
把下面的代码跑一遍就知道了。

  #include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int array[] = {23,34,12,17,204,99,16};

int main()
{
int d;

for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d/n",array[d+1]);

return 0;
}

3 C语言中,变量名、函数名、宏命名中的合法字符都是英文字符、数字、下划线,所以下面这样的函数名是错误的:

   void OS_UBUNTU-UX_print()

4 stdou标准输出是行缓冲的,所以和 stdout和stderr混用的时候要注意及时用‘/n'刷新。
下面的代码是不会输出hello-out的,详见APUE中文第二版Page110
#include
<stdio.h> #include <unistd.h> int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}

5. 编译数据库模块时出现这样的错误:
dbapi.c|1019| error: static declaration of ‘nsr_db_printerr’ follows non-static declaration
dbapi.c|989| error: previous implicit declaration of ‘nsr_db_printerr’ was here

什么意思? 原来是在static函数声明之前,使用了static函数。没想到C语言还有这个讲究,有两个解决办法:
1> 在使用之前声明static函数
2> 去掉函数的static修饰。

6

看到一段C代码中有类似的写法,很不解,冒号为什么用在这,

xmlSAXHandler saxHandler = {
internalSubset: internalSubset,
isStandalone: isStandalone,
hasInternalSubset: hasInternalSubset,
hasExternalSubset: hasExternalSubset,
resolveEntity: resolveEntity,
getEntity: getEntity,

.... ...

}

自己写了段测试代码才弄明白,冒号是用来赋值的:

#include <stdio.h>
struct sax {
int a;
int b;
};
typedef struct sax Sax;

int main() {
int aa=23,b=20;
Sax s = {a:b,b:aa}; /*s.a = b,s.b = aa*/
printf("%d,%d/n",s.a,s.b);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值