关于static char[]的初始化问题。

021850524 2004-10-17 02:12:28
关于static char[]的初始化问题。
在windows程序设计中看到了这种用法,感觉有些奇怪,请大家帮忙解释一下
(具体位置在上册218页函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
中的第7行)。

具体例子是:
#include <stdio.h>
#include <string.h>
int main()
{
static char szTop[] = "this" "is" "a" "test"; //这是什么意思呢?
printf("%s\t%d.\n", szTop, strlen(szTop));
return 0;
}
/*
结果:thisisatest 11.
*/
...全文
710 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞在天空的鱼 2004-10-17
  • 打赏
  • 举报
回复
无聊
021850524 2004-10-17
  • 打赏
  • 举报
回复
哦,这样啊,多谢各位的解释,在这本书上头一次看到这种写法.
somedummy 2004-10-17
  • 打赏
  • 举报
回复
C99的前一个标准是C89……
ftkghost 2004-10-17
  • 打赏
  • 举报
回复
"this""is""a""test"
请问this、is、a、test后面的'\0'呢?
是不是和string变量相加一样,前面3个单词后的'\0'被覆盖了?
yzss 2004-10-17
  • 打赏
  • 举报
回复
空格也是一个字符,而"this""is""a""test"显然没有空格,
所以比"this is a test"少三个长度。
zgy166 2004-10-17
  • 打赏
  • 举报
回复
引号之间的字符串会被直接连接起来,这是C99的前一个标准(标准名字我忘了,C80?)新增加的内容。
如:
#define RELEASE_S_TYPE "X"
#define SVER "2.01"
#define SOFTVER RELEASE_S_TYPE SVER

那么SOFTVER 就是“X2.01”
greenteanet 2004-10-17
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
#include <string.h>
int main()
{
static char szTop1[] = "this" "is" "a" "test";
static char szTop2[] = "thisisatest";
static char szTop3[] = "this is a test";
printf("%s\t%d.\n", szTop1, strlen(szTop1));
printf("%s\t%d.\n", szTop2, strlen(szTop2));
printf("%s\t%d.\n", szTop3, strlen(szTop3));
return 0;
}
thisisatest 11
thisisatest 11
this is a test 14
个人认为应该是
static char szTop1[] = "this" "is" "a" "test";
static char szTop2[] = "thisisatest";
相同!
xiaonian_3654 2004-10-17
  • 打赏
  • 举报
回复
楼上错了
kobefly 2004-10-17
  • 打赏
  • 举报
回复
static char szTop[] = "this" "is" "a" "test";

static char szTop[] = "thisisatest";

相同!
相同吗?
应该是
static char szTop[] = "this" "is" "a" "test";

static char szTop[] = "this is a test";

相同吧!
你我他111 2004-10-17
  • 打赏
  • 举报
回复
同意楼上的
shadowstar 2004-10-17
  • 打赏
  • 举报
回复
static char szTop[] = "this" "is" "a" "test";

static char szTop[] = "thisisatest";

相同!

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧