473,661 Members | 2,440 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 1899
In article <11************ **********@i39g 2000cwa.googleg roups.com>,
Shastri <sa*****@gmail. com> wrote:
I have this macro. #include<stdio .h>
#include<stdli b.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("ente r the number");
scanf("%d",&i) ;
scanf() is often unsafe to use.
invalid+=gette r(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************ **********@i39g 2000cwa.googleg roups.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*********@nw rdmz02.dmz.ncs. ea.ibs-infra.bt.com>):
On Thursday 09 March 2006 21:16, Shastri opined (in
<11************ **********@i39g 2000cwa.googleg roups.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.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <11************ **********@i39g 2000cwa.googleg roups.com>,
Shastri <sa*****@gmail. com> wrote:
I have this macro.

#include<stdio .h>
#include<stdli b.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.barracu danetworks.com> ,
Micah Cowan <mi***@cowan.na me> wrote:
ro******@ibd.n rc-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.barracu danetworks.com> ,
Micah Cowan <mi***@cowan.na me> wrote:
ro******@ibd. nrc-cnrc.gc.ca (Walter Roberson) writes:


That defines a function. C89 does not permit nested function
definition s, 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************ **********@i39g 2000cwa.googleg roups.com>,
Shastri <sa*****@gmail. com> wrote:
I have this macro.

#include<stdio .h>
#include<stdli b.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("ente r the number");
scanf("%d",&i) ;


scanf() is often unsafe to use.
invalid+=gette r(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_Keit h) 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

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

Similar topics

25
3234
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 clarification on what 'macro' means. A Lisp macro is a way of modifying code when that code is first defined. It can rearrange the structure of the code, and add and remove parts of it. Unlike C's #define macro language, Lisp macros understand the...
699
33869
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
2
3483
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 menus by customizing a toolbar. With this method you have to create a separate macro for every single menu and sub menu. The old method would allow me to include several menus (File/ print/ page setup ...) all within one macro. Now it seems I...
7
23541
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. Execution is faster. Where will it be stotred?(Is it in bss/stack/?) FUNCTION:
3
8470
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(); bar();")
8
10862
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
12
6518
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 B() -nothing, *warning ISO C99 requires rest arguments to be used*
6
6430
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 Autoexec Macro looks like the way to go. Could someone please assist? Thank you very much! Mike
5
3481
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 the cursor was on in the datasheet at the time I executed the macro. So, the questions are: 1) In the macro, how to I get my hands on the record key or record data of the record the cursor was on in the datasheet at the time I executed the...
0
2049
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 data again and delete the unwanted data and repeat few more times in new sheets. End product will be apprximately 7 or 8 sheets - 1 for Active Customers, Inactive Customers, Pending Installs, Etc... I'm getting hung up I believe with naming...
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8758
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7364
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.