我正在学习(ANSI)C 语言编程语言(第2版).
这是2.10赋值运算符和表达式的代码片段:
1 /* bitcount() counts the number of 1-bits in its integer argument */
2 int bitcount(unsigned x)
3 {
4 int b;
5 for (b = 0; x != 0; x >>= 1)
6 if (x & 01)
7 b++;
8 return b;
9 }
Run Code Online (Sandbox Code Playgroud)
我很困惑,为什么x & 01写在第6行而不是x & 1或x & 0x1?是0之前1有必要吗?
#include<windows.h>已经添加了,为什么GCC-mingw32编译器报告了'GetConsoleWindow' was not declared in this scope?
这是我的代码:
#include<iostream>
#include<cmath>
#include<windows.h>
using namespace std;
#define PI 3.14
int main()
{
//Get a console handle
HWND myconsole = GetConsoleWindow();
//Get a handle to device context
HDC mydc = GetDC(myconsole);
int pixel =0;
//Choose any color
COLORREF COLOR= RGB(255,255,255);
//Draw pixels
for(double i = 0; i < PI * 4; i += 0.05)
{
SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR);
pixel+=1;
}
ReleaseDC(myconsole, mydc);
cin.ignore();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢.^^
int main ()
{
float Num = 3254.4;
printf("%06.4f",Num);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么不打印003254.3999出我的期望,但是3254.3999?
在发布之前我已经完全阅读了这篇文章.
我使代码片段更易于解释
// Example 1
#define sum2(a, b) (a + b)
#define sum3(a, b, c) (sum2(a, sum2(b, c)))
sum3(1, 2, 3) // will be expanded to ((1 + (2 + 3)))
// Example 2
#define score student_exam_score
#define print_score(student_exam_score) printf("%d\n", score)
#undef score
print_score(80); // will be expanded to printf("%d\n", score);
// but not printf("%d\n", 80); that I expect
Run Code Online (Sandbox Code Playgroud)
第一个是直观的,这种代码存在于多个地方,例如找到最大或最小数。但是,我想使用该技术使我的代码干净且易于阅读,因此我将宏中的某些单词替换为更短且更有意义的名称。
AFAIK,C 预处理器每个编译单元只运行一次并且只执行字符串替换,但为什么print_score不能扩展到printf("%d\n", 80);?
这是我猜的更换程序:
#define score student_exam_score
#define print_score(student_exam_score) printf("%d\n", score)
#undef score
print_score(80);
// -->
#define …Run Code Online (Sandbox Code Playgroud) 一开始,我写了这样的东西
char* argv[] = { "ls", "-al", ..., (char*)NULL };
execvp("ls", argv);
Run Code Online (Sandbox Code Playgroud)
但是,GCC突然出现了这个警告:"C++禁止将字符串常量转换为char*."
然后,我将代码更改为
const char* argv[] = { "ls", "-al", ..., (char*)NULL };
execvp("ls", argv);
Run Code Online (Sandbox Code Playgroud)
结果,GCC弹出了这个错误,"无效转换const char**为char* const*."
然后,我将代码更改为
const char* argv[] = { "ls", "-al", ..., (char*)NULL };
execvp("ls", (char* const*)argv);
Run Code Online (Sandbox Code Playgroud)
它最终工作,编译没有任何警告和错误,但我认为这有点麻烦,我找不到有人在互联网上写这样的东西.
有没有更好的方法execvp在C++中使用?
我试图通过Xlib来模拟按键,但我找不到Fn" keysymdef.h "中定义键的位置.
如果它确实没有在Xlib中定义,是否有任何变通方法?
01 xcb_generic_event_t *event;
02 while ( (event = xcb_wait_for_event (connection)) ) {
03 switch (event->response_type & ~0x80) {
04 case XCB_EXPOSE: {
05 xcb_expose_event_t *expose = (xcb_expose_event_t *)event;
06 printf ("Window %"PRIu32" exposed. Region to be redrawn at location (%"PRIu16",%"PRIu16"), with dimension (%"PRIu16",%"PRIu16")\n",
07 expose->window, expose->x, expose->y, expose->width, expose->height );
08 break;
09 }
Run Code Online (Sandbox Code Playgroud)
在第5行,指向指向的xcb_generic_event_t指针xcb_expose_event_t,是否是在标准C语言中执行此类操作的好方法?请解释一下它的含义是什么?
是Str"指针"还是"指针指针" Str[3][5]?
我通常声明char **Str要制作一个模拟的2D数组,但是如何将地址发送Str[3][5]到另一个函数,以及如何设置该函数的参数以便修改值Str[3][5]?
据我所知,编译器将2D数组视为普通数组,因此我应该将Str(指针)发送到另一个函数.但是如何修改Str[3][5]通过接收指针的值?
例如,foobar:0.1将在名为的机器上指定显示0的屏幕1 foobar.
但Xlib的X服务器的屏幕号和显示号是什么意思?
这是否意味着X服务器可以有多个显示器,显示器可以有多个屏幕,屏幕可以有多个窗口?
我在不同的机器和不同的编译器上测试了这个,但我给出了相同的输出:
int sum = 10, i = 5;
printf("%d", sum+++i);
Run Code Online (Sandbox Code Playgroud)
这是C中定义明确或未定义的行为吗?