473,507 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Macro

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

Mar 9 '06 #1
12 1880
In article <11**********************@i39g2000cwa.googlegroups .com>,
Shastri <sa*****@gmail.com> wrote:
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??


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)
Mar 9 '06 #2
On Thursday 09 March 2006 21:16, Shastri opined (in
<11**********************@i39g2000cwa.googlegroups .com>):
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??


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.

Mar 9 '06 #3
On Thursday 09 March 2006 21:42, Vladimir S. Oka opined (in
<du*********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com>):
On Thursday 09 March 2006 21:16, Shastri opined (in
<11**********************@i39g2000cwa.googlegroups .com>):
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??


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.


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.

Mar 9 '06 #4
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11**********************@i39g2000cwa.googlegroups .com>,
Shastri <sa*****@gmail.com> wrote:
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.


This hasn't changed in recent versions of the C standard, either.
Mar 9 '06 #5
In article <87************@mcowan.barracudanetworks.com>,
Micah Cowan <mi***@cowan.name> wrote:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
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.

This hasn't changed in recent versions of the C standard, either.


What? You mean they didn't cannonize gcc yet? ;=)

--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Mar 9 '06 #6


Walter Roberson wrote On 03/09/06 17:41,:
In article <87************@mcowan.barracudanetworks.com>,
Micah Cowan <mi***@cowan.name> wrote:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:


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.


This hasn't changed in recent versions of the C standard, either.

What? You mean they didn't cannonize gcc yet? ;=)


No; they cannonized the Great Zacchini.

--
Er*********@sun.com

Mar 9 '06 #7
Shastri wrote:
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);
} This code doesn't work . Can some one help me understand it??


It appears to be trying to implement the following in an obscure and
illegal way (using illegal nested functions and attempting to assign the
illegal nested function to an int):

#include <stdio.h>
#include <stdlib.h>

#define getter(a) ((a)>0)

int main(void)
{
int i;
int invalid = 0;
printf("enter the number: ");
fflush(stdout);
scanf("%d", &i);
invalid += getter(i);
return 0;
}

Mar 9 '06 #8

Walter Roberson wrote:
In article <11**********************@i39g2000cwa.googlegroups .com>,
Shastri <sa*****@gmail.com> wrote:
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??


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)


All,

Thanks for your comments. But I studied
here(http://www.experts-exchange.com/Prog..._21737426.html)
that Gcc would support nested function definitions. Isn't that true??
Thanks
Shastri

Mar 10 '06 #9
"Shastri" <sa*****@gmail.com> writes:
[...]
Thanks for your comments. But I studied
here(http://www.experts-exchange.com/Prog..._21737426.html)
that Gcc would support nested function definitions. Isn't that true??


That may well be true, but nested functions are a language extension,
not part of standard C, which is what we discuss here.

If you want to discuss the C-like language implemented by gcc, try a
newsgroup or mailing list with "gcc" in its name.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 10 '06 #10
Walter Roberson wrote:
Micah Cowan <mi***@cowan.name> wrote:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:

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.

This hasn't changed in recent versions of the C standard, either.


What? You mean they didn't cannonize gcc yet? ;=)


Why do you want to fire possibly red hot iron balls at that poor
innocent grazing gnu? Maybe you were thinking of 'canonize'?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Mar 10 '06 #11
In article <11**********************@v46g2000cwv.googlegroups .com>,
Shastri <sa*****@gmail.com> wrote:
Thanks for your comments. But I studied
here(http://www.experts-exchange.com/Prog..._21737426.html)
that Gcc would support nested function definitions. Isn't that true??


That's why I gave my answer in terms of C89. gcc's support for
nested function definitions is not part of any C standard.

Also, as I indicated, a function -definition- does not return
a value, so you cannot say anything like

result = return_type function_name( parameter_list ) { body; }

If you insisted on using (non-standard) nested function definitions,
you would have to break it up into pieces:

return_type function_name( parameter_list ) { body; }

result = function_name( actual_argument_list );
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Mar 10 '06 #12
In article <44***************@yahoo.com>,
CBFalconer <cb********@maineline.net> wrote:
Walter Roberson wrote:
What? You mean they didn't cannonize gcc yet? ;=)

Why do you want to fire possibly red hot iron balls at that poor
innocent grazing gnu? Maybe you were thinking of 'canonize'?


Nah, I meant 'cannonize': to build something up with a lot of hype
and then launch it with a bang, ignoring the fact that it can't say
up because it doesn't have a good foundation underneath it.

On the other hand, I wasn't above punning on 'canonize' ;-)
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Mar 10 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
3211
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
33325
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
2
3463
by: Pete | last post by:
In Access 95/97 I used to be able to create pull down menus (File,Edit ...) from a macro. It seems there used to be some wizard for that. However in Access 2000 it seems you have to build your...
7
23528
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
3
8460
by: Alexander Ulyanov | last post by:
Hi all. Is it possible to pass the whole blocks of code (possibly including " and ,) as macro parameters? I want to do something like: MACRO(FOO, "Foo", "return "Foobar";", "foo();...
8
10848
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
12
6486
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x ...
6
6424
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
5
3473
by: Bill | last post by:
This database has no forms. I am viewing an Access table in datasheet view. I'd like to execute a macro to execute a function (using "runcode"). In the function, I'll reading data from the record...
0
2033
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.