what are these macros doing?

S

Steven T. Hatton

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;
/*...*/
 
S

Stephen Howe

/* 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
 
G

GB

Steven said:
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:
#ifndef _
# ifdef __STDC__
# define _(x) x
# else
# define _(x) ()
# endif
#endif /* _ */

and this:
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
 
G

GB

GB said:
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
 
A

Alan Johnson

Steven said:
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:
/* 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
 
D

Default User

Alan Johnson wrote:

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


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
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top