C pre Processor

C

candy

hi all,

Ive to make a C pre processor for my semester project.
Can anybody please tell me the functions which are performed by the c
pre processor?

Well,is it true that one of the funcitons performed by the CPP is to
include the source
code of the included header file in the original source code file.eg
include the source code
of stdio.h if this file is used in the .c file.

Thanks in advance
Candy
 
T

Trent Buck

Quoth candy on or about 2004-11-14:
I've to make a C pre processor for my semester project.
Can anybody please tell me the functions which are performed by the c
pre processor?

Open a terminal and type `man cpp'. Then open K&R2 to Chapter 4,
section 11 and read it. That will get you started.

If you have no idea what I'm talking about, you will probably have
*extreme* difficulty writing a C preprocessor.
Well,is it true that one of the funcitons performed by the CPP is to
include the source
code of the included header file in the original source code file.eg
include the source code
of stdio.h if this file is used in the .c file.

That is one of the things the preprocessor does. The preprocessor can
also do conditional compilation and macro substitution.

-trent
 
R

Robert Gamble

hi all,

Ive to make a C pre processor for my semester project. Can anybody
please tell me the functions which are performed by the c pre processor?

Well,is it true that one of the funcitons performed by the CPP is to
include the source
code of the included header file in the original source code file.eg
include the source code
of stdio.h if this file is used in the .c file.

This seems like quite an undertaking for someone who doesn't appear to be
even vaguely familiar with the preprocessor. The first thing you should
do is determine exactly which responsibilities of the preprocessor
(hopefully not all) you will be expected to handle. Then you should
obtain a copy of the Standard which describes in detail the functions of
the preprocesser (namely section 6.10) assuming you were not given a
detailed list of requirements (an unreasonable assumption in my opinion).
This basically boils down to the conditional directives (#if/#elif/#else,
#ifdef/#ifndef/#endif), file inclusion (#include "file"/<file>), macros
(#define/defined) and the various intricacies associated with them, the
line/error/pragma/etc directives, and the predefined macros. This is not
a comprehensive list and there are many complexities to be dealt with.

Rob Gamble
 
R

Robert Gamble

Quoth candy on or about 2004-11-14:

Open a terminal and type `man cpp'. Then open K&R2 to Chapter 4,
section 11 and read it. That will get you started.

On my system "man cpp" provides very little information about the tasks
performed by the preprocessor and lots of information about the options
the program supports.

Although Section 4.11 of K&R2 provides a decent overview of the
preprocessor, this is a far cry from the information that will be required
when writing a program to handle even a subset of the standard
functionality.
If you have no idea what I'm talking about, you will probably have
*extreme* difficulty writing a C preprocessor.


That is one of the things the preprocessor does. The preprocessor can
also do conditional compilation and macro substitution.

The preprocessor does not compile anything, it performs conditional
inclusion, not compilation.

Rob Gamble
 
M

Malcolm

candy said:
Ive to make a C pre processor for my semester project.
Can anybody please tell me the functions which are performed by the c
pre processor?
As other people have said, a C pre-processor is quite a difficult program to
write. The problem is that the output and input is quite tightly defined by
the language specification, and if you don't implement every detail of, say,
macro expansion, the the program is a nuisance to anyone trying to use it.
Well,is it true that one of the funcitons performed by the CPP is to
include the source code of the included header file in the original source code
file.eg include the source code of stdio.h if this file is used in the .c file.
You need to get a C language specification. The #directives are for the
preprocessor, the most important of which are #include and #define. However
you also have to support conditional compilation - the #if family of
directives.

Input and output is straight text - all the directive are essentially
wordprocessing commands.

The place to start is probably the #include statement. However even this is
complicated, bacause you need search paths defined. Then you probably need
the #ifdef, because most real programs use "include guards" to prevent
double inclusion.
 
T

Trent Buck

Quoth Robert Gamble on or about 2004-11-14:
...this is a far cry from the information that will be required
when writing a program to handle even a subset of the standard
functionality.

I was trying to give the OP a starting point. (S)he sounded like she
was starting from almost first principles, so slamming the standard down
on the desk and saying `off you go, then' didn't seem very helpful.
The preprocessor does not compile anything, it performs conditional
inclusion, not compilation.

Sorry. I haven't elevated my pedantry enough to pass muster in here :)

-trent
 
M

Merrill & Michele

Malcolm said:
As other people have said, a C pre-processor is quite a difficult program to
write. The problem is that the output and input is quite tightly defined by
the language specification, and if you don't implement every detail of, say,
macro expansion, the the program is a nuisance to anyone trying to use it. source ..c
You need to get a C language specification. The #directives are for the
preprocessor, the most important of which are #include and #define. However
you also have to support conditional compilation - the #if family of
directives.

Input and output is straight text - all the directive are essentially
wordprocessing commands.

The place to start is probably the #include statement. However even this is
complicated, bacause you need search paths defined. Then you probably need
the #ifdef, because most real programs use "include guards" to prevent
double inclusion.

What's nasty about double inclusion? MPJ
 
R

Robert Gamble

[snip]
The place to start is probably the #include statement. However even this is
complicated, bacause you need search paths defined. Then you probably need
the #ifdef, because most real programs use "include guards" to prevent
double inclusion.

What's nasty about double inclusion? MPJ

Header files usually contain object definitions and an object defined
multiple times can cause compiler errors.

Rob Gamble
 
K

Keith Thompson

Robert Gamble said:
Header files usually contain object definitions and an object defined
multiple times can cause compiler errors.

Header files generally shouldn't contain object definitions (though
they may contain object declarations). There's nothing in the
language to forbid it, but it's generally not a good idea.
 
L

Lawrence Kirby

....



Header files usually contain object definitions and an object defined
multiple times can cause compiler errors.

Headers typically shouldn't contain object definitions. They can contain
declarations but those can be repeated. A signficiant problem is type
definitions. E.g. typedefs and structure/union/enum definitions that
specify members/values can't be repeated so will generate compile errors
if compiled twice.

Lawrence
 
R

Robert Gamble

Header files generally shouldn't contain object definitions (though
they may contain object declarations). There's nothing in the
language to forbid it, but it's generally not a good idea.

I should have been more clear, I was specifically thinking about typedef
definitions which are quite common. Although not good practice it is not
that rare to see other type of definitions in a header file either (short
wrapper functions for example).

Rob Gamble
 
M

Marcus Lessard

This seems like quite an undertaking for someone who doesn't appear to be
even vaguely familiar with the preprocessor.

I think "candy" is trolling the mighty pedants of c.l.c

heheh
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top