小编bet*_*eta的帖子

使用按位运算符和布尔逻辑的绝对值 abs(x)

这是如何运作的?

这个想法是abs(x)对整数使用按位运算符(假设为 32 位字):

y = x >> 31
(x + y) ^ y // This gives abs(x) (is ^ XOR)?
Run Code Online (Sandbox Code Playgroud)

c c++ bit-manipulation absolute-value language-lawyer

52
推荐指数
3
解决办法
9802
查看次数

为什么`\`会影响printf的返回值?

参考此处给出的答案,您必须从以下代码中得到什么输出:

#include<stdio.h>

int main()
{
    int a=65;
    printf("%d\n",printf("%d\
    \n",a));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它给出了输出:

65
4
Run Code Online (Sandbox Code Playgroud)

但对我来说,它似乎应该给出这个:

65
3
Run Code Online (Sandbox Code Playgroud)

为什么是输出65 4

c printf

4
推荐指数
2
解决办法
281
查看次数

复制 C 风格的数组和结构

C++ 不允许使用=. 但是允许使用 复制结构=,如本链接中所示 -> Copying array in C v/s copying structure in C 。它还没有任何可靠的答案。但请考虑以下代码

#include <iostream>
using namespace std;

struct user {
    int a[4];
    char c;
};

int main() {
    user a{{1,2,3,4}, 'a'}, b{{4,5,6,7}, 'b'};
    a = b;             //should have given any error or warning but nothing
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码段没有给出任何错误和警告,而且工作正常。为什么?考虑解释两个问题(这个和上面链接的一个)。

c++ arrays struct variable-assignment c++11

4
推荐指数
2
解决办法
163
查看次数

字符串的大小

从下面的代码

#include<stdio.h>
int main()
{
    char *s;
    s="cool man army";  //here LHS and RHS are same type
    printf("ptr= %zd, normal string= %zd",sizeof(s),sizeof("cool man army"));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

schar *类型,"cool man army"也是char *那么为什么输出ptr= 8, normal string= 14不同?

c pointers c-strings

3
推荐指数
1
解决办法
100
查看次数