宏 [英] Macro
问题描述
我有这个宏。
#include< stdio.h>
# include< stdlib.h>
#define getter(a)int get _ ## a(int a){if(a> 0)return 1;否则返回
0;}
main()
{
int i;
INT无效= 0;
的printf(QUOT;输入号码");
的scanf(QUOT;%d",&安培; I);
无效+ = getter(i);
}
我得到的错误是:
" get.c",line 10:语法错误之前或之后:int
" get.c",第10行:警告:语法错误:空声明
" get.c",line 11:语法错误之前或之后:}
这段代码不起作用。有人可以帮我理解吗?
谢谢你,
Shastri
Hi,
I have this macro.
#include<stdio.h>
#include<stdlib.h>
#define getter(a) int get_##a (int a) { if(a>0) return 1; else return
0;}
main()
{
int i;
int invalid=0;
printf("enter the number");
scanf("%d",&i);
invalid+=getter(i);
}
The errors I got are:
"get.c", line 10: syntax error before or at: int
"get.c", line 10: warning: syntax error: empty declaration
"get.c", line 11: syntax error before or at: }
This code doesn''t work . Can some one help me understand it??
Thank you,
Shastri
推荐答案
在文章< 11 ********************** @ i39g2000cwa.googlegroups .com>,
Shastri< sa*****@gmail.com>写道:
In article <11**********************@i39g2000cwa.googlegroups .com>,
Shastri <sa*****@gmail.com> wrote:
我有这个宏。
#include< stdio.h>
#include< stdlib.h>
#define getter(a)int get _ ## a(int a){if(a> 0)return 1 ;否则返回
0;}
定义一个函数。 C89不允许嵌套函数
定义,因此在C89中你只能使用函数定义外的
。
main ()
int main(无效)
{
int i;
int invalid = 0;
printf( 输入数字);
scanf("%d",& i);
scanf()通常不安全使用。
无效+ = getter(i);
这会尝试在现有函数定义中的代码中定义名为get_i的函数。
。即使你使用允许嵌套定义的C,但函数*定义*
也不能返回一个值,所以在
a需要rvalue的上下文。
}
最好从main()显式返回一个值。
这段代码不起作用。有人能帮我理解吗?
I have this macro. #include<stdio.h>
#include<stdlib.h>
#define getter(a) int get_##a (int a) { if(a>0) return 1; else return
0;}
That defines a function. C89 does not permit nested function
definitions, so in C89 you would only be able to use that outside
of a function definition.
main()
int main(void)
{
int i;
int invalid=0;
printf("enter the number");
scanf("%d",&i);
scanf() is often unsafe to use.
invalid+=getter(i);
This tries to define the function named get_i right at that point
in the code, inside an existing function definition. Even if you
were using a C that allowed nested definitions, a function *definition*
cannot return a value, so it would not be legal to use one in
a context that required an rvalue .
}
It is better to explicitly return a value from main().
This code doesn''t work . Can some one help me understand it??
为什么要为每个变量创建一个新的get_ *函数?
为什么不呢?只是创建一个getter -function-而不是一个宏?
你是否试图从本质上扩展getterinline,作为一个快捷方式
to在每个点写出代码?如果是这样那么定义
应该只是诸如
#define getter(a)((a)> 0)
如果比较结果为比较结果为1
如果为假则为0。
-
法律 - 它是一种商品
- Andrew Ryan(环球邮报,2005/11/26)
Why would you want to create a new get_* function for each variable?
Why not just create a single getter -function-, instead of a macro?
Are you trying to essentially expand getter "inline", as a shortcut
to writing out the code at each point? If so then the definition
should merely be something such as
#define getter(a) ((a)>0)
seeing as the result of a comparison is 1 if the comparison is true
and 0 if it is false.
--
"law -- it''s a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
>
2006年3月9日星期四21:16,Shastri认为(
< 11 ********************** @ i39g2000cwa.googlegroups .com>):
On Thursday 09 March 2006 21:16, Shastri opined (in
<11**********************@i39g2000cwa.googlegroups .com>):
我有这个宏。
#include< stdio.h> ;
#include< stdlib.h>
#define getter(a)int get _ ## a(int a){if(a> 0)return 1;否则返回
0;}
main()
{
int i;
int invalid = 0;
printf("输入数字) ;
scanf("%d"& i);
无效+ = getter(i);
}
我得到的错误是:
" get.c",第10行:语法错误之前或之后:int
" get.c",第10行:警告:语法错误:空声明
" get.c" ,第11行:语法错误之前或之后:}
这段代码不起作用。有人能帮我理解吗?
Hi,
I have this macro.
#include<stdio.h>
#include<stdlib.h>
#define getter(a) int get_##a (int a) { if(a>0) return 1; else return
0;}
main()
{
int i;
int invalid=0;
printf("enter the number");
scanf("%d",&i);
invalid+=getter(i);
}
The errors I got are:
"get.c", line 10: syntax error before or at: int
"get.c", line 10: warning: syntax error: empty declaration
"get.c", line 11: syntax error before or at: }
This code doesn''t work . Can some one help me understand it??
你的代码试图在另一个
函数中声明并调用一个函数。前者在C中是不可能的。
它也不可能在相同的
呼吸中声明和调用一个函数。即,你不能这样做:
int s = 42;
int x = int a_func(int s){return s};
甚至在任何函数之外。那就是你的第一个错误来自。
顺便说一句,GCC 4.0.2在迂腐模式下,我做网中间错误
你的。
-
BR,Vladimir
真正的程序员不会记录;如果它很难写,那就很难理解了。
Your code is trying to declare and invoke a function inside another
function. The former is not possible in C.
It''s also not possible to declare and invoke a function in the same
breath. I.e., you can''t do:
int s = 42;
int x = int a_func(int s) { return s };
even outside of any function. That''s where your first error comes from.
BTW, with GCC 4.0.2 in pedantic mode, I do net get the middle error of
yours.
--
BR, Vladimir
Real programmers don''t document; if it was
hard to write, it should be hard to understand.
2006年3月9日星期四21:42, Vladimir S. Oka认为(
< du ********* @ nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com>):
On Thursday 09 March 2006 21:42, Vladimir S. Oka opined (in
<du*********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com>):
2006年3月9日星期四21:16,Shastri认为(
< 11 ********************** @ i39g2000cwa.googlegroups .com>):
On Thursday 09 March 2006 21:16, Shastri opined (in
<11**********************@i39g2000cwa.googlegroups .com>):
我有这个宏。
#include< stdio.h> ;
#include< stdlib.h>
#define getter(a)int get _ ## a(int a){if(a> 0)return 1;否则返回
0;}
main()
{
int i;
int invalid = 0;
printf("输入数字) ;
scanf("%d"& i);
无效+ = getter(i);
}
我得到的错误是:
" get.c",第10行:语法错误之前或之后:int
" get.c",第10行:警告:语法错误:空声明
" get.c" ,第11行:语法错误之前或之后:}
这段代码不起作用。有人可以帮我理解吗?
Hi,
I have this macro.
#include<stdio.h>
#include<stdlib.h>
#define getter(a) int get_##a (int a) { if(a>0) return 1; else return
0;}
main()
{
int i;
int invalid=0;
printf("enter the number");
scanf("%d",&i);
invalid+=getter(i);
}
The errors I got are:
"get.c", line 10: syntax error before or at: int
"get.c", line 10: warning: syntax error: empty declaration
"get.c", line 11: syntax error before or at: }
This code doesn''t work . Can some one help me understand it??
你的代码试图在另一个
函数中声明和调用一个函数。前者在C中是不可能的。
也不可能在相同的呼吸声明和调用函数。即,你不能这样做:
int s = 42;
int x = int a_func(int s){return s};
甚至在外面任何功能。这就是你的第一个错误来自的地方。
Your code is trying to declare and invoke a function inside another
function. The former is not possible in C.
It''s also not possible to declare and invoke a function in the same
breath. I.e., you can''t do:
int s = 42;
int x = int a_func(int s) { return s };
even outside of any function. That''s where your first error comes
from.
这并不是说你做不到:
#include< stdio.h>
#include< stdlib.h>
#define getter(a)int get _ ## a(int a ){if(a> 0)返回1;否则返回
0;}
getter(i)
main()
{
int i;
int invalid = 0;
printf("输入数字);
scanf("%d"& i);
/ *请注意,在这里你必须知道''get_i` * /
无效= get_i(5);
}
除了我之外还有什么意义......
>
-
BR,弗拉基米尔
妄想并不意味着整个世界都不是为了得到你。
Which is not to say that you couldn''t do:
#include<stdio.h>
#include<stdlib.h>
#define getter(a) int get_##a (int a) { if(a>0) return 1; else return
0;}
getter(i)
main()
{
int i;
int invalid=0;
printf("enter the number");
scanf("%d",&i);
/* note that here you have to know it''s `get_i` */
invalid = get_i(5);
}
What would be the point is beyond me...
--
BR, Vladimir
Paranoia doesn''t mean the whole world isn''t out to get you.
这篇关于宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!