C Syntax

S

Stephen Sprunk

C# Learner said:
One of the biggest flaws in C syntax, in my opinion, is the required
parentheses for test conditions.

Without the parens you would need a "then" keyword of some kind. It seems
like an even trade.
Another nicety about Python is the fact that whitespace is used for
defining code blocks. This makes code much clearer than the equivalent
C code, which requires block being/end markers.

OTOH, C not using whitespace to mean anything gives programmers lots of
flexibility in how they format their source code to improve readability.

Block begin/end markers are very useful, and having such things explicit
arguably makes things easier for both the compiler and subsequent coders.
Here's a very simple Python code sample:
http://www.kernelthread.com/hanoi/html/py.html .

Try staring at it for one minute.

After having done so, take a look at this:
http://www.kernelthread.com/hanoi/html/c.html .

Even if one's accustomed to C syntax, the former is still clearer and
easier-to-read, don't you think?

That depends on the reader; the C version is far clearer to me. You also
put error-checking into the C version that's not in the Python one, which is
unfair when considering code length.

S
 
I

I. Appel

opinion.

Well, I have learned BASIC, C-like languages (C and C++), Delphi
(formerly named 'Object Pascal') and Smalltalk.

So, basically, I've learned a couple of procedural languages, and a
couple of OO ones.

And which one do seems you the best?
Which one seems to have the best syntax?

All your languages are Algol-derivative, so they share equivalent
syntactic constructs and all them are similar.

Try to learn something truly alternative. Try FORTRAN 77, the language
without any flame wars about indentation. Try LISP, the language with
syntax based on two symbols. Then come back.

When personally I met C, I already knew BASIC and Pascal and I was amused
by the expressiveness and brevity of C's syntax. I still think that
for (i=0;i<10;i++) {...}
is much better than
for i:=0 to 9 do begin ... end;
I don't understand why I'd need to learn a few more languages to
understand that C's basic syntax could be nicer.

Suggest some improvements and I'll say you, why you're wrong ;-)
 
C

C# Learner

[I'll do the courtesy of a reply at the risk of getting flamed by the
comp.lang.c police.]

I. Appel said:
And which one do seems you the best?
Which one seems to have the best syntax?

I'd say Delphi has the clearest syntax.

[...]
Suggest some improvements and I'll say you, why you're wrong ;-)

Simply:

1) Remove the need to specify parentheses for test conditions --
substitute a test condition terminator, such as Python's ':'.

2) Remove begin/end code block markers and require indentation instead.
 
I

Irrwahn Grausewitz

C# Learner said:
[I'll do the courtesy of a reply at the risk of getting flamed by the
comp.lang.c police.]

[No police around, only conscious people.]
<snip>

Note:
"Procedural" and "OO" are not mutually exclusive language concepts.
Smalltalk is a functional language.
Simply:

1) Remove the need to specify parentheses for test conditions --
substitute a test condition terminator, such as Python's ':'.

2) Remove begin/end code block markers and require indentation instead.

Who or what is keeping you from designing a language incorporating the
worst of BASIC/Pascal/Python/FORTRAN/etc. worlds? Not that it will
ever have a fighting chance to be used by anyone for anything but as
an example of a language with a syntax "uneasy to the eye".

"Doctor, it hurts when I look at C."
"Well, then don't."

Regards
 
G

Grumble

Lew said:
I resemble that remark! :)

You resemble[1] that remark? Are you intangible?

Do you also resent[2] that remark?

[1] to be like or similar to
[2] to feel or express annoyance or ill will at
 
P

Paul Hsieh

Justin Rogers said:
Note that they point out you can leave out parens only when it won't lead to
ambiguity. C doesn't make that mistake. They make sure there is no
ambiguity, and if you leave out a parens it doesn't make the statement
ambiguous, it makes it a different or erroneous statement altogether.

C has plenty of ambiguity:

- Braces are optional if there is only one statement for if() or for() or
while(). (But not for do ... while(), switch, or structure/union
declarations.)

- If a statement has multiple side effects, the order in which those side
effects take place can be unknown.

- The right shift operator may or may not sign extend signed integers. Its
implementation defined.

- The size of an integer is platforms specific.

- The number of bits in a bytes is platform specific.

- Try this one on for size:

char a = -1, b = -2;
unsigned short x = (1 > 0) ? a : b;

printf ("x = %d\n", x);

What do you think is printed out? Explain it.
 
J

Jeremy Yallop

Paul said:
C has plenty of ambiguity:

- Braces are optional if there is only one statement for if() or for() or
while(). (But not for do ... while(), switch, or structure/union
declarations.)

The braces are also optional on switch and do-while statements:

do something(); while (not_finished());

switch (expression)
statement;

Struct or union declarations are not relevant: their components are
declarations, not statements.

None of your other examples were syntactically ambiguous.

Jeremy.
 
G

Grumble

C# Learner said:
Simply:

1) Remove the need to specify parentheses for test conditions --
substitute a test condition terminator, such as Python's ':'.

#define if if (
#define then )

int main(int argc, char **argv)
{
if argc-1 == 0 then return 666;
return 0;
}
 
M

Michael Wojcik

FWIW, if you want a computer language that's really "uneasy on the eye", try
APL or RPG2. Even Lisp isn't very easy to read.

APL?!! APL is beauty itself, manifesting as a swell set of new
keyboard caps. C'mon - just look at that cute little rho, or the
noble edifice of the dotted-quad.

Of course, few languages can produce source as readable as ETA's.[1]
OTOH, I've had to read through pages of Java recently (analysis and design of
a new web component for my corporate masters, requiring review of our current
j2ee web apps), and if Java is the new "readability", then give me C any day.
Worse spaghetti code than you could find in any assembly program. I have no
doubt that C# and C++ are just as bad.

It's possible to write highly readable code in Java, of course, just
as it is in C, but many Java programmers for some reason seem to suffer
from a variety of common style deficiencies. I've seen a lot of Java
source which was apparently written in an editor with a 200-character
line length, for example.

As untold commentators have pointed out untold times, a talented
programmer can produce a mess in any language. There *are* languages
which make it more or less convenient to produce readable source, but
for all but the worst offenders it's possible. (The only real
counterexample I can think of would be an evil esoteric language,
even then perhaps only if it didn't permit comments. I don't think
Unlambda[2] has any provision for comments, for example. If it had
them, even an Unlambda program might be rendered readable, for
suitably small values of "unreadable".)

1. http://www.miketaylor.org.uk/tech/eta/doc/
2. http://www.eleves.ens.fr:8080/home/madore/programs/unlambda/
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Lew said:
I resemble that remark! :)


You resemble[1] that remark? Are you intangible?

Old joke, obviously forgotten.



- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAtd2LagVFX4UWr64RArazAJ0W5D7pR59pWn/nCYBpIbPSRP4YZgCgohYK
Wmdduf6UQMxLBkRVJdhNwnA=
=hfB0
-----END PGP SIGNATURE-----
 
I

I. Appel

I'd say Delphi has the clearest syntax.

Delphi is boring. IMO syntax of C is much more funny.
[...]
Suggest some improvements and I'll say you, why you're wrong ;-)

Simply:

Well, rephrasing Paul Graham, I should say "Don't wait until C will become
as
clear as Python, use Python instead".
1) Remove the need to specify parentheses for test conditions --
substitute a test condition terminator, such as Python's ':'.

Actually in C it is permitted to build much more complicated conditions.
It's generally bad programming style, but that feature requires parenteses.
2) Remove begin/end code block markers and require indentation instead.

Block markers vs. indentation is IMO eternal flame war.
 
M

Mark McIntyre

Aha, but I only saw Python code for the /first/ time yesterday. :)

*shrug*
Me too. And C is much easier to read. YMMV.
Hmm... I have to disagree. I feel that this discussion is about C's
basic syntax and its ubiquity in popular modern-day languages.

But the topic of CLC is the C /language/ and using it, not whether there's
a neater way to structure the language. You could try talking about that
over in comp.std.c, but I warn you, you'll get roasted in about 2 seconds.
 
M

Mark McIntyre

One of the biggest flaws in C syntax, in my opinion, is the required
parentheses for test conditions.
if (FooBar(Parse(Process(GetInput())))
DoSomething();

(he prefers)
if FooBar(Parse(Process(GetInput())):
DoSomething();

Okay, I'll bite. Why on earth do you consider this in any way an
improvement? What difference does it make to anything?
 
M

Mark McIntyre

Aha, but I only saw Python code for the /first/ time yesterday. :)

*shrug*
Me too. And C is much easier to read. YMMV.
Hmm... I have to disagree. I feel that this discussion is about C's
basic syntax and its ubiquity in popular modern-day languages.

But the topic of CLC is the C /language/ and using it, not whether there's
a neater way to structure the language. You could try talking about that
over in comp.std.c, but I warn you, you'll get roasted in about 2 seconds.
 
A

Alan Balmer

Also, I guess I'm just trying to make a point and get heard here; and,
at the same time, I want to hear what those who are more experienced
than me have to say on the matter -- especially C gurus!

For the record, I don't /hate/ this C syntax of which I talk. I just
have a feeling of disbelief, perhaps, that some badly-implemented (in my
opinion) syntactial elements of the C language have passed through to
modern programming languages.

Forgive me if I'm wrong, but my impression is that you don't have a
deep enough understanding of the C language, and indeed of programming
languages in general, to appreciate the reasons for the syntax
features you're commenting on. Take your original example of
eliminating the deli meters around the conditional in an if statement.
Think about what this would mean for compound conditions. Combine that
with the Python-style blocking and then think about an if statement
which tests for several conditions, requiring more than one line to
write.

As you study and understand the language, you'll find that it's all
nicely consistent, and that there are good reasons for most of the
features which seem odd to you. One of the characteristics of C is
terseness, and extra parens aren't required by the language for no
reason.

Also, think about the fact that language inventors and implementers
are, by and large, a pretty bright bunch. In general, they probably
have more and wider experience in the field than you do, and some of
them might even be as smart ;-) Those features which have passed
through to modern languages have done so for a reason.
 

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
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top