Differences between 'const char' and 'char'

H

hokieghal99

What is the difference between these two statements? Are there any major
differences?

const char filename[] = "ips_c.txt";
char filename[] = "ips_c.txt";

Thanks!!!
 
M

Mark A. Odell

What is the difference between these two statements? Are there any major
differences?

const char filename[] = "ips_c.txt";
char filename[] = "ips_c.txt";

Only a compile time difference. If you attempt to write to the first
definition of filename, as in filename[0] = 'I' you will get a
compile-time error. Do the same thing for the second definition and you
will get no error and filename[] will contain "Ips_c.txt".

off-topic: some implementations may place the const version into some sort
of read-only memory.
 
B

Ben Pfaff

hokieghal99 said:
What is the difference between these two statements? Are there any
major differences?

const char filename[] = "ips_c.txt";
char filename[] = "ips_c.txt";

First, these are declarations, not statements[*]. Second, yes,
there is a difference: attempting to modify the former `filename'
yields undefined behavior. The C implementation is allowed to
place the former `filename' in a "read-only" section of memory.

[*] In C99, a declaration can be a statement, but it's still
misleading.
 
B

Ben Pfaff

Mark A. Odell said:
What is the difference between these two statements? Are there any major
differences?

const char filename[] = "ips_c.txt";
char filename[] = "ips_c.txt";

Only a compile time difference. If you attempt to write to the first
definition of filename, as in filename[0] = 'I' you will get a
compile-time error. Do the same thing for the second definition and you
will get no error and filename[] will contain "Ips_c.txt".

It may be only a compile-time difference with your C
implementation, but the standard doesn't say anything about that.
off-topic: some implementations may place the const version into some sort
of read-only memory.

Thus, there may be a runtime difference.
 
M

Mark A. Odell

Only a compile time difference. If you attempt to write to the first
definition of filename, as in filename[0] = 'I' you will get a
compile-time error. Do the same thing for the second definition and you
will get no error and filename[] will contain "Ips_c.txt".

It may be only a compile-time difference with your C
implementation, but the standard doesn't say anything about that.

So the compiler doesn't need to issue an error?
Thus, there may be a runtime difference.

If the module had an error and didn't compile I wouldn't expect a run-time
at all. What am I missing?
 
B

Ben Pfaff

Mark A. Odell said:
Only a compile time difference. If you attempt to write to the first
definition of filename, as in filename[0] = 'I' you will get a
compile-time error. Do the same thing for the second definition and you
will get no error and filename[] will contain "Ips_c.txt".

It may be only a compile-time difference with your C
implementation, but the standard doesn't say anything about that.

So the compiler doesn't need to issue an error?

Some such errors cannot be detected at compile time, and many
such errors are not required to be detected at compile time. In
the particular case you describe, yes, a diagnostic is required.
If the module had an error and didn't compile I wouldn't expect a run-time
at all. What am I missing?

That not every attempted modification of a const-qualified object
can be detected at compile time.
 
M

Micah Cowan

Ben Pfaff said:
[*] In C99, a declaration can be a statement, but it's still
misleading.

Still can't be: only in C++. In C99, they're simply allowed to be
intermixed with statements, but they're still syntactically
distinct.

-Micah
 
B

Ben Pfaff

Micah Cowan said:
Ben Pfaff said:
[*] In C99, a declaration can be a statement, but it's still
misleading.

Still can't be: only in C++. In C99, they're simply allowed to be
intermixed with statements, but they're still syntactically
distinct.

Ah, now that I check the standard, instead of just trying to
recall it, I see that you're right. In particular, I had thought
that a declaration was now one kind of statement, whereas in fact
a block now can contain any mix of declarations and statements.
The two are similar in terms of what parses[*] but, indeed, a
declaration is never a statement.

[*] If a declaration were a statement, then `if(a) int b;' would
parse, but since it isn't, it doesn't. That (and similar) are
the one difference that I see between the two possibilities.
 
B

Ben Pfaff

E. Robert Tisdale said:
You are probably confusing statements with imperatives.

Could you just go away please? C does not have a concept of
"imperatives", and you damn well know it.
 
E

E. Robert Tisdale

Ben said:
C does not have a concept of "imperatives".

It certainly does.

statements:

1. declaration statement and
2. executable statement.

Statements are *terminated* with a semicolon ;
 
B

Ben Pfaff

E. Robert Tisdale said:
It certainly does.

statements:

1. declaration statement and
2. executable statement.

Statements are *terminated* with a semicolon ;

The word "imperative" does not appear in the C standard.
 
N

Noah Roberts

Micah said:
[*] In C99, a declaration can be a statement, but it's still
misleading.


Still can't be: only in C++. In C99, they're simply allowed to be
intermixed with statements, but they're still syntactically
distinct.

Since I am unfamiliar with C99 (only ansi) I take the above to mean that
you don't have to declair all of your variables at the beginning of a
block any longer? Is the following also legal then?

for (int i = 0; i < x; i++) ....;

NR
 
B

Ben Pfaff

Noah Roberts said:
Micah said:
Ben Pfaff said:
[*] In C99, a declaration can be a statement, but it's still
misleading.
Still can't be: only in C++. In C99, they're simply allowed to be
intermixed with statements, but they're still syntactically
distinct.

Since I am unfamiliar with C99 (only ansi)

C99 *is* an ANSI standard.
I take the above to mean that you don't have to declair all of
your variables at the beginning of a block any longer?
Yes.

Is the following also legal then?

for (int i = 0; i < x; i++) ....;

Yes, but as a separate change. The first clause of a `for'
statement isn't part of a block.
 
B

Ben Pfaff

hokiegal99 said:
E. Robert Tisdale said:
Statements are *terminated* with a semicolon ;

This is what I've been taught. That's why I said that these
are statements:

const char filename[] = "ips_c.txt";
char filename[] = "ips_c.txt";

Just because something is terminated with a semicolon does not
make it a statement. Declarations can also be terminated by a
semicolon, as in this case.
Maybe we're being too anal about this. Maybe we all need to get a life.

Precision is important.
 
J

j

E. Robert Tisdale said:
It certainly does.

statements:

1. declaration statement and
2. executable statement.

Statements are *terminated* with a semicolon ;

6.8 Statements and blocks
Syntax
1 statement:
labeled-statement
compound-statement
expression-statement
selection-statement
iteration-statement
jump-statement

I don't see in the above anything referring to a declarator as a statement.
Furthermore, after reading section 6.8 of c99(which I doubt there is much
difference
between what is stated in c99 in this regard and c89/90), claiming that
anything which
ends in a semi-colon qualifies as a statement, doesn't seem to be entirely
correct.
 
M

Micah Cowan

Ben Pfaff said:
Micah Cowan said:
Ben Pfaff said:
[*] In C99, a declaration can be a statement, but it's still
misleading.

Still can't be: only in C++. In C99, they're simply allowed to be
intermixed with statements, but they're still syntactically
distinct.

Ah, now that I check the standard, instead of just trying to
recall it, I see that you're right. In particular, I had thought
that a declaration was now one kind of statement, whereas in fact
a block now can contain any mix of declarations and statements.
The two are similar in terms of what parses[*] but, indeed, a
declaration is never a statement.

In C++, it's a statement, which might have been better in some sense
[*] If a declaration were a statement, then `if(a) int b;' would
parse, but since it isn't, it doesn't. That (and similar) are
the one difference that I see between the two possibilities.

The only case where the difference has actually affected me is
the infortunate fact that labeled statements doesn't include
labeled declarations (which is an impossibility in C). This is
particularly annoying in switch statements, where the following
(made up):

case evt_menu_event_code:
unsigned char low_octet, hi_octet;

low_octet = e->event_data & 0xff;
hi_octet = (e->event_data >> 8) & 0xff;

handle_menu(low_octet, hi_octet);
break;

Seems reasonable, but is still illegal (requiring a surrounding {
and } to legalize it).

-Micah
 
M

Micah Cowan

Ben Pfaff said:
Noah Roberts said:
Micah said:
[*] In C99, a declaration can be a statement, but it's still
misleading.
Still can't be: only in C++. In C99, they're simply allowed to be
intermixed with statements, but they're still syntactically
distinct.

Since I am unfamiliar with C99 (only ansi)

C99 *is* an ANSI standard.
I take the above to mean that you don't have to declair all of
your variables at the beginning of a block any longer?
Yes.

Is the following also legal then?

for (int i = 0; i < x; i++) ....;

Yes, but as a separate change. The first clause of a `for'
statement isn't part of a block.

At this point, it's worth pointing out that certain *other*
similar changes were *not* made; for example:

while (int c = getchar())
...

Is still illegal in C.

-Micah
 
E

E. Robert Tisdale

Ben said:
hokiegal99 said:
E. Robert Tisdale said:
Statements are *terminated* with a semicolon ;

This is what I've been taught. That's why I said that these
are statements:

const char filename[] = "ips_c.txt";
char filename[] = "ips_c.txt";

Just because something is terminated with a semicolon does not
make it a statement. Declarations can also be terminated by a
semicolon, as in this case.
Maybe we're being too anal about this. Maybe we all need to get a life.

Precision is important.

Yes, but the ANSI/ISO standards documents are *not* Holy Scripture --
they are not even a dictionary of the English language.

Your attitude reminds me of an olde tyme preacher
with no formal seminary training
or any education beyond that required to read "The Bible"
but who is "filled with the spirit"
and interprets scripture just a little too literally.

Please just back away from your copy
of the ANSI/ISO C standards documents a little.

A declaration is a statement.
An executable statement is an imperative.
Your attempt to narrow these definitions is anal.
It doesn't contribute anything except confusion.
 
R

Richard Heathfield

hokiegal99 said:
This is what I've been taught.

You were taught incorrectly.
That's why I said that these
are statements:

const char filename[] = "ips_c.txt";
char filename[] = "ips_c.txt";

Maybe we're being too anal about this. Maybe we all need to get a life.

You don't need a life. You need a grammar. There's one in K&R2.
 

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,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top