这是如何运作的?
这个想法是abs(x)对整数使用按位运算符(假设为 32 位字):
y = x >> 31
(x + y) ^ y // This gives abs(x) (is ^ XOR)?
Run Code Online (Sandbox Code Playgroud) 参考此处给出的答案,您必须从以下代码中得到什么输出:
#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++ 不允许使用=. 但是允许使用 复制结构=,如本链接中所示 -> 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)
上面的代码段没有给出任何错误和警告,而且工作正常。为什么?考虑解释两个问题(这个和上面链接的一个)。
从下面的代码
#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)
s是char *类型,"cool man army"也是char *那么为什么输出ptr= 8, normal string= 14不同?