By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,127 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,127 IT Pros & Developers. It's quick & easy.

what are these macros doing?

P: n/a
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
Jul 26 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> /* 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
Jul 26 '05 #2

P: n/a
GB
Steven T. Hatton wrote:
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
Jul 27 '05 #3

P: n/a
GB
GB wrote:
#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
Jul 27 '05 #4

P: n/a
Steven T. Hatton wrote:
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
Jul 27 '05 #5

P: n/a
Alan Johnson wrote:

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
Jul 27 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.