C Syntax

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
 
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/
 
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.
 
F

Francois Beaussier

:
- 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.

the same as in c# :

unchecked
{
char c = (char) -1;
ushort x = c;
Console.WriteLine(">> " + x);
}

wich is the way -1 is represented in 16 bits unsigned data ;)

what is your point ? what else where we supposed to expect ? 255 ?
 
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.
 
G

Guillaume

Delphi is boring. IMO syntax of C is much more funny.

Delphi is Pascal with maybe a few bells and whistles.
Therefore, it's based on an older language than C, maybe that's why
it's a bit boring.

Well, rephrasing Paul Graham, I should say "Don't wait until C will become
as
clear as Python, use Python instead".

If you really think Python has clearer syntax than C, well, think twice.
One terrible fact is this indentation thing in Python to delimit code
blocks. One tab or space is left behind, and the whole thing is screwed.
To me, it's an unacceptable syntax issue.
 
D

Dan Pop

In said:
What you call a nicety <sic>, I call a liability.

None of the programming languages assigning semantics to indentation
has ever become mainstream. There must be a reason...

Dan
 
I

Irrwahn Grausewitz

Michael Voss said:
Irrwahn Grausewitz wrote:
[...snip...]
Smalltalk is a functional language.

How do you define "functional language" ?

I don't - others already did. If you're interested in this subject
I suggest to Go Ogle for the comp.lang.functional FAQ - it contains
a nice explanation of functional vs. procedural programming paradigms.

Ob the portion you snipped: While both C++ and Smalltalk are OO
languages, the former is a mere procedural, the latter a mere
functional language.

Regards
 
C

C# Learner

Alan said:
[...]

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.

I believe that I appreciate the reasons for these features. What I'm
saying is that I think that there are better (in terms of allowing the
user of the language to write readable code) alternatives that could
have been implemented instead.
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.

a) Valid C syntax:

if (foo &&
bar) {
foobar();
}

b) Similar code to the above but using my suggested syntax changes:

if foo &&
bar:
foobar();

Why wouldn't (b) be feasible here?

Everything from 'if' to ':' is considered the condition. After the
newline after ':', whitespace is required to form a code block.
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.

The only way in which they seem odd to me is that they make code much
less readable than it could be, in my opinion.

Okay, if you don't agree with the 'if'..':' idea, then how about
changing the parentheses required for test conditions for a different
pair of characters? An ideal pair would be a pair that isn't used
elsewhere in the language, for readability's sake.
One of the characteristics of C is
terseness, and extra parens aren't required by the language for no
reason.

My point is that a different construct could be substituted in each case.
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 ;-)

So those who invented C's syntax are necessarily brighter than those who
invented, say, Python's syntax?
Those features which have passed
through to modern languages have done so for a reason.

I honestly wonder what that reason is.

Regards
 

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

Top