找到你要的答案

Q:can we declare a variable in #define directive in c?

Q:我们可以声明一个变量在#定义指令的C?

what would be the output for the following code snippet??

#include<stdio.h>
#define MUL(A,B) int t; t=A*B;
void main()
{
    int A=10,B=12;
    printf("%d", MUL(A,B));
}

will the output be an error(like declaration not allowed) or the given numbers would be multiplied???

Is the declaration allowed in #define preprocessor directive???

将下面的代码段的输出??

#include<stdio.h>
#define MUL(A,B) int t; t=A*B;
void main()
{
    int A=10,B=12;
    printf("%d", MUL(A,B));
}

输出将是一个错误(如声明不允许)或给定的数字将乘以???

允许在#声明定义预处理器指令???

answer1: 回答1:

Compiler will replace your printf("%d", MUL(A,B)) with printf("%d", int t; t=A*B). It will cause an error Type name is not allowed, because you pass int typename in the printf function.Yes, you can declare a variable using #define:

#include<stdio.h>

#define MUL(A,B) int t; t=A*B;

void main()
{
    int A=10, B=12;
    MUL(A,B);
    printf("%d", t);
}

It will not cause an error. After MUL(A,B) you will be able to access t variable.But declaring variables in this way is really complicated for understanding and debugging. Avoid it.

编译器将取代你的printf(“%d”,(A,B)多)用printf(“%d”,int t;T=A×B)。这会导致一个错误类型的名字是不允许的,因为你通过int类型在printf函数。是的,你可以声明一个变量使用#定义:

#include<stdio.h>

#define MUL(A,B) int t; t=A*B;

void main()
{
    int A=10, B=12;
    MUL(A,B);
    printf("%d", t);
}

它不会造成错误。多(A,B)后,你将能够访问变量。但这种方式声明变量是很复杂的理解和调试。避免它。

declaration  c-preprocessor