我有两个C文件.
在file1.c
int main()
{
func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
file2.c中
static void func(void)
{
puts("func called");
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我用命令编译上面的代码cc file2.c file1.c,我得到了以下内容,
undefined reference to `func'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
但是,如果我删除file2.c中的static关键字并使用命令编译上面的代码,它就会成功运行.cc file2.c file1.c
所以,我有一个问题,C中的void和static void函数有什么区别?
我在C标准草案(n1570)中看到了以下示例:
$ 3.14第4段:宣布为以下结构:
struct
{
char a;
int b:5, c:11, :0, d:8;
struct
{
int ee:8;
} e;
}
Run Code Online (Sandbox Code Playgroud)
那么,什么:0意思呢?
我知道什么是字段,但:0没有名字,我不明白.
:0没有任何标识符的目的是什么?
我有一个std::set允许从迭代器范围中扣除的东西.
#include <iostream>
#include <set>
int main()
{
std::set s1 = {1,2,3,4};
std::set s2(s1.begin(), s1.end());
}
Run Code Online (Sandbox Code Playgroud)
上述程序未能在GCC中编译.
为什么演绎失败std::set?
根据cppreference:
如果表达式的类型是可变长度数组类型, 则计算表达式,并在运行时计算它计算的数组的大小.
这意味着:如果表达式的类型是VLA类型,则计算表达式.例如:
#include <stdio.h>
int main() {
int i = 0;
int a[i];
printf("%zu\n",sizeof(a[i++]));
printf("%d\n",i); // Here, print 0 instead of 1
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以,根据参考,这里i变成了1.但是,使用我的GCC编译器,i打印为0.
请参阅Wandbox演示.
我写了关于sizeof运算符的代码.如果我写的东西如下:
#include <stdio.h>
int main() {
char a[20];
printf("%zu\n", sizeof(a));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
20 // Ok, it's fine
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用这样的逗号运算符:
#include <stdio.h>
int main() {
char a[20];
char b;
printf("%zu\n", sizeof(b, a));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
8 // Why the output 8?
Run Code Online (Sandbox Code Playgroud)
所以,我有一个问题:
8在第二个例子中提供输出?comma运营sizeof()商对运营商的行为是什么?我看到了其他运营商(如and,or,not等)浏览cppreference时.
他们是替代"正常"运营商如&&,||,!等.
我检查了程序集中使用&&和的代码and.两个版本都生成相同的程序集.
代码:
#include <iostream>
int n = 1;
int main()
{
// if(n > 0 && n < 5)
if(n > 0 and n < 5)
{
std::cout << "n is small and positive\n";
}
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
&&和and运营商有什么区别?and过&&?and,or,not等等)?在普通的控制台应用程序我有这个
Environment.SpecialFolder.LocalApplicationData是 C:\Users\Simon\AppData\Local\
在Windows服务中
Environment.SpecialFolder.LocalApplicationData 是 C:\Windows\system32\config\systemprofile\AppData\Local\
如何在两种类型的应用程序中指定相同的路径?
在以下程序中我使用了unsigned关键字.
#include <stdio.h>
int main()
{
unsigned char i = 'A';
unsigned j = 'B';
printf(" i = %c j = %c", i, j);
}
Run Code Online (Sandbox Code Playgroud)
输出:
i = A j = B
Run Code Online (Sandbox Code Playgroud)
是unsigned char i 相当于unsigned j?
C++ 11引入了std :: nearbyint和std :: round函数.两者都返回"最接近的"整数值.
我应该何时何地更喜欢一个?
我用以下值测试了它们0.5:
案例1:附近的演示
#include <iostream>
#include <cmath>
int main()
{
std::cout<<"nearbyint(0.5) = "<<std::nearbyint(0.5);
}
Run Code Online (Sandbox Code Playgroud)
输出: 0
案例2: 圆形演示
#include <iostream>
#include <cmath>
int main()
{
std::cout<<"round(0.5) = "<<std::round(0.5);
}
Run Code Online (Sandbox Code Playgroud)
输出: 1
为什么输出不同?
情况1:
#include <iostream>
decltype(auto) fun()
{
std::string str = "In fun";
return str;
}
int main()
{
std::cout << fun() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在这里,程序在Gcc编译器中工作正常.decltype(auto)被推断为类型str.
案例2:
#include <iostream>
decltype(auto) fun()
{
std::string str = "In fun";
return (str); // Why not working??
}
int main()
{
std::cout << fun() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在这里,生成以下错误和分段错误:
In function 'decltype(auto) fun()':
prog.cc:5:21: warning: reference to local variable 'str' returned [-Wreturn-local-addr]
std::string str = "In fun";
^~~
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
为什么 …