c语言中void print是什么意思,如何引用另外一个文件中的串, 顺便说说void print();和(void)print();的区别...

博客探讨了C++中extern关键字的使用,展示了如何在多个源文件间共享变量。主要内容包括不同源文件间的变量传递,strcpy函数改变全局变量值的影响,以及函数声明与调用的差异。程序示例中,strcpy修改了test.cpp中str的值,并影响了print()函数的输出。

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

由于需要, 所以小写一下。

main.cpp中的代码为:

#include

using namespace std;

extern char str[];

int main()

{

cout << str << endl;

return 0;

}

test.cpp中的代码为:

char str[100] = "abcdefg";

程序的结果为:abcdefg

下一道菜:

main.cpp

#include

using namespace std;

void print();

extern char str[];

int main()

{

cout << str << endl;

// cout << sizeof(str) << endl; //编译错误

cout << strlen(str) << endl; // 7

strcpy(str, "xyz");

cout << str << endl;

print(); // 输出hello world, xyz, 可见, 上面的strcpy改变了test.cpp中的值

return 0;

}

test.cpp

#include

char str[100] = "abcdefg";

void print()

{

printf("hello world, %s\n", str);

}

输出结果:

abcdefg

7

xyz

hello world, xyz

最后看一下void print();和(void)print();的区别:

void print();是生命, 而(void)print();是调用, 在代码中非常常见, 为什么要加(void)呢, 多数为了通过静态检查工具的检查。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值