external variables

K

Keith Thompson

Larry Gates said:
What values does one assume as the processor steps through these?

The processor *doesn't* step through them. These are declarations,
not statements that the program executes as the program is running.
The net effect is to define an object named "one", of type int, with
file scope, external linkage, and static storage duration, with an
initial value of 1.

(Note that declarations within a function often *are* executed as the
program is running, but file-scope declarations generally are not.)

(In a typical implementation, the value 1 will probably be stored in
the executable file and loaded into memory along with the executable
code when the program starts up; the system will arrange for the value
1 to be loaded into memory in the location where the object will live
once the program starts executing. But many other methods are
possible; the standard says very little about how this is
implemented.)
 
L

Larry Gates

Those are outside any function and don't get executed. On most
machines, the effect of all that nonsense is just that a variable "one"
is created, which is initialized to 1 when the program is loaded (before
main(), in particular).

Let me rephrase: how does the preprocessor think about subsequent
declarations. Does the last one win?
 
L

Larry Gates


The One Definition Rule (ODR) is an important concept in the C++
programming language. It's defined in the ISO C++ Standard(ISO/IEC 14882)
2003, at section 3.2.

In short the ODR states that:
In any translation unit, a template, type, function, or object can have no
more than one definition. Some of these can have any number of
declarations. A definition provides an instance.
In the entire program, an object or non-inline function cannot have more
than one definition; if an object or function is used, it must have exactly
one definition. You can declare an object or function that is never used,
in which case you don't have to provide a definition. In no event can there
be more than one definition.
Some things, like types, templates, and extern inline functions, can be
defined in more than one translation unit. For a given entity, each
definition must be the same. Non-extern objects and functions in different
translation units are different entities, even if their names and types are
the same.

What would qualify as a translation unit in the example of the reverse
polish calculator?
--
larry gates

Perhaps you should compile your Perl with long doubles one of these
megaseconds.
-- Larry Wall in <[email protected]>
 
I

Ian Collins

Larry said:
Let me rephrase: how does the preprocessor think about subsequent
declarations. Does the last one win?

The preprocessor ignores them, they aren't preprocessor tokens or
directives.
 
K

Keith Thompson

Larry Gates said:
Let me rephrase: how does the preprocessor think about subsequent
declarations. Does the last one win?

First you asked about the processor; now you're asking about the
preprocessor.

The preprocessor has nothing to do with this.
 
L

Larry Gates

The preprocessor ignores them, they aren't preprocessor tokens or
directives.

Wouldn't one normally make these tentative declarations using the C
equivalent to coco from fortran, which is the # ?
--
larry gates

But at some point you just give up and call it cheating, er,
I mean, AOP. :)
-- Larry Wall in <[email protected]>
 
N

Nate Eldredge

Larry Gates said:
Let me rephrase: how does the preprocessor think about subsequent
declarations. Does the last one win?

It's not the preprocessor, but the compiler. And there's no question of
which one "wins"; they all have to agree about the type and linkage of
the variable in question, and only once can you have an initialization.
If you added to that list

static int one;

or

double one;

or

int one = 2;

then there would be a compiler error. Later declarations don't replace
previous ones; either they agree, or they're invalid.
 
S

Stefan Ram

Ian Collins said:
What do you think qualifies as a translation unit?

»After preprocessing, a preprocessing translation unit
is called a translation unit.«

ISO/IEC 9899:1999 (E), 5.1.1.1, #1
 
R

Richard Bos

Larry Gates said:
Wouldn't one normally make these tentative declarations using the C
equivalent to coco from fortran, which is the # ?

One would drink the cocoa (Dutch process, please, keine schweizerische
Schlamperei erlaubt), and tentative declarations have as much to do with
the preprocessors as Jerries have with chocolate.

Richard
 
R

Richard Bos

Larry Gates said:
What would qualify as a translation unit in the example of the reverse
polish calculator?

As you well know, the reverse Polish manoeuvre consists _at least_ of a
division of horse.

Richard
 
L

Larry Gates

One would drink the cocoa (Dutch process, please, keine schweizerische
Schlamperei erlaubt), and tentative declarations have as much to do with
the preprocessors as Jerries have with chocolate.

Richard

What I meant was hash, something you and your countrymen know better than
we here as Texas' neighbor.

#ifdef

#elseif

My question is wouldn't you normally do tentative definitions with #define
and #undefine and their cohort?

Wie es die andere Frage angeht, meine Liebliengschockolade kommt aus dem
Ort, wo ich sie mir hole. Zu Chicago hatten wir sehr tolle Polnische.
Vor ein paar Monaten hatten wir ein russisches Laden, wo ich ganz leckere
Schockoladen erfahren habe. Der ist leide pleitte geworden, wie so viele
andere waehrend der Bush Jahren. Nun ist mein Lieblings Lindt, bei
Smith's.
 
L

Larry Gates

As you well know, the reverse Polish manoeuvre consists _at least_ of a
division of horse.

Richard

Maybe you have different whores on division or a horse of a different
color.
 
G

Guest

(one) is initialized as an external object with a value of (1).




Yes.

er, why is it always of type char* why does the unadorned
0 have thype char*. This is a genuine confusion I'm having!

The type of a conditional expression
which has as its second and third operands:
        a null pointer constant
    and a pointer,

ok, but what is the type of a null pointer constant?
is the type of the pointer, regardless of the test condition.

but what type of pointer?
 
G

Guest

the processor doesn't see these. it likley compiles to some
sort of assembler or loader directive.

this has nothing to do with the preprocessor.
Wouldn't one normally make these tentative declarations using the C
equivalent to coco from fortran, which is the # ?

you'll have to explain what "coco" is. Some Fortran thingy?
 
G

Guest

#ifdef

#elseif

My question is wouldn't you normally do tentative definitions with #define
and #undefine and their cohort?

no. The preprocessor has nothing to do with definitions, tentative or
otherwise. The preprocessor is strictly a textual substitution phase
and has no understanding of C syntax or semantics. the C preprocessor
even gets used on Fortarn programs (I believe).

<snip>
 
K

Keith Thompson

Boon wrote: [...]
AFAICT, (sizeof *(one ? 0 : (char *)0)) is always 1
(Because the above ?: expression has type char *)

Yes.

er, why is it always of type char* why does the unadorned
0 have thype char*. This is a genuine confusion I'm having!

The unadorned 0 doesn't have type char*; it has type int, as it always
does. It also happens to be a null pointer constant. (char*)0 has
type char*, and is not a null pointer constant. The standard's
description for the conditional operator says "if one operand is a
null pointer constant, the result has the type of the other operand",
so the expression (one ? 0 : (char *)0) has type char*.
ok, but what is the type of a null pointer constant?

It depends on the null pointer constant. The fact that an expression
is a null pointer constant doesn't affect its type. For example, 0,
0ULL, and (void*)0 are all null pointer constants; their types are
int, unsigned long long, and void*, respectively. (A null pointer
constant may be converted to some pointer type, but the expression
itself has whatever type it would have if it weren't a null pointer
constant.)
but what type of pointer?

In this case, char*. If the second and third operands are a null
pointer constant and an expression of pointer type (in either order),
then the type of the null pointer constant is irrelevant; the result
of the conditional operator has the type of the operand that isn't a
null pointer constant.

It almost looks like there might be an ambiguity if both the second
and third operands are null pointer constants, but I think all
possible cases are unambiguous. If both are integer constant
expressions (with the value zero), then the rule for when both
operands have arithmetic type applies, since neither operand is a
pointer. If one is an integer constant expression and the other is of
type void*, then the rule for when one is a pointer (the void*
operand) and the other is a null pointer constant (the integer
constant expression) applies, and the result is of type void* (and is
a null pointer, whichever way the condition goes). If both are of
type void*, then the result is of type void* (and is a null pointer,
whichever way the condition goes).

The type of a null pointer constant is always either an integer type
or void*.

[snip]
 
G

Guest

What would qualify as a translation unit in the example of the reverse
polish calculator?

the C files (plus their includes). p82 shows all the files.
main.c, getop.c, stack.c and getch.c all correspond to translation
units.
The include file calc.h does not.


--
Nick Keighley

Once upon a time people could come to this newsgroup for a friendly
exchange of
calling each other's nonsense. One lurked in the shadows, one hand on
the
handle of his katana, waiting to leap out and strike down Dan Pop or
Lawrence
Kirby upon the slightest sign (or should we say odor) of bullschildt.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top