我试图打印,\a但它没有显示任何内容,所以我搜索了这个,发现它应该发出声音,但它也没有.
我在Windows 8上使用code lite.
\a?这是我的代码:
#include <iostream>
using namespace std;
int main()
{
cout <<"\a";
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我可以在 mmap 手册页中看到错误情况下的返回值是 (void *)-1.
c 编译器(void *)在常量之前将如何处理,这里-1.
以下代码片段是检查 mmap 错误值的正确方法吗?
int *p;
p = (int *)mmap();
if(p == -1)
printf("error \n");
Run Code Online (Sandbox Code Playgroud)
我们是否需要使用以下错误条件检查。
if(*p == -1)
printf("error \n");
Run Code Online (Sandbox Code Playgroud) 我真的不明白为什么我必须b在反演后对变量进行类型转换(一元operator ~).任何人都可以解释为什么需要它吗?
unsigned char a = 0xFF;
unsigned char b = 0x00;
return (a == (~b)); //expected to return 1 but 0
...
return (a == (unsigned char)(~b)); //after typecast returns 1 as expected
Run Code Online (Sandbox Code Playgroud) 我在VC++ 2015中尝试了以下代码
#include <iostream>
#include <string>
using namespace std;
int foo(int v)
{
cout << v << endl;
return 10;
}
string bar(int v)
{
cout << v << endl;
return "10";
}
int main()
{
auto a = foo(1) + foo(2) + foo(3);
auto b = bar(10) + bar(20) + bar(30);
cout << "----" << endl << a << endl << b << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
控制台上的结果如下
1
2
3
30
20
10
----
30
101010
Run Code Online (Sandbox Code Playgroud)
众所周知,二元+运算符具有从左到右的关联性,并且可以通过3次调用来确认foo …
可以说有一个c ++函数foo()返回一个布尔值.
我调用此函数来检查属性的状态或获取函数调用的结果.
那么调用这种类型函数的最佳方法是什么呢.
方法1:
bool flag = foo()
if (flag)
{
// some code
}
else
{
// else some code
}
Run Code Online (Sandbox Code Playgroud)
方法2:
if ( foo() )
{
// some code
}
else
{
// some code
}
Run Code Online (Sandbox Code Playgroud)
我的问题:使用临时变量是否为编译器提供了更好地优化的机会.
我在发送从指向结构的指针构建的 zmq 消息时遇到问题,该结构包含其他结构。
服务器代码:
#include <zmq.hpp>
#include <string>
#include <iostream>
using namespace zmq;
using namespace std;
struct structB{
int a;
string c;
};
struct structC{
int z;
struct structB b;
};
int main()
{
context_t context(1);
socket_t *socket = new socket_t(context,ZMQ_REP);
socket->bind("tcp://*:5555");
message_t *request = new message_t();
socket->recv(request);
struct structB messageB;
messageB.a=0;
messageB.c="aa";
struct structC *messageC = new struct structC;
messageC->z = 4;
messageC->b = messageB;
char *buffer = (char*)(messageC);
message_t *reply = new message_t((void*)buffer,
+sizeof(struct structB)
+sizeof(struct structC)
,0); …Run Code Online (Sandbox Code Playgroud) 在编译下面的代码时,我收到错误,
在int main()中,t1未在此范围内声明.
我在用g++.在main()我已经宣布t1,t2和t3.那为什么我会收到这个错误?
#include<iostream>
using namespace std;
class time
{
int hours;
int minute;
public:
void getdata(int h,int m)
{
hours=h;
minute=m;
}
void putdata(void)
{
cout<<"\n Hours = "<<hours;
cout<<"\n Minutes = "<<minute;
}
void sumtime(time ,time);
};
void time::sumtime(time t1,time t2)
{
minute=t1.minute+t2.minute;
hours=minute/60;
minute=minute%60;
hours = hours + t1.hours + t2.hours;
}
int main()
{
time t1,t2,t3;
t1.getdata(2,45);
t2.getdata(3,30);
t3.sumtime(t1,t2);
cout<<"\n T1 ... "<<t1.putdata();
cout<<"\n T2 …Run Code Online (Sandbox Code Playgroud) 以下片段是否在发生错误时调用未定义的行为?
#include <stdio.h>
int main() {
int i; /* Indeterminate */
if (scanf("%d", &i) == 1) /* Initialize */
printf("%d\n", i); /* Success! Print read value */
else
printf("%d\n", i); /* Input failed! Is printing `i` UB or not? */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果scanf失败怎么办,是否访问了未初始化的变量?
编辑
此外,如果我替换scanf("%d", &i)为my_initializer(&i):
int my_initializer(int *pi)
{
double room_temp_degc = get_room_temp_in_degc();
if(room_temp_degc < 12.0) {
// Cool
*pi = 42;
return 1;
} else {
return 0;
}
}
Run Code Online (Sandbox Code Playgroud) 我需要输入这个等式,其中有一个因子.我想知道是否有类似*=乘法或pow(1,3)的东西用于C中的因子.
term = pow(-1, K) * pow(x, 2K)/(2K)
Run Code Online (Sandbox Code Playgroud)
阶乘将是最后2K.