A small question about comment style

X

Xiaoshen Li

Hi,

I found every book says that comment in C is: /* comments */(I know
it can comment out a block.)

Nobody mention "//"(comment out a line till its end). It gives me the
impression "//" doesn't work at all. But I tried on my computer. "//"
does work. Is "//" a new feature developed recently or I was purely
lucky to have my program compiled?

Thank you very much for your help.
 
X

Xiaoshen Li

"The C programming language" by Kernighan & Ritchie , 2nd edition, 1988
never mention "//" style of comments. I thought this is the most
important book for C programming lanuage.

I am using gcc compiler. I think gcc knows very well it is compiling a C
program or a C++ program. (C program is with .c extension; C++ program
is with .cpp extension). The reason that gcc accepts "//" comment styple
probably is a new feature of the compiler, not the compiler thought it
is a C++ program.

Xiaoshen
 
M

Marc Boyer

Le 04-11-2005 said:
I found every book says that comment in C is: /* comments */(I know
it can comment out a block.)

Bad or old book: change book.
Nobody mention "//"(comment out a line till its end). It gives me the
impression "//" doesn't work at all. But I tried on my computer. "//"
does work. Is "//" a new feature developed recently or I was purely
lucky to have my program compiled?

"//" Was introduced in C++ before C, and since most popular
C compilers are also C++ compilers, accepting // comments was
a popular extension.

Since C99, // is also a comment in C.

Then, in 2005, a book should mention /* */ and //

Marc Boyer
 
F

Flash Gordon

Marc said:
Bad or old book: change book.

I would not go that far since that would mean replacing my copy of K&R2.
"//" Was introduced in C++ before C, and since most popular
C compilers are also C++ compilers, accepting // comments was
a popular extension.

Since C99, // is also a comment in C.

Then, in 2005, a book should mention /* */ and //

I agree that new books should mention them so that people will know what
they mean on seeing them, however the dearth of conforming C99
implementations (there are some but not many) and the fact that neither
gcc nor MS VC++ are conforming C99 implementations mean that the
portability issue should also be raised IMHO.

Also, it should be noted that // style comments are not very good for
posting to news groups since line wrapping breaks them.
 
P

pete

Marc said:
Bad or old book: change book.


"//" Was introduced in C++ before C, and since most popular
C compilers are also C++ compilers, accepting // comments was
a popular extension.

Since C99, // is also a comment in C.

Then, in 2005, a book should mention /* */ and //

However, my guess as to why it worked, is that he's
using a C89 compiler with some common extensions,
since that is what is popular in 2005 AFAIK.
 
M

Marc Boyer

Le 04-11-2005 said:
I would not go that far since that would mean replacing my copy of K&R2.

I just use my own copy of K&R2 to discuss with people refering to it.


Marc Boyer
 
C

Christopher Benson-Manica

Marc Boyer said:
I just use my own copy of K&R2 to discuss with people refering to it.

If that's all you're using it for, I can't help but think you're
ignoring an excellent language reference.
 
B

Ben Pfaff

Christopher Benson-Manica said:
If that's all you're using it for, I can't help but think you're
ignoring an excellent language reference.

What advantages does K&R2 have over the language standard as a
language reference? The language standard is cheaper, more
accurate, and more portable because it is (legally) obtainable in
electronic form.
 
P

pete

Ben Pfaff wrote:
What advantages does K&R2 have over the language standard as a
language reference?

Table 2-1, page 53.

I think I attempted to extract
all that information from the standard once.
Maybe some day I'll feel up to it again.
 
C

Christopher Benson-Manica

Ben Pfaff said:
What advantages does K&R2 have over the language standard as a
language reference? The language standard is cheaper, more
accurate, and more portable because it is (legally) obtainable in
electronic form.

I would argue that K&R2 wins the portability battle hands down for
people who don't carry laptops around with them. Searchable is of
course a different story.
 
K

Keith Thompson

Xiaoshen Li said:
"The C programming language" by Kernighan & Ritchie , 2nd edition,
1988 never mention "//" style of comments. I thought this is the most
important book for C programming lanuage.

K&R2 is an excellent book, but it only covers the C90 standard.

Strictly speaking, it covers the language defined by the old ANSI
standard, which was officially adopted in 1989, but was very close to
its final form when K&R2 was published in 1988. The 1990 ISO C
standard is the same as the 1989 ANSI C standard except for some
cosmetic changes, including a renumbering of the sections; it was
officially adopted by ANSI, superseding the 1989 ANSI standard. The
1999 ISO C standard supersedes both, but the 1990 standard is still in
wide use, and is still colloquially referred to as "ANSI C".

Many people have wished for a K&R3 covering the C99 standard, but all
indications are that it's not going to happen. The decision is, of
course, entirely up to Brian Kernighan and Dennis Ritchie.

K&R2 is an excellent tutorial and a good reference. H&S5 (Harbison &
Steele, "C: A Reference Manual", 5th Edition) is a very good
reference, especially for those who might be intimidated by the
standard itself; unlike K&R2, it covers both the C90 and C99
standards.
 
M

Marc Boyer

Christopher Benson-Manica said:
If that's all you're using it for, I can't help but think you're
ignoring an excellent language reference.

I was using it still I discovered Harbison &Steele, "C: A
Reference Manual", 5th Edition.

Marc Boyer
 
M

marcelpaulo

Keith said:
K&R2 is an excellent tutorial and a good reference. H&S5 (Harbison &
Steele, "C: A Reference Manual", 5th Edition) is a very good
reference, especially for those who might be intimidated by the
standard itself; unlike K&R2, it covers both the C90 and C99
standards.

Is there another book that best conveys the "feeling", the "style" of C
than K&R ? Wouldn't this "feeling" be more important than the details
of the standards ? (OT) Brian Kernighan have this knack of conveying
the "feeling" of things in his books (see "The Unix Programming
Environment" or "The Practice of Programming").

My ignorance here: where can I find this "intimidating" C standard ?

My U$0.02
Paulo
 
S

Skarmander

marcelpaulo said:
Is there another book that best conveys the "feeling", the "style" of C
than K&R ? Wouldn't this "feeling" be more important than the details
of the standards ?

That wholly depends on what you want to do. For effectively using the
language, the "feeling" is certainly important. It's probably not such a
good idea to "feel" your way around if you're writing portable programs
or maintaining a compiler, on the other hand.

Few people benefit from reading and understanding the standard in full
(you might learn more about reading and writing legalese than C, in fact
:)) but that doesn't mean it's less important. Books that convey the
feeling of C can always be written, and exist in abundance. The standard
is the ultimate authority, of which there logically should only be one.
They serve different purposes.
(OT) Brian Kernighan have this knack of conveying
the "feeling" of things in his books (see "The Unix Programming
Environment" or "The Practice of Programming").
I must say I've never read anything by his hand. No, not even K&R.
My ignorance here: where can I find this "intimidating" C standard ?
That's in the FAQ, which you should read:
http://www.eskimo.com/~scs/C-faq/top.html. Specifically, question 11.2.

S.
 
M

Mark McIntyre

I would argue that K&R2 wins the portability battle hands down for
people who don't carry laptops around with them. Searchable is of
course a different story.

one word: index
 
M

Mark McIntyre

What advantages does K&R2 have over the language standard as a
language reference?

This is a trick question right?

The standard is a scientific / legalistic document, and is written by
committee not all of whom are excellent teachers. Its often dense,
hard for laypeople to understand, and sometimes apparently
contradictory.

K&R is a book whose aim is to teach people how to programme in C.
The language standard is cheaper,

My copy of K&R cost five pounds
more accurate,

Certainly. DRs notwithstanding of course.
and more portable because it is (legally) obtainable in
electronic form.

A computer is more portable than a book? You're having a laugh.

Is it april 1st already? Do Californians celebrate Guy Fawkes in some
unusual manner?
 
A

Andrew Poelstra

Xiaoshen said:
Hi,

I found every book says that comment in C is: /* comments */(I know
it can comment out a block.)

Nobody mention "//"(comment out a line till its end). It gives me the
impression "//" doesn't work at all. But I tried on my computer. "//"
does work. Is "//" a new feature developed recently or I was purely
lucky to have my program compiled?

Thank you very much for your help.

/* */ comments are often referred to as "C-Style comments" whereas //
comments are often referred to as "C++-style comments". Even modern
books may refer to them as such.

As has been stated, // comments are valid in C99, although there are
not in C89. Most compilers do support them because they support C++,
but //s are not standard C89 code.
 
B

Ben Pfaff

Mark McIntyre said:
This is a trick question right?
No.

The standard is a scientific / legalistic document, and is written by
committee not all of whom are excellent teachers. Its often dense,
hard for laypeople to understand, and sometimes apparently
contradictory.

I don't have much trouble using it as a reference. I use it
daily to answer questions about C, so this may partly be because
I use it so much. On the other hand, I don't even know where my
copy of K&R2 is; I suspect it is in my Stanford office, whereas I
am at home in San Mateo now.
K&R is a book whose aim is to teach people how to programme in C.

Nice to know. That doesn't have much bearing on its usability as
reference, though, does it? Reference books and tutorials are
written differently.
My copy of K&R cost five pounds

Around here, K&R2 is more expensive than that.
Certainly. DRs notwithstanding of course.


A computer is more portable than a book? You're having a laugh.

How much coding do you do without a computer? I'm not in the
habit of writing significant amounts of code on paper and then
typing it in later. When I'm at a computer, I can look at my
electronic copy of the standard, either because there's a copy
locally or because I can ssh to my home computer that has a copy.
But I have to carry a copy of K&R2 around with me, if I want to
use it.

Also, I can do full text search on the standard, but not on K&R2.
Is it april 1st already? Do Californians celebrate Guy Fawkes in some
unusual manner?

Californians do not celebrate Guy Fawkes at all. (And I'd rather
not think of myself as a Californian, even if I happen to live
here for the moment. I'm really from Michigan.)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top