相关疑难解决方法(0)

通过在c中平方宏SQR而感到困惑

这个问题在模拟面试中被问到了......真的很惊讶地找到了尴尬的答案......

考虑一个宏:

#define SQR(x) (x*x)
Run Code Online (Sandbox Code Playgroud)

例1:

SQR(2) //prints 4
Run Code Online (Sandbox Code Playgroud)

例2:

如果给出SQR(1 + 1),它不会求和(1+1),2而是......

SQR(1+1) //prints 3
Run Code Online (Sandbox Code Playgroud)

尴尬吧?是什么原因?这段代码是如何工作的?

注意:我搜索了SO,但找不到任何相关问题.如果有任何好评请分享!

c c++ macros c-preprocessor

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

C宏和括号中的参数使用

#define Echo(a)  a
#define Echo(a) (a)
Run Code Online (Sandbox Code Playgroud)

我意识到这里可能没有显着差异,但为什么你想要a在宏体内包括括号内?它是如何改变它的?

c macros parentheses c-preprocessor

13
推荐指数
1
解决办法
3032
查看次数

我怎样才能保证一个#define比另一个大?

#define MY_CONST 20
#define OTHER_CONST 10
Run Code Online (Sandbox Code Playgroud)

我的代码才有意义MY_CONST > OTHER_CONST.如何使用预处理器保证这一点?有这样的命令吗?

#assert MY_CONST < OTHER_CONST
Run Code Online (Sandbox Code Playgroud)

arduino c-preprocessor

7
推荐指数
1
解决办法
126
查看次数

为什么在 C 语言中 f(0) + f(11) == f(0) 通过条件定义 f(0)

#include<stdio.h>
#define f(x) (x<10) ? 1 : 3

int main(){
    int i = f(1) + f(11);
    printf("%d", i);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么在这种情况下,程序输出“1”而不是“4”。
定义宏的这两种不同用途究竟是如何工作的?

c

5
推荐指数
1
解决办法
109
查看次数

我们可以删除C宏定义中参数周围的括号吗?

http://c-faq.com/style/strcmp.html,我学到了以下便利宏:

#define Streq(s1, s2) (strcmp((s1), (s2)) == 0)
Run Code Online (Sandbox Code Playgroud)

我想知道为什么在这个宏中使用了这么多括号.每个括号是用于某个目的还是使用冗余括号的宏,这些括号没有用处?

我们可以删除周围的括号s1s2像这样制作一个宏吗?

#define MyStreq(s1, s2) (strcmp(s1, s2) == 0)
Run Code Online (Sandbox Code Playgroud)

这个MyStreq宏似乎对我很有用Streq.

#include <string.h>
#include <stdio.h>

#define Streq(s1, s2) (strcmp((s1), (s2)) == 0)
#define MyStreq(s1, s2) (strcmp(s1, s2) == 0)

int main()
{
    printf("%d %d\n", Streq("foo", "foo"), MyStreq("foo", "foo"));
    printf("%d %d\n", Streq("fox", "foo"), MyStreq("fox", "foo"));
    printf("%d %d\n", Streq("foo", "fox"), MyStreq("foo", "fox"));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面代码的输出:

1 1
0 0
0 0
Run Code Online (Sandbox Code Playgroud)

你能想象这些宏的使用在哪里Streq可以实现人们所期望的但却 …

c macros parentheses c-preprocessor

3
推荐指数
2
解决办法
922
查看次数

Is there any difference between this two macros?

I find no difference between these two macros except the parentheses surrounding the macro in the first one.

Is it a matter of readability or is it a way to deal with the priority of operators?

#define TAM_ARRAY(a) (sizeof(a)/sizeof(*a))
#define TAM_ARRAY2(a) sizeof(a)/sizeof(*a)
Run Code Online (Sandbox Code Playgroud)

c c-preprocessor

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

定义处理是否有所不同

为什么这段代码给我的值是"4"而不是"0"?

#define PLUGIN_PPQ 96
#define MIDIENGINE_SCORE_LENGTH PLUGIN_PPQ * 4

int main ()
{
    int mCurrentPatternPulse = 97;
    int patternBar = (int)floor(mCurrentPatternPulse / MIDIENGINE_SCORE_LENGTH);
    cout << "value: " << patternBar << " (" << MIDIENGINE_SCORE_LENGTH << ")";
}
Run Code Online (Sandbox Code Playgroud)

97/384(有/没有楼层)应该给我0.

但它似乎除以96而不是384?即使我打印MIDIENGINE_SCORE_LENGTH是384 ...

c++

2
推荐指数
3
解决办法
77
查看次数

为什么math.h库中的tanh函数会给出错误的结果?

#include<stdio.h>
#include<math.h>
#define PI 2*acos(0.0)
int main(void)
{
    double theta;
    theta=tanh(1/(sqrt(3.0)));
    printf("With tanh function = %lf\n",theta);
    printf("Actual value = %lf\n",PI/6.0);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

tanh函数= 0.520737

实际值= 0.523599

为什么这两个值不同?它应该和我的理解一样.

c math.h

2
推荐指数
1
解决办法
152
查看次数

MISRA C 2012规则20.5 #undef不应使用

我试图摆脱违反规则20.5

示例代码:

#define VAL 2
int32_t func(void)
 {
    int32_t n1 = VAL;
    #undef VAL
    #define VAL(x) (x*x)
    return VAL(n1);
 }
Run Code Online (Sandbox Code Playgroud)

在没有改变任何其他线路的情况下,是否有任何针对undef的工作?

c misra

2
推荐指数
1
解决办法
382
查看次数

仅按位操作反转(翻转)数字的最后n位

给定一个二进制整数,如何在c/c ++中仅使用按位运算来反转(翻转)最后n位?
例如:

// flip last 2 bits
0110 -> 0101
0011 -> 0000
1000 -> 1011
Run Code Online (Sandbox Code Playgroud)

c c++ bitwise-operators

2
推荐指数
1
解决办法
279
查看次数