Macro

S

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
 
W

Walter Roberson

Shastri said:
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.

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.
 
V

Vladimir S. Oka

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.
 
V

Vladimir S. Oka

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...
 
M

Micah Cowan

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.
 
M

Martin Ambuhl

Shastri said:
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;
}
 
S

Shastri

Walter said:
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.


int main(void)


scanf() is often unsafe to use.


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().



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.

All,

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


Thanks
Shastri
 
K

Keith Thompson

C

CBFalconer

Walter said:
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/>
 
W

Walter Roberson

Shastri said:
Thanks for your comments. But I studied
here(http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_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 );
 
W

Walter Roberson

Walter Roberson wrote:
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' ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,627
Members
45,328
Latest member
66Teonna9

Latest Threads

Top