!!<====== Existence of a variable - Debunk a myth ======>!!

Z

Zero

Hi there,

I am a little bit irritated!
Consider a function fA which has a local variable iA (storage class is
auto).
Now, some books tell you,
that a variable does not exist until a value is passed to it, like
iA = 5;
Why is is then possible to write commands like
printf("%d", &iA);
Is this a random value or is it really the location of the variable?

Or consider the following code:
int iA;
int piA;

piA = &iA;

As some books say, iA does not exist, because not value is stored,
but to which address points piA.

So what really happens, when the function fA is entered?
What is the right place to read something about this topic.

Greetings from Zeh Mau
 
H

Harald van Dijk

!!<====== Existence of a variable - Debunk a myth ======>!!

That's a bit excessive, no?
Hi there,

I am a little bit irritated!
Consider a function fA which has a local variable iA (storage class is
auto).
Now, some books tell you,
that a variable does not exist until a value is passed to it, like iA =
5;

If that's what those books say, those books are wrong. iA does exist as an
object, but it doesn't have a value yet.
Why is is then possible to write commands like printf("%d", &iA);

To print a pointer value, use %p and cast to void *:
printf("%p", (void *) &iA);

%d is for printing integers. Pointers are not integers, and are not always
represented as integers.

Printing the address of iA is possible because iA exists already.
Is this a random value or is it really the location of the variable?

It is the address of iA.
Or consider the following code:
int iA;
int piA;

piA = &iA;

This is also fine.
As some books say, iA does not exist, because not value is stored, but
to which address points piA.

piA points, as you would expect, to iA.
So what really happens, when the function fA is entered?

Memory is reserved for iA as soon as the block (here, the function) is
entered, but it is not filled until you initialise or assign to iA.
(Compilers may do something different, for example reserve memory for iA
as soon as it is used -- including when its address is taken -- so long as
a valid program can't detect it.)
What is the
right place to read something about this topic.

Decent books on C should address this. What is it you're currently
reading?
 
K

Keith Thompson

Zero said:
Consider a function fA which has a local variable iA (storage class is
auto).
Now, some books tell you,
that a variable does not exist until a value is passed to it, like
iA = 5;

(Terminology: a value can be "passed" to a function. I think you mean
until a value is assigned to it.)

What books say this? An auto variable exists from the point of its
declaration until the end of its scope.
Why is is then possible to write commands like
printf("%d", &iA);
Is this a random value or is it really the location of the variable?

It's possible to write that statement (not command), but it invokes
undefined behavior; "%d" expects an int argument, and you're giving it
a pointer value. However, you can meaningfully write:

printf("%p", (void*)&iA);
Or consider the following code:
int iA;
int piA;

piA = &iA;

As some books say, iA does not exist, because not value is stored,
but to which address points piA.

Any books that say that are wrong. iA exists; it just doesn't have a
meaningful value.

[...]
 
R

Richard Tobin

Zero said:
Or consider the following code:
int iA;
int piA;

(I'm assuming you meant "int *piA;" here.)
piA = &iA;

As some books say, iA does not exist, because not value is stored,
but to which address points piA.

This is not only legal, it's a perfectly reasonable thing to do,
because you can then set the value of iA by indirecting through
piA:

*piA = 6;

So from the point of view of the language, the variable certainly does
exist.

If you're looking for a way in which the assertion could be true, at
the implementation level it's possible that the system may not
allocate any real memory for the variable until it is assigned to.
Many operating systems extend the stack automatically when it is
accessed (in units of pages), so it is conceivable that your program
could fail at the point of assignment because there is insufficient
memory even though it was possible to take the address.

-- Richard
 
Z

Zero

What books say this?  An auto variable exists from the point of its
declaration until the end of its scope.

At the moment, it read "C the complete reference" written by Herbert
Schildt.

In the book, he says, when defining a variable, which means a value is
assigned to it,
storage is reserved for the object.
On the other hand, when declaring an object, only the type of object
and its name
is made public. So no storage is reserved.

Zeh Mau
 
M

Martin Ambuhl

Zero said:
Consider a function fA which has a local variable iA (storage class is
auto).

Unless you are a vampire, there is no reason to use the Transylvanian
Heresy naming convention.
Now, some books tell you,
that a variable does not exist until a value is passed to it, like
iA = 5;

I seriously doubt that any books on C tell you that.
Any auto variable exists at least from the moment that its declaration
in encountered.
An auto variable has no defined value until one is given it, either by
initialization or assignment.
Why is is then possible to write commands like
printf("%d", &iA);
Is this a random value or is it really the location of the variable?

1) The variable exists from its declaration until control leaves the
smallest enclosing block. It has an address; the address has a value.
The contents of that variable will not have a defined value until given
one by assignment or initialization.

2) You are wrong to claim that 'printf("%d", &iA);' is a permissible
statement. The reason is that &iA is an address and %d is the specifier
for a signed int. These are conceptually different and may have very
different representations. You want
printf("%p", (void *)&iA);
%p is the specifier to print a pointer, and it expects a
pointer-to-void, so the cast (void *) is used to make sure that the
address &iA is in an appropriate form.


By the way, calling that statement a 'command' will create an impression
in the minds of real programmers, and that impression will not be one
you want.
Or consider the following code:
int iA;
int piA;

piA = &iA;

As some books say, iA does not exist, because not value is stored,
but to which address points piA.

No book on the C programming language says such a thing.
You need to learn to read more carefully.

So what really happens, when the function fA is entered?
What is the right place to read something about this topic.

Any real book on C,
 
M

Martin Ambuhl

Zero said:
At the moment, it read "C the complete reference" written by Herbert
Schildt.

Herbert Schildt is well-known for his illegal code, his incoreect
understanding of C, his claiming that implementation-specific constructs
are standard, and his refusal to fix his errors. Unfortunately, he has
a very readable style that appeals to those who don't know better.

But I cannot believe that even Herbert Schildt would claim such utter
nonsense. A direct quotation would be nice instead of your paraphrase.
The odds are strong that you have misunderstood rather than Schildt
wrote such a thing. Still, his books are garbage and you should replace
that book with a real book on C as soon as possible.
 
K

Keith Thompson

I wrote the above. Please don't snip attribution lines, like the
Zero said:
At the moment, it read "C the complete reference" written by Herbert
Schildt.

Herbert Schildt writes bad books, but I think in this case you've
misunderstood him.
In the book, he says, when defining a variable, which means a value
is assigned to it, storage is reserved for the object. On the other
hand, when declaring an object, only the type of object and its name
is made public. So no storage is reserved.

An object *definition* causes an object to be created; this includes
reserving storage for it. And object *declaration* merely declares
that an object exists; it might be defined elsewhere. A definition is
also a declaration.

At file scope (outside any function), a line like
int obj = 42;
*defines* the object "obj". A line like:
int obj;
declares it, but may or may not define it.

Inside a function, any object declaration (unless it contains the word
"extern") is a definition, whether it includes an initialization or
not.

It's also important to understand the difference between assignment
and initialization. An initialization is part of a declaration. An
assignment is an expression (typically an expression statement) that
modifies the value of an object. They're distinct constructs that do
very similar things.

The presence of absence of an initialization in a declaration can
*sometimes* affect whether that declaration is a definition or not.
That's probably all Schildt meant to say, though I wouldn't be
surprised if he got it wrong (you haven't quoted his exact words).
But an initialization or assignment itself does not cause the object
to be created; the definition does that.
 
Z

Zero

By the way, calling that statement a 'command' will create an impression
in the minds of real programmers, and that impression will not be one
you want.

Sorry, what is the difference between a command
and a statement?

You are wrong to claim that 'printf("%d", &iA);' is a permissible
statement. The reason is that &iA is an address...

What is wrong to say, that the address - a 32 bit value on my machine
-
shall be printed as decimal value, therfore %d?

Unless you are a vampire, there is no reason to use the Transylvanian
Heresy naming convention.

Why not?
You need to learn to read more carefully.
Ok, that might be a problem. But can you recommend some real good C
books?

Thanks for your help,
Zeh Mau
 
Z

Zero

So what? Addresses are not signed ints, so don't treat them like signed
ints. They are different things. It is quite possible that a float and
an int take the same space on your machine. Do you really feel
compfortable treated them as the same things? Just as a float is not an
int, an address is not a signed int.

Wow, I see, what you mean. I did not consider that things you just
mentionend.
Let me say, I was blind.

Do you have still recommendations for good C books?

Thanks,
Zeh Mau
 
K

Keith Thompson

Please don't snip attribution lines. I've restored this one.
Sorry, what is the difference between a command
and a statement?

A statement is a particular kind of construct in a C program; see any
decent C textbook for an explanation. C doesn't have anything called
a "command"; it typically refers to something provided by an operating
system.
What is wrong to say, that the address - a 32 bit value on my machine
-
shall be printed as decimal value, therfore %d?

printf with a "%d" format requires an argument of type int. Passing
anything not of type int (small quibble: you can use unsigned int in
some circumstances) invokes undefined behavior.

By using "%d" you are promising printf that you've passed an int
argument. printf will rely on that promise, and assume that it can
retrieve a value of type int in some system-specific manner. You're
passing it an argument of type int* (a pointer value), which could be
of a different size, or could be passed via a different mechanism
(stack vs. register, for example). It might *happen* to work on your
system, but it's still wrong.

printf provides a format for printing pointer values, "%p", which
requires an argument of type void*.

I think he's referring to Hungarian notation, which uses prefixes on
variable names to specify the type of the variable (such as your "iA",
where the "i" presumably means that it's of type int). Most
programmers dislike it intensely. It's a matter of taste.
Ok, that might be a problem. But can you recommend some real good C
books?

The comp.lang.c FAQ is at <http://www.c-faq.com/>. See section 18,
"Tools and Resources".

My own recommendations:

Kernighan & Ritchie, "The C Programming Language", 2nd edition,
affectionately known as K&R2. This is *the* book on C, co-authored by
the inventor of the language. It assumes some programming experience.

Harbison & Steele, "C: A Reference Manaul", 5th edition. An excellent
reference work (not a tutorial).

The FAQ mentions K.N. King's "C: A Modern Approach"; I haven't read it
myself.
 
Z

Zero

Hello everybody,

sorry,
the question on books is really in the FAQ,
so it was bullschildt :) to asking that.

Nevertheless, it seems, that I have learnt
today many things.

Thanks all to your great contribution.

Zeh Mau
 
G

Guest

I think he's referring to Hungarian notation, which uses prefixes on
variable names to specify the type of the variable (such as your "iA",
where the "i" presumably means that it's of type int).  Most
programmers dislike it intensely.

"most programmers" or just "many programmers"?

 It's a matter of taste.

or lack of it :)
 
J

James Kuyper

Zero said:
Sorry, what is the difference between a command
and a statement?

A statement is one of the named parts of the C grammar. The only use of
the word "command" in the C standard is in reference to the "command
processor" that system() invokes (if the host environment has one
available).

In conventional usage outside of the C standard, the word "command" has
many uses, the most relevant of which refers to the things you type when
using a command processor to cause a program to be run. In today's
GUI-oriented environments, it's possible that you might never have
actually used a command processor, but most of the systems I know of
have one.
What is wrong to say, that the address - a 32 bit value on my machine
-
shall be printed as decimal value, therfore %d?

Well, if you're willing to rely upon implementation-specific undefined
behavior that happens to do what you want it to do, then the only thing
wrong with it is that it teaches you bad programming habits. However, if
you actually care about whether or not your program is portable, you
must first convert the pointer to void* and print it with %p.

If you convert the pointer to an int and print it with %d, your
program's behavior will not be undefined, but the number you print could
be essentially meaningless.

If you feel that you must print an integer value instead of a pointer
value, but are willing to consider using an integer type other than
'int', then consider the following code:

#include <stdint.h>
....

// 'T' represents an arbitrary object or incomplete type.
T* my_pointer;
...

#ifdef PRIiPTR
fprintf(file, "%" PRIiPTR, (intptr_t)my_pointer)
#endif

That produces a resulting string is guaranteed to be meaningful, at
least in one specific sense: you can scan it back in to an intptr_t
object using

fscanf(file, "%" SCNiPTR, &my_intprt)

with the guarantee that (T*)my_intptr == my_pointer. However, you would
get the same guarantee a little more directly using "%p" for printing
and scanning void* pointers, and it works even on systems where intptr_t
does not exist.
Ok, that might be a problem. But can you recommend some real good C
books?

"The C programming Language, 2nd Edition" - Kernighan and Ritchie.
 
Z

Zero

Please don't snip attribution lines.  I've restored this one.


A statement is a particular kind of construct in a C program; see any
decent C textbook for an explanation.  C doesn't have anything called
a "command"; it typically refers to something provided by an operating
system.

For example,
Kim King says in his book "C programming - A modern approach":

'A statement is a command to be executed when the programs run'.
So nothing wrong with my statement: printf is a command.

The ISO/IEC draft does not mention command,
it says
'A statement specifies an action to be performed. Except as indicated,
statements are
executed in sequence.'

As far as I have realized, in the English speaking world,
normally you speak about statements,
in other language speaking countries a command is an action,
that is why a command is statement. Got it?

Zeh Mau
 
J

James Kuyper

Zero wrote:
....
For example,
Kim King says in his book "C programming - A modern approach":

'A statement is a command to be executed when the programs run'.
So nothing wrong with my statement: printf is a command.

The ISO/IEC draft does not mention command,
it says
'A statement specifies an action to be performed. Except as indicated,
statements are
executed in sequence.'

The final standard agrees with the draft in this regard, and is more
relevant than what Kim King says. If you wish to be understood, you
should use the term 'statement' rather than 'command' when describing
that aspect of C code.
 
J

John Bode

At the moment, it read "C the complete reference" written by Herbert
Schildt.

Not at all surprised.

Herb Schildt may be a great guy, and his books are fairly readable as
reference manuals go, but where the C language is concerned he is a
menace. He invariably explains things incorrectly, offers example
code that's buggy and highly platform-specific, and is probably
responsible for encouraging bad practice among C programmers more than
anyone else.

I had the first edition of that book, and I literally threw it in the
trash after a week. Half the examples wouldn't compile, mostly
because they assumed all C programmers worked on DOS (I was working on
VAX/VMS at the time). Of the ones that would compile, many had
horrible runtime bugs (such as closing a file before attempting to
write to it).
 
M

Martin Ambuhl

For example,
Kim King says in his book "C programming - A modern approach":

'A statement is a command to be executed when the programs run'.
So nothing wrong with my statement: printf is a command.

Please read what I wrote. What is wrong with your statement is the
impression it creates with other programmers. It does you no good at
all to provide examples of use that conform with yours if you have
already left the impression that you are at most a hobbyist BASIC
programmer. There is no reason to gratuitously tell other people not to
take you seriously.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top