473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

role of preprocessor

I know that for my own programs, the things that precede the main call are
either remarks or begin with a hash. What else can you put up there? MPJ
Nov 14 '05 #1
6 2257
Merrill & Michele wrote:
I know that for my own programs, the things that precede the main call are
either remarks or begin with a hash. What else can you put up there? MPJ


Declarations and definitions of functions, types, and
variables. In short, any (correct) code:

#include <stdio.h>

typedef enum { ENGLISH, LATIN } Language;

static const char *salutation(Language l) {
return (l == ENGLISH) ? "Hello" : "Ave";
}

static const char *divider = ", ";

static const char *planet(Language l) {
return (l == LATIN) ? "Terra" : "World";
}

static const char closing[] = "!\n";

static void printstring(const char *string) {
fputs (string, stdout);
}

int main(int argc, char **argv) {
Language tongue = (argc > 1) ? LATIN : ENGLISH;
printstring(salutation(tongue));
printstring(divider);
printstring(planet(tongue));
printstring(closing);
return 0;
}

(This is, of course, a clumsy program for its task. The
point was to exhibit things that could precede main(), not
necessarily things that *should* precede main(). Note also
that most "significant" C programs have many independently-
compiled source files yet only one main(); it follows that
main() is not a necessary part of every source file.)

--
Er*********@sun.com

Nov 14 '05 #2
Merrill & Michele wrote:
I know that for my own programs, the things that precede the main call are either remarks or begin with a hash. What else can you put up there?
"Eric Sosman" :
Declarations and definitions of functions, types, and
variables. In short, any (correct) code:

#include <stdio.h>

typedef enum { ENGLISH, LATIN } Language;

static const char *salutation(Language l) {
return (l == ENGLISH) ? "Hello" : "Ave";
}

static const char *divider = ", ";

static const char *planet(Language l) {
return (l == LATIN) ? "Terra" : "World";
}

static const char closing[] = "!\n";

static void printstring(const char *string) {
fputs (string, stdout);
}

int main(int argc, char **argv) {
Language tongue = (argc > 1) ? LATIN : ENGLISH;
printstring(salutation(tongue));
printstring(divider);
printstring(planet(tongue));
printstring(closing);
return 0;
}

(This is, of course, a clumsy program for its task. The
point was to exhibit things that could precede main(), not
necessarily things that *should* precede main(). Note also
that most "significant" C programs have many independently-
compiled source files yet only one main(); it follows that
main() is not a necessary part of every source file.)


How about:
int i = 0;
++ i;
int main (){
return 0
}
MPJ

(I can't imagine that that is kosher, but I'm frequently surprised.)
Nov 14 '05 #3
Merrill & Michele <be********@comcast.net> scribbled the following:
How about:
int i = 0;
++ i;
int main (){
return 0
}
MPJ (I can't imagine that that is kosher, but I'm frequently surprised.)


This should not even compile. You cannot put executable statements
outside functions.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"C++ looks like line noise."
- Fred L. Baube III
Nov 14 '05 #4
Merrill & Michele wrote:
Merrill & Michele wrote:
I know that for my own programs, the things that precede the main call
are
either remarks or begin with a hash. What else can you put up there?

"Eric Sosman" :
Declarations and definitions of functions, types, and
variables. In short, any (correct) code:
[...]


How about:
int i = 0;


Fine: this is a variable definition, with an
initializer.
++ i;
Not fine: this is an executable statement, and an
executable statement can exist only inside the body of a
function definition. It cannot "stand alone."
int main (){
return 0
Not fine: a semicolon is required.
}
MPJ

(I can't imagine that that is kosher, but I'm frequently surprised.)


Examine my list of possibilities again: You can have
(1) declarations and (2) definitions of (A) functions,
(B) types, and (C) variables. That's six combinations:

1A: Function declarations
int foo(double);
void bar(long time_no_see);

1B: Type declarations
struct this { double toil; double trouble; };
union jack { char coal[128]; long john_silver; };
enum fruit { APPLE, BANANA, CRANBERRY };

1C: Variable declarations
extern struct this global_that;
extern double global_trouble;

2A: Function definitions
int foo(double x) { return x > 0.0; }
void bar(long time_no_see) {
printf ("%ld\n", time_no_see);
}

2B: Type definitions
(Well, I lied: You can't really "define" a type.
Chalk it up to a failed attempt at brevity.)

2C: Variable definitions
static double electricity;
char *situation = "hopeless";
enum fruit peelable_fruits[] = {
APPLE, BANANA };

Now, to ward off my fellow pedants let me emphasize
that there are several things wrong with this list. The
main thing is that every definition also serves as a
declaration, so the nice, neat "1 or 2" categorization
above isn't quite right. Also, the Standard talks about
something called a "tentative definition," which is really
just a formalization of how the information about a single
data object can be gathered from several bits of source.
Judging from some of the questions you've asked I suspect
that a thorough study of such subtleties would do more harm
than good at this point; return to the matter once your
grasp of the structure of C programs is firmer.

--
Er*********@sun.com

Nov 14 '05 #5
Merrill & Michele wrote:

How about:
int i = 0;
This you can do. Though you may have heard that global variables
should be viewed with suspicion. Sometimes they are the best
solution to your problem, but most often they are to be avoided.
++ i;


This you can not do.

--
Thomas.
Nov 14 '05 #6
> > MPJ:
[outside of a function]
++ i;
Thomas Stegen et ali:
This you can not do.


Thanks all for replies, in particular Mr. Sosman with his generous detail.
Ever since I was made aware of K&R2 ex 1-24, I've been thinking about how
I'd do it. Of course, if I solved the problem completely, I would have
something darn near a compiler.
------------------------
A bottle of red
A bottle of white
I'm relaxing at
Mama's tonight

Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: craig | last post by:
I am working on my first .NET development project that involves custom role-based security per the project requirements. This lead to a general design issue this week that really caused us some...
205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
24
by: Nudge | last post by:
I have an array, and an unrolled loop which looks like this: do_something(A); do_something(A); .... do_something(A); I thought: why should I type so much? I should write a macro. So I was...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
64
by: Merrill & Michele | last post by:
#include <stdio.h> int main(void) { int i = 0; ++ i; ++ i; printf("i equals %d\n", i); return 0; } I've been trying to determine the great and manifold uses of the semicolon
9
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
4
by: cybertoast | last post by:
i seem to have some misunderstanding about how roles work in sql server 2005. i see that i can add a role to a database (dbname->->properties->permissions->. THis allows me to add either users or...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
2
by: Anthony Smith | last post by:
I have a user object that is set when a user logs in. There are also permissions that I get about the user from a web service. Currently I take the results from those web services and store them as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.