char *p = "abc";
char *q = "abc";
if (p == q)
printf ("equal");
else
printf ("not equal");
Run Code Online (Sandbox Code Playgroud)
输出:相等
它是编译器特定的,还是在标准中的某个位置定义为预期行为.
我只是好奇,因为在我的作业中它说下面的陈述是错误的:
每个C++实现都不包含C++标准库函数和类.
我不认为标准库包含在每个C++实现中,除非你添加(#include)相应的标题,对吧?在这种情况下,我认为上述陈述是真实的,而不是虚假的.
是语句真还是假?
在C++中,我有这个示例实现:
#include <thread>
#include <iostream>
void doSomeWork( void )
{
std::cout << "hello from thread..." << std::endl;
while(true)
{
printf("Getting inside doSomeWork\n");
sleep(1);
}
}
int main( int argc, char *argv[] )
{
std::thread t( doSomeWork );
t.join();
processing();
return 0;
}
void processing()
{
while(true)
{
printf("Getting inside processing\n");
sleep(1);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个问题,doSomeWork()继续做事情并阻止处理().我认为线程是异步的,所以当它正在睡觉时,我可以做其他事情.我的问题是如何在doSomeWork()中休眠并产生其他线程,然后恢复doSomework()?
哪些标准文档指定了C和/或C++预处理器的行为?
维基百科建议http://www.open-std.org/JTC1/SC22/WG14/www/standards对C99有效.是吗?C++风味怎么样?
我有几个void函数(让我们称之为foo和bar)共享相同的void函数清理,如果它们陷入困境,它们会在它们之后清理:
#include <iostream>
void cleanup() { std::cout << "doing cleanup" << std::endl; }
void foo(int & i) {
if(i == 0) { return cleanup(); }
--i;
if(i == 0) { return cleanup(); }
++i;
}
void bar(int & i) {
if(i == 0) { return cleanup(); }
++i;
if(i == 0) { return cleanup(); }
--i;
}
int main() {
int i = 0;
foo(i);
bar(i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
cpp.sh愉快地编译并运行代码.
感谢这个问题的答案,我知道我可以返回一个void类型的对象.但我不知道它是否适用于返回void返回值.
你怎么看,代码是否符合标准?
你宁愿返回虚拟整数来使代码更具可读性吗?或者无用的返回值会使代码难以阅读吗?
编辑:我觉得我需要补充一些澄清,例如'cleanup(); 返回;' 不是解决方案.实际代码比示例更复杂,并且取决于我离开函数的位置,在cleanup()调用之后会发生其他一些事情.'return cleanup();' …
此代码编译正常,没有错误.
#include <stdio.h>
#define N 10000
int main(void){
int t;
for(int i = 0, t = 1; i < 100; ++i){
printf("%i\n", i);
}
}
Run Code Online (Sandbox Code Playgroud)
然而,如果颠倒顺序i和t,它不会编译.
#include <stdio.h>
#define N 10000
int main(void){
int t;
for(t= 1, int i = 0; i < 100; ++i){
printf("%i\n", i);
}
}
Run Code Online (Sandbox Code Playgroud)
error: expected expression before int
这是故意的吗?它为什么会这样?我正在使用GCC和C11.
请注意,我正在执行此奇怪的任务不是因为我不清楚语法或对c还是陌生的。
我只是在尝试如果将一个int分配给一个数组会发生什么:
int a = {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
为此,我收到以下警告:
warning: excess elements in scalar initializer
int a = {1, 2, 3};
^
warning: excess elements in scalar initializer
int a = {1, 2, 3};
^
Run Code Online (Sandbox Code Playgroud)
当我打印a的内容时,我得到1。
但是当我这样做时:
int a;
a = {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
error: expected expression before ‘{’ token
a = {1, 2, 3};
^
Run Code Online (Sandbox Code Playgroud)
尽管我知道数组不是要分配给int变量的,但以上结果使我提出了疑问:
int x; x = ...和int x = ...和
我nullptr在一行 C 代码中包含了一个检查。编译器 (gcc) 在使用-std=c17以及-std=gnu17.
在现代 C 标准中是否有 nullptr(或等效的)这样的东西?(C11, C17)
如果不是,那为什么呢?
关于此代码:
#include <stdio.h>
int main(){
int x = 1;
if (0 == fork()){
int x = x + 1;
fork();
}
else{
int x = 4 * x;
fork();
}
printf("%d",x);
}
Run Code Online (Sandbox Code Playgroud)
我的一些朋友告诉我结果应该是2244,但是当我尝试运行它时,我得到了1111。
您能帮我了解如何fork()在这里工作吗?哪个答案是正确的?
多谢!
我在这个论坛上遇到过这个问题
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int x=0;
while (x<3) {
x = x++;
cout << x << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
鉴于上面的代码,为什么while循环无限?在mac os下使用gcc 4.4,while循环确实终止:)所以这个问题并不适用于所有架构.我变得艰难的输出是
1
2
3
我没有看到0,我猜原因与双重任务有关?