标签: c-preprocessor

在#define中使用引号

我想做这样的事情:

#define OF(k)                                     \
Open("##k##file");     
Run Code Online (Sandbox Code Playgroud)

那意味着,那

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

必须相当于

Open("1file");
Run Code Online (Sandbox Code Playgroud)

怎么做到这一点?

c++ c-preprocessor

0
推荐指数
1
解决办法
253
查看次数

如何使用#define在Arduino中分配引脚?

我试图用来#define创建一个常量并定义一个引脚,检查这个代码

#define PIN_MICROPHONE 13;

void loop()
{
    analogRead(PIN_MICROPHONE);
}
Run Code Online (Sandbox Code Playgroud)

但是在尝试编译时,它会说这个错误:

: In function 'void loop()':
error: expected `)' before ';' token
error: expected primary-expression before ')' token
error: expected `;' before ')' token
Run Code Online (Sandbox Code Playgroud)

如何使用#define宏来定义引脚?

这段代码编译好了

#define PIN_MICROPHONE 13;

void loop()
{
    analogRead(13);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Arduino 1.0.5

macros arduino c-preprocessor

0
推荐指数
1
解决办法
8444
查看次数

C++如何用consts替换#defines

最近我一直在仔细研究我的编程风格以及如何改进它.首先让我说,在我目前的职位上,我是唯一的程序员.结果,我可以把事情做成我想要的hacky,但我真的在努力成为一个更好,更健全的程序员.

另外,我的背景主要是基于C的,基本上在必要时使用C++作为C的超集.结果,我偶然发现了以下难题.

我总是用#define ERROR_FUNCTION_BLEW_UP -2定义错误代码.诚实地说,我可以看到这样做的好处,因为我不需要分配内存来存储-2.但是,在C++中,我可以看到使用const变量的好处,因为两个竞争宏之间的冲突机会较少.

结果,我想知道在C++中实现错误代码的最简洁方法是什么.也就是说,我希望客户端能够通过执行类似于"if(return_value == ERROR_FUNCTION_BLEW_UP)"的操作来检查某些函数的返回值.我已经尝试在每个类中添加一个const变量,但是代码看起来不正确.也就是说,客户端现在检查"if(return_value == MyClass.kErrorFunctionBlewUp_)"行.有没有更简洁的方法来实现这一点,而不是让常数成为班上的公共成员?

另外,要添加到我的问题,myClass是一个基类,现在我想在MyDerivedClass中添加更多错误代码.有什么办法解决这个问题并避免使用宏?

感谢大家的帮助.

c++ const c-preprocessor

0
推荐指数
1
解决办法
139
查看次数

C预处理器条件语句输出

我有这段代码,但无法理解输出.有人可以帮忙知道这种行为

#include<stdio.h>
int main(){

int a =1;
#if(a==0)
  printf("equal");
#else if
  printf("unequal");
#endif

return -1;
}
Run Code Online (Sandbox Code Playgroud)

输出出来了equal.对我来说很奇怪.

此外,如果我改变if条件a==2,输出来了unequal

如果我尝试在'if'块内打印'a'的值

#if(a==0)
 printf("value of a: %d",a);
Run Code Online (Sandbox Code Playgroud)

输出出来了 value of a: 1

请有人解释输出.

c c-preprocessor

0
推荐指数
1
解决办法
163
查看次数

是否存在预处理'AND'指令

是否有Preproccessor AND指令?如果不是,你会怎么建议我解决以下任务?

#define INSTALL_V8
#define INSTALL_V9
#define INSTALL_V10  // Using preprocessor directives and not static variables to
// avoid packaging unnecessary code into the application/installer

#ifdef INSTALL_V8 AND INSTALL_V9 AND INSTALL_V10
  #define CHECK_BOX_STRT_Y 60 // move the start y up so we have room to fit 3 checkboxes in the window
#else
  #define CHECK_BOX_STRT_Y 80 // place 1st checkbox in middle of window
#endif
Run Code Online (Sandbox Code Playgroud)

c-preprocessor preprocessor-directive

0
推荐指数
1
解决办法
111
查看次数

C++中函数的多重定义

我有一个非常不寻常的问题:

我不断在课堂上得到多个函数定义.

这是我的主要.cpp

#include <iostream>
#include "Calculation.cpp"

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我的班级.h

#ifndef CALCULATION_H_INCLUDED
#define CALCULATION_H_INCLUDED

class Calculation
{
  public:
  Calculation();
  private:

};
#endif // CALCULATION_H_INCLUDED
Run Code Online (Sandbox Code Playgroud)

这是我的实现文件.cpp

#include "Calculation.h"

Calculation::Calculation()
{

}
Run Code Online (Sandbox Code Playgroud)

请帮我; 我试图创建一个新项目,但没有帮助.

所有帮助表示赞赏.

c++ c-preprocessor

0
推荐指数
1
解决办法
262
查看次数

定义Array C/C++

这两个数组定义有什么区别,哪一个更正确?为什么?

#include <stdio.h>
#define SIZE 20

int main() {

    // definition method 1:
    int a[SIZE];
    // end definition method 1.

    // defintion method 2:
    int n;
    scanf("%d", &n);
    int b[n];
    // end definition method 2.

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道,如果我们看大小,变量n,从stdin,这是更正确的定义我们(我们将使用的内存块)数组的指针,并使用stdlib.harray = malloc(n * sizeof(int)),而不是decalring它int array[n],但是又为什么呢?

c c++ arrays c-preprocessor

0
推荐指数
1
解决办法
594
查看次数

C++使用#define访问struct?

我正在测试一些C++代码.目前有一个问题我无法理解其起源.代码是这样的:

        typedef struct
        {
          UWORD tFollowUp_uw;

          union
          {
            UBYTE Control_ub;

            struct
            {
              BITFIELD8 Request_b1  :1;
            }b_st;
          }b_un;
        }X_CONTROLS;

    #define Request_b       (Controls_st.b_un.b_st.Request_b1)
    #define tFollowUp_uw    (Controls_st.tFollowUp_uw)

class Class_T20
{
  public:
      X_CONTROLS Controls_st;
}
Run Code Online (Sandbox Code Playgroud)

所以,当我测试这些LOC时,我首先声明一个对象Class_T20_obj.

我只能Request_b1通过调用来访问变量Class_T20_obj.Controls_st.b_un.b_st.Request_b1

我无法tFollowUp_uw通过调用访问变量Class_T20_obj.Controls_st.tFollowUp_uw

谁能帮我这个?

c++ c-preprocessor

0
推荐指数
1
解决办法
106
查看次数

有没有办法写一个知道另一个宏使用了多少次的宏?

我有类似以下内容,我不满意:

#define BEGIN {

#define END_1 };
#define END_2 END_1 };
#define END_3 END_2 };
// ... (possibly more of these) ...

#define END(x) END_ ## x

int main()
{
    BEGIN
    BEGIN
    BEGIN
    END(3) // <- I don't want to pass the 3 here

    BEGIN
    BEGIN
    END(2) // <- I don't want to pass the 2 here
}
Run Code Online (Sandbox Code Playgroud)

我想重写BEGIN和/或END后者的定义,以便后者不需要进行论证.

我相信这不可能做到,但我对C预处理器不是很有经验.是否至少有任何方法可以比我发布的示例更接近我的目标?

c macros c-preprocessor

0
推荐指数
1
解决办法
168
查看次数

C预处理器保持多行

我需要在C中使用不同类型的几个数组上做同样的事情.

使用void *我编写了一个将接收所有类型的函数:

void
floatarraymanip(void *inarray)
{
  float *array=(float *)inarray;

  [SEVERAL LINES OF OPERATIONS]-------------
}                                          |
                                           |
void                                       |
longarraymanip(void *inarray)              ----> Same
{                                          |
  long *array=(long *)inarray;             |
                                           |
  [SEVERAL LINES OF OPERATIONS]-------------
}

void
arraymanip(void *array, char *arraytype)
{
  if(strcmp(arraytype,"FLOAT")==0)
    floatarraymanip(array);
  if(strcmp(arraytype,"LONG")==0)
    floatarraymanip(array);
}
Run Code Online (Sandbox Code Playgroud)

[SEVERAL LINES OF OPERATIONS]是完全一样的和相对长(约50线).我还需要为其他几种类型执行此操作,使我的源代码的当前版本非常长!

所以我的问题是:有什么方法可以在预处理器中将所有这些行保存在一个命令中,并在所有这些函数中调用该预处理器变量,以便程序变得更短更可读?

c c-preprocessor

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

标签 统计

c-preprocessor ×10

c++ ×5

c ×4

macros ×2

arduino ×1

arrays ×1

const ×1

preprocessor-directive ×1