Linguistics and such

M

Michael

Hi,
I'm currently writing up a report and had a couple of question I want to
clarify:

Is it 'c' or 'C'?

In C++ an instance of a class is an 'Object'. Is an instance of a struct
also an 'Object' in c/C?

Is there a 'const' keyword in Standard c/C? Can you use it to declare array
sizes as I can in C++?

Regards


Mike
 
E

E. Robert Tisdale

Michael said:
I'm currently writing up a report
and had a couple of questions [that] I want to clarify:

Is it 'c' or 'C'?
'C'

In C++, an instance of a class is an 'object'.
Is an instance of a struct also an 'object' in C?

Yes in C++ as well as C
an instance of *any* type is an object.
Is there a 'const' keyword in standard C?
Can you use it to declare array sizes as I can in C++?

Yes.
You can also declare variable array sizes in C.
This feature will almost certainly be adopted by C++ soon.
 
T

Thomas Matthews

Michael said:
Hi,
I'm currently writing up a report and had a couple of question I want to
clarify:

Is it 'c' or 'C'?
It is 'C', captialized, since it refers to the name of something,
in this case a language.

In C++ an instance of a class is an 'Object'. Is an instance of a struct
also an 'Object' in c/C?
The term "object" is pretty vague. My understanding is that
an object is the instance to differentiate it from a stencil.

In C, the struct is the stencil, the object is created from
the stencil. You can have a struct without have any variables
created from the struct (or as the type).

Is there a 'const' keyword in Standard c/C? Yes.

Can you use it to declare array sizes as I can in C++?
The "const" qualifier has nothing to do with the size
of an array, in both languages. In both languages, an
array is of fixed length (quantity), designated at
compile time.

Both languages allow for arrays whose contents are
constant and arrays whose contents are muteable. The
"const" qualifier, when applied to arrays, designates
the contents as constant.
Regards


Mike

Per the properties of Usenet, my comments will
be detailed by those who know more than me. Such
is the beauty of Usenet.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
C

Chris Torek

I'm currently writing up a report and had a couple of question I want to
clarify:

Is it 'c' or 'C'?

The name of the language should be spelled in uppercase.
In C++ an instance of a class is an 'Object'. Is an instance of a struct
also an 'Object' in c/C?

If you use a fly to fish, be sure to zip your fly. If you buy it
on a credit card, do not expect to get a credit. :) I mention
all this because the literal answer to your question is "yes", but
the word "object" means something different in C++ than in C, just
as your fly (on your pants) is not your fly (on the end of your
fishing line).
Is there a 'const' keyword in Standard c/C?
Yes.

Can you use it to declare array sizes as I can in C++?

No and yes. The const keyword in C means something different than
the const keyword in C++. In the 1989 ANSI C standard -- which is
also the 1990 ISO C standard, and is often referred to as either
C89 or C90 -- an array size must be an integer constant expression
(or omitted entirely, in cases like "extern int a[];"). Variables
defined with the "const" keyword are still variables, despite being
read-only (at least in principle), so they may not be used to give
the size of an array.

In the 1999 C standard (both ANSI and ISO), however, there is a new
kind of array, the "variable length array" or VLA, whose size is not
required to be a constant. Since "const" variables are variables,
not constants, they can be used where VLAs are allowed (inside
functions), but not where VLAs are forbidden. Thus:

const int k = 20;

int arr[k]; /* illegal in both C89 and C99 */

extern int somefunc(void);

void f(void) {
int arr2[k]; /* illegal in C89, legal in C99 */
int arr3[somefunc()]; /* likewise legal only in C99 */
...
}

C and C++ are really very different languages, despite syntactic
similarities. While one can write in a contorted "subset language"
so that one's programs are valid in both C (89 and 99) and C++,
and even have the same semantics in both (note that it is not
difficult to write programs that have different semantics), such
programs are rarely "good style" in either language. For instance,
C++ programmers should eschew malloc() in favor of either "new" or
container-classes, and C programmers should avoid casting malloc()
calls (generally required in C++).
 
W

Walter Roberson

I'm currently writing up a report and had a couple of question I want to
clarify:
Is it 'c' or 'C'?

"or" is not an operator in the C89 standard, but both 'c' and 'C' are
valid character constants.

In C++ an instance of a class is an 'Object'. Is an instance of a struct
also an 'Object' in c/C?

C has no such thing as an "instance", and no such thing as an 'Object'.
Memory in C is described in terms of of "object"s, but a C "object"
has fundamental differences from a C++ object.

Is there a 'const' keyword in Standard c/C?
Yes.

Can you use it to declare array
sizes as I can in C++?

Yes. For example this is valid in both C and C++.

int main(void) {
char c[5 * sizeof(const int)];
return 0;
}
 
M

Michael Mair

Walter said:
Michael said:
I'm currently writing up a report and had a couple of question I want to
clarify:
Is it 'c' or 'C'?

"or" is not an operator in the C89 standard, but both 'c' and 'C' are
valid character constants.
In C++ an instance of a class is an 'Object'. Is an instance of a struct
also an 'Object' in c/C?

C has no such thing as an "instance", and no such thing as an 'Object'.
Memory in C is described in terms of of "object"s, but a C "object"
has fundamental differences from a C++ object.
Is there a 'const' keyword in Standard c/C?
Yes.


Can you use it to declare array
sizes as I can in C++?

Yes. For example this is valid in both C and C++.

int main(void) {
char c[5 * sizeof(const int)];
return 0;
}

Is it you or someone else -- or did I miss some irony tags?
This looks like one of these awful c.l.c regular answers some
W. Roberson complained about; soon you will cry "off-topic:
repent, sinner" ;-)


Cheers
Michael
 
K

Keith Thompson

Michael said:
I'm currently writing up a report and had a couple of question I want to
clarify:

Is it 'c' or 'C'?

Is what 'c' or 'C'? :cool:}

The language is usually referred to as C.
In C++ an instance of a class is an 'Object'.

C++ is off-topic here, but it happens that the C++ standard defines
the word "object" very similarly to the way the C standard does, which
has very little to do with "object-oriented" programming. (What
you're thinking of as an "object" is what C++ calls a "class object".)
Is an instance of a struct also an 'Object' in c/C?

I'm not sure why you're capitalizing the word "object".

The C standard defines an "object" as a "region of data storage in the
execution environment, the contents of which can represent values". A
variable of any type is an object. So is a region of memory allocated
by malloc().

So yes, an instance of a struct is an object in C. So is a variable
of type int, or float, or anything else.
Is there a 'const' keyword in Standard c/C? Can you use it to declare array
sizes as I can in C++?

Yes, C has a 'const' keyword. Consult any C textbook to find out
what it means.
 
M

Michael

Thomas Matthews said:
It is 'C', captialized, since it refers to the name of something,
in this case a language.


The term "object" is pretty vague. My understanding is that
an object is the instance to differentiate it from a stencil.

In C, the struct is the stencil, the object is created from
the stencil. You can have a struct without have any variables
created from the struct (or as the type).


The "const" qualifier has nothing to do with the size
of an array, in both languages. In both languages, an
array is of fixed length (quantity), designated at
compile time.

Both languages allow for arrays whose contents are
constant and arrays whose contents are muteable. The
"const" qualifier, when applied to arrays, designates
the contents as constant.


Per the properties of Usenet, my comments will
be detailed by those who know more than me. Such
is the beauty of Usenet.
Also by those less helpful :p
Many thanks for your help

Mike
 
K

Keith Thompson

E. Robert Tisdale said:
Michael wrote: [...]
Is there a 'const' keyword in standard C?
Can you use it to declare array sizes as I can in C++?

Yes.
You can also declare variable array sizes in C.
This feature will almost certainly be adopted by C++ soon.

You can declare variable length arrays in C99. Many compilers don't
yet support C99, and so do not support variable length arrays.

ERT likes to pretend that C99 support is universal. He's wrong.
 
M

Michael

Walter Roberson said:
"or" is not an operator in the C89 standard, but both 'c' and 'C' are
valid character constants.

Nor are 'Is' or 'it' operators but let's be helpful. I came to this
newsgroup for clarification on a subject I was not sure about.
C has no such thing as an "instance", and no such thing as an 'Object'.
Memory in C is described in terms of of "object"s, but a C "object"
has fundamental differences from a C++ object.

What in this case is the technical term for what I am talking about?
Does the standard not talk about the creation of 'objects' (or equilvalent
words). Does it never ever talk about non-primitaive types expect through
memory references?


Is there a 'const' keyword in Standard c/C?
Yes.

Can you use it to declare array
sizes as I can in C++?

Yes. For example this is valid in both C and C++.

int main(void) {
char c[5 * sizeof(const int)];
return 0;
}

Granted I could have been more explicit, but at the same time you're not
being overly helpful!
In c++ I can write:

int i = 5;
const int Ci = 5;

char MyArray1; //Will not compile
char MyArray2[Ci]; // Will compile


is this the same case with C99 C?
 
M

Michael

Chris Torek said:
I'm currently writing up a report and had a couple of question I want to
clarify:

Is it 'c' or 'C'?

The name of the language should be spelled in uppercase.
In C++ an instance of a class is an 'Object'. Is an instance of a struct
also an 'Object' in c/C?

If you use a fly to fish, be sure to zip your fly. If you buy it
on a credit card, do not expect to get a credit. :) I mention
all this because the literal answer to your question is "yes", but
the word "object" means something different in C++ than in C, just
as your fly (on your pants) is not your fly (on the end of your
fishing line).
Is there a 'const' keyword in Standard c/C?
Yes.

Can you use it to declare array sizes as I can in C++?

No and yes. The const keyword in C means something different than
the const keyword in C++. In the 1989 ANSI C standard -- which is
also the 1990 ISO C standard, and is often referred to as either
C89 or C90 -- an array size must be an integer constant expression
(or omitted entirely, in cases like "extern int a[];"). Variables
defined with the "const" keyword are still variables, despite being
read-only (at least in principle), so they may not be used to give
the size of an array.

In the 1999 C standard (both ANSI and ISO), however, there is a new
kind of array, the "variable length array" or VLA, whose size is not
required to be a constant. Since "const" variables are variables,
not constants, they can be used where VLAs are allowed (inside
functions), but not where VLAs are forbidden. Thus:

const int k = 20;

int arr[k]; /* illegal in both C89 and C99 */

extern int somefunc(void);

void f(void) {
int arr2[k]; /* illegal in C89, legal in C99 */
int arr3[somefunc()]; /* likewise legal only in C99 */
...
}
so can i write: (in C99)

void f()
{
for(int i=0;i<5;i++)
{
int a;
}
}

granted it ain't helpful but is it legal?

Mike
C and C++ are really very different languages, despite syntactic
similarities. While one can write in a contorted "subset language"
so that one's programs are valid in both C (89 and 99) and C++,
and even have the same semantics in both (note that it is not
difficult to write programs that have different semantics), such
programs are rarely "good style" in either language. For instance,
C++ programmers should eschew malloc() in favor of either "new" or
container-classes, and C programmers should avoid casting malloc()
calls (generally required in C++).
--
Good use of the word eschew! Massively underused.

In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to
spammers.
 
K

Keith Thompson

Chris Torek said:
If you use a fly to fish, be sure to zip your fly. If you buy it
on a credit card, do not expect to get a credit. :) I mention
all this because the literal answer to your question is "yes", but
the word "object" means something different in C++ than in C, just
as your fly (on your pants) is not your fly (on the end of your
fishing line).

<SEMI-OT>

Actually, the C and C++ standards define the term "object" very
similarly. The wording is different, but I think the semantics are
identical.

C99 3.14:

object
region of data storage in the execution environment, the contents
of which can represent values

C++98 1.8:

The constructs in a C++ program create, destroy, refer to, access,
and manipulate objects. An _object_ is a region of storage.
[_Note:_ A function is not an object, regardless of whether or not
it occupies storage in the way that objects do. ]

(I've used underscores here where the C++ standard uses italics,
indicating that this is the definition of the word "object".)

Although C++ (unlike C) has direct support for what's commonly called
"object-oriented programming", the C++ standard's use of the term
"object" has nothing to do with OOP. What's called an "object" in the
OOP sense is referred to as a "class object" in C++.

</SEMI-OT>
 
K

Keith Thompson

Michael said:
What in this case is the technical term for what I am talking about?
Does the standard not talk about the creation of 'objects' (or equilvalent
words). Does it never ever talk about non-primitaive types expect through
memory references?

Sorry, I can't figure out what you're asking. C has no classes, so of
course it doesn't have a term for an instance of a class. Given the
following:

struct foo {
int x;
int y;
};
struct foo obj1;
int obj2;
double obj3;

obj1, obj2, and obj3 are all "objects" (they're also informally
referred to as "variables", though the C standard uses that word only
in passing). There's no special term for an object of a struct type,
nor, as far as I can tell, is there any need for one. You can say
that obj1 is a struct object, or even that it's a struct (though the
latter is ambiguous since it can refer to either an object or a type,
or even a value).

[...]
Granted I could have been more explicit, but at the same time you're not
being overly helpful!
In c++ I can write:

int i = 5;
const int Ci = 5;

char MyArray1; //Will not compile
char MyArray2[Ci]; // Will compile


is this the same case with C99 C?


In C90, both declarations are illegal. (They remain illegal even if
you drop the "//" comments, which C90 doesn't support).

In C99, both declarations are legal, and they both create VLAs
(variable length arrays).

Unlike C++, the declaration "const int Ci = 5;" does not make Ci a
constant expression; Ci is merely a read-only object. (A compiler is
likely to optimize away references to the object, treating it as if it
were a literal, but it's still not a constant expression as far as the
language is concerned.)

The difference between C90 and C99 in this context is that C99 allows
a non-constant expression to be used as an array size. The semantics
of "const" are unchanged from C90 to C99. You still can't use Ci in
other contexts that still require constant expressions, such as case
labels:

int main(void)
{
const int Ci = 5;
int x = 5;

switch(x) {
case Ci: /* illegal in C90, C99, legal in C++ */
break;
default:
break;
}
return 0;
}

This is legal in C++ because C++ treats "const" differently than C
does, not because of any difference in the definition of the switch
statement.
 
C

Chris Croughton

E. Robert Tisdale said:
Michael wrote: [...]
Is there a 'const' keyword in standard C?
Can you use it to declare array sizes as I can in C++?

Yes.
You can also declare variable array sizes in C.
This feature will almost certainly be adopted by C++ soon.

You can declare variable length arrays in C99. Many compilers don't
yet support C99, and so do not support variable length arrays.

They may not support VLAs, they may do so as an extension even though
they don't support other features of C99 (gcc had support for them well
before it started supporting -std-c99, for instance).

Chris C
 
K

Keith Thompson

Chris Croughton said:
On Thu, 24 Mar 2005 23:39:00 GMT, Keith Thompson


They may not support VLAs, they may do so as an extension even though
they don't support other features of C99 (gcc had support for them well
before it started supporting -std-c99, for instance).

Good point -- though I think gcc's initial implementation of VLAs was
slightly incompatible with the feature as defined by C99. (Or vice
versa, if you prefer.)

Any C compilers that implement VLAs from now on are likely to do so in
the manner specified by the C99 standard, even if their authors don't
choose to implement the full standard.
 
C

Chris Croughton

Good point -- though I think gcc's initial implementation of VLAs was
slightly incompatible with the feature as defined by C99. (Or vice
versa, if you prefer.)

Yes, it was. I don't remember the exact differences, for most purposes
it seemed to be compatible. Something about the zero-length case (one
of them used [] and the other [0], or something like that) as I recall.
Any C compilers that implement VLAs from now on are likely to do so in
the manner specified by the C99 standard, even if their authors don't
choose to implement the full standard.

And other features as well (macro varargs, for instance). And of course
some libraries have some C99 features even when the compilers don't
(extended printf() format specifications and stdint.h are two I've
found commonly); the opposite is also true, compilers which are C99 but
the library hasn't caught up.

Chris C
 
S

Simon Biber

Michael said:
so can i write: (in C99)

void f()
{
for(int i=0;i<5;i++)
{
int a;
}
}


The first time through your loop it would try to make a zero-length array,
which I don't think is allowed.
 
P

Peter Shaggy Haywood

Groovy hepcat E. Robert Tisdale was jivin' on Thu, 24 Mar 2005
12:17:58 -0800 in comp.lang.c.
Re: Linguistics and such's a cool scene! Dig it!
an instance of *any* type is an object.

Not so. An instance of a function type is not an object.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top