C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

what are these macros doing?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Steven T. Hatton
Guest





PostPosted: Tue Jul 26, 2005 10:59 pm    Post subject: what are these macros doing? Reply with quote



I'm trying to figure out if this code (which I'm sure was written for a C
compiler, could be considered legal C++. My confusion is from the last bit
of code at the end. Could someone explain to me what the reason for this
is?

#ifndef MAIN_H
#define MAIN_H

/*
* function prototype insulation
*/
#ifndef _
# ifdef __STDC__
# define _(x) x
# else
# define _(x) ()
# endif
#endif /* _ */

/*
* array manipulation
*/
#define SIZEOF(a) (sizeof(a) / sizeof(a[0]))
#define ENDOF(a) ((a) + SIZEOF(a))

/*
* Take the define out of comments to debug
* the utilities.
* /
#define DEBUG
*/

#endif /* MAIN_H */

/* hexdump.h */

#ifndef HEXDUMP_H
#define HEXDUMP_H

#include <main.h>

void hexdump _((char *infile, char *outfile, long start, long finish,
int by_lines, int width, int no_ascii, int radix));

#endif /* HEXDUMP_H */



/* hexdump.c */
void
hexdump(infile, outfile, start, finish, by_lines, width, no_ascii, rdx)
char *infile;
char *outfile;
long start;
long finish;
int by_lines;
int width;
int no_ascii;
int rdx;
{
FILE *input;
FILE *output;
long addr;
long nlines;
int state;
int c;
unsigned char *line;
int addr_width;
int byte_width;
/*...*/
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Back to top
Stephen Howe
Guest





PostPosted: Tue Jul 26, 2005 11:41 pm    Post subject: Re: what are these macros doing? Reply with quote



Quote:
/* hexdump.c */
void
hexdump(infile, outfile, start, finish, by_lines, width, no_ascii, rdx)
char *infile;
char *outfile;
long start;
long finish;
int by_lines;
int width;
int no_ascii;
int rdx;
{
FILE *input;
FILE *output;
long addr;
long nlines;
int state;
int c;
unsigned char *line;
int addr_width;
int byte_width;
/*...*/

The last line is comment characters. Valid C and C++.
And the line before that is the declaration of a local variable of type int.

Stephen Howe

Stephen Howe



Back to top
GB
Guest





PostPosted: Wed Jul 27, 2005 2:43 am    Post subject: Re: what are these macros doing? Reply with quote



Steven T. Hatton wrote:
Quote:
I'm trying to figure out if this code (which I'm sure was written for a C
compiler, could be considered legal C++. My confusion is from the last bit
of code at the end. Could someone explain to me what the reason for this
is?


I assume you are asking about this:

Quote:
#ifndef _
# ifdef __STDC__
# define _(x) x
# else
# define _(x) ()
# endif
#endif /* _ */

and this:

Quote:
void hexdump _((char *infile, char *outfile, long start, long finish,
int by_lines, int width, int no_ascii, int radix));

The first sequence defines a macro named "_" that evaluates to its
argument if __STDC__ is defined and discards its argument if __STDC__ is
not defined.

It is then used to automatically discard the prototype arguments when
being compiled by a K&R C (pre-ANSI C) compiler, which does not have
prototypes. Notice the extra pair of parentheses that allow the whole
parameter list to be treated as a single macro argument.

Gregg

Back to top
GB
Guest





PostPosted: Wed Jul 27, 2005 2:47 am    Post subject: Re: what are these macros doing? Reply with quote

GB wrote:
Quote:

#ifndef _
# ifdef __STDC__
# define _(x) x
# else
# define _(x) ()
# endif
#endif /* _ */

The first sequence defines a macro named "_" that evaluates to its
argument if __STDC__ is defined and discards its argument if __STDC__ is
not defined.

Correction: it replaces its argument with a single pair of parentheses
if __STDC__ is not defined. It does not discard it.

Gregg

Back to top
Alan Johnson
Guest





PostPosted: Wed Jul 27, 2005 9:38 pm    Post subject: Re: what are these macros doing? Reply with quote

Steven T. Hatton wrote:
Quote:
I'm trying to figure out if this code (which I'm sure was written for a C
compiler, could be considered legal C++. My confusion is from the last bit
of code at the end. Could someone explain to me what the reason for this
is?

I assume the part that is confusing is how the variable list is declared
for this function:

Quote:
/* hexdump.c */
void
hexdump(infile, outfile, start, finish, by_lines, width, no_ascii, rdx)
char *infile;
char *outfile;
long start;
long finish;
int by_lines;
int width;
int no_ascii;
int rdx;
{

This is the now very old K&R syntax for function parameters. I believe
C99 removes this syntax from the grammar. I'm not sure if it is still
(or ever was) valid C++. In any case, this code can easily be rewritten
to follow the modern style by putting the parameter types before their
names, instead of in a separate list. There are likely tools available
that do that for you.

-Alan

Back to top
Default User
Guest





PostPosted: Wed Jul 27, 2005 11:05 pm    Post subject: Re: what are these macros doing? Reply with quote

Alan Johnson wrote:


Quote:
I assume the part that is confusing is how the variable list is
declared for this function:

/* hexdump.c */
void
hexdump(infile, outfile, start, finish, by_lines, width, no_ascii,
rdx) char *infile;
char *outfile;
long start;
long finish;
int by_lines;
int width;
int no_ascii;
int rdx;
{

This is the now very old K&R syntax for function parameters. I
believe C99 removes this syntax from the grammar.


No, still valid. Still deprecated as well, but way too much prior code
written to remove support.

Implict function declarations have gone away though, so no more:

main()
{
}




Brian

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.