473,395 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

indentation

I have had several complaints by some people who wish to help me and I
wish to get the problem straight. I wrote this small utility myself and
added some indentation and I wonder if it is acceptable. It does make source
easier to read.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc!=3) {
fprintf(stderr,"usage error\n");
return -1;
}
double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
printf("%.2f\n",y/x);
return 0;
}

Is this a good example of a properly indended program?

Bill
Jun 27 '08 #1
43 1909
Bill Cunningham wrote:
I have had several complaints by some people who wish to help me and I
wish to get the problem straight. I wrote this small utility myself and
added some indentation and I wonder if it is acceptable. It does make source
easier to read.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc!=3) {
fprintf(stderr,"usage error\n");
return -1;
}
double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
printf("%.2f\n",y/x);
return 0;
}

Is this a good example of a properly indended program?

Bill

Find the GNU 'indent' utility. It gives us..

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 3) {
fprintf(stderr, "usage error\n");
return -1;
}
double x, y;
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
printf("%.2f\n", y / x);
return 0;
}

...given your program as input. What do you think?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 27 '08 #2
Bill Cunningham wrote:
>
I have had several complaints by some people who wish to help me
and I wish to get the problem straight. I wrote this small utility
myself and added some indentation and I wonder if it is acceptable.
It does make source easier to read.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc!=3) {
fprintf(stderr,"usage error\n");
return -1;
}
double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
printf("%.2f\n",y/x);
return 0;
}

Is this a good example of a properly indended program?
Close. The main problems are too little indentation (I suggest 3
spaces) and the lack of spaces in the source. Also the variable
declaration should not occur after executable code. Compare the
following:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
double x,y;

if (argc != 3) {
fprintf(stderr, "usage error\n");
return -1;
}
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
printf("%.2f\n", y/x);
return 0;
} /* main */

I like to mark the closing brace of a function with the function
name.

There is a program around called indent (GNU version 2.2.9 here)
which does all this for you. It is configurable. I use the
following configuration for it (really just one long line in
indent.pro):

-kr -l66 -i3 -bad -di16 -lc66 -nce -ncs -cbi0 -bbo -pmt -psl -ts1
-cdw -ppi 3

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #3

"Joe Wright" <jo********@comcast.netwrote in message
news:xd******************************@comcast.com. ..
..given your program as input. What do you think?
The first copy of the file I wrote has had its indentation changed by
the news server or client. It was indented when I posted it and the
indentation was removed. Maybe nntp does that. The progran was originally
indented like this.

if (argc!3) {
fprintf(stderr,"usage error\n");
return -1;
}

Thatis what was copied onto the post and it was posted without the
indentation.

Bill
Jun 27 '08 #4
Bill Cunningham wrote:
"Joe Wright" <jo********@comcast.netwrote in message
news:xd******************************@comcast.com. ..
>..given your program as input. What do you think?

The first copy of the file I wrote has had its indentation changed by
the news server or client. It was indented when I posted it and the
indentation was removed. Maybe nntp does that. The progran was originally
indented like this.
You probably used tabs rather than spaces. Stick to (two) spaces and
your code will be left unmolested.

--
Ian Collins.
Jun 27 '08 #5
Keith Thompson wrote:
"Bill Cunningham" <no****@nspam.comwrites:
> I have had several complaints by some people who wish to help me and I
wish to get the problem straight. I wrote this small utility myself and
added some indentation and I wonder if it is acceptable. It does make source
easier to read.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc!=3) {
fprintf(stderr,"usage error\n");
return -1;
}
double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
printf("%.2f\n",y/x);
return 0;
}

Is this a good example of a properly indended program?

Better, but not yet good.

A single space per indentation level is IMHO insufficient.
It's still very hard to see what's indented. I like 4-column
indentation myself; I consider 2 to be the bare minimum. (Some
advocate 8-column indentation, which certainly encourages Any
decent text editor should do most of the work for you automatically
(":se ai" or ":set autoindent" in vi, for example).

There are several common styles for indentation and brace placement.
Your program exhibits none of them. That's not necessarily A Bad
Thing, but it's a bit jarring, and there's rarely any good reason
not to follow one of the existing styles.

Two of the most common styles are:

1. K&R style. An opening brace '{' goes at the end of a line,
preceded by a space (except possibly for the opening brace of a
function definition, for historical reasons). Enclosed lines are
indented by one level (e.g., by 4 columns). The closing brace is
aligned directly under the beginning of the line containing the
opening brace (this is where you go astray).

2. Opening and closing braces appear on lines by themselves.
Enclosed lines are indented by one level.

Rarer, but still valid, styles are:

3. Like 2, but the braces are aligned with the enclosed lines.

4. Like 2, but the braces are half-indented (e.g., by 2 columns).
I think this is the GNU-recommended style.
That is the GNU style and is the default provided by indent. It is
fairly common, especially by open source enthusiasts, though I cannot
think of a more hideous style, and I've seen my share of truly awful C code.
Jun 27 '08 #6

"Ian Collins" <ia******@hotmail.comwrote in message
news:68*************@mid.individual.net...
You probably used tabs rather than spaces. Stick to (two) spaces and
your code will be left unmolested.
Exactly. I always do. Maybe it's a bad habit.

Bill
Jun 27 '08 #7

"Bill Cunningham" <no****@nspam.comwrote in message
news:%7sUj.3801$Ve.393@trnddc08...
I have had several complaints by some people who wish to help me and I
wish to get the problem straight. I wrote this small utility myself and
added some indentation and I wonder if it is acceptable. It does make
source easier to read.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc!=3) {
fprintf(stderr,"usage error\n");
return -1;
}
double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
printf("%.2f\n",y/x);
return 0;
}

Is this a good example of a properly indended program?
well, this gets into this big hairy area known as "coding style" (most have
opinions, few can completely agree...).
now, I usually code in notepad, which has inflexible 8-space tabs, so
usually I use this.
if the tab space is adjustable, usually I like 4 space tabs.

2 or 3 spaces is IMO too little.
1 space is just horrid (may as well not indent at all...).
usually, I put opening and closing braces on their own lines, and closing
braces are indended the same as the opening braces.

int main(int argc, char *argv[])
{
FILE *fd;
if(argv<2)
{
printf("usage: %s <filename>\n", argv[0]);
return(-1);
}

fd=fopen(argv[1], "rb");
...
return(0);
}

note that EXIT_SUCCESS and EXIT_FAILURE are considered "more correct" for
main return values, but 0 and -1 are more common/traditional.
IMO, both forms:
if(...)
{

and:
if(...) {

are fairly common and acceptable, but most people put the brace on its own
line for functions, and rarely for structs or unions.

it is common for commas to be followed by a space ("f(x, y);" but not
"f(x,y);").
some people precede/follow parens and/or operators with spaces.

if certain single-letter variable names are used (especially, i,j,k,s,t,...)
it is almost obligatory that they be certain types (i,j,k are int, s,t are
'char *', ...).

return is often/usually written as if it were a function call (common in C,
rare in C++).

and so on...

Bill


Jun 27 '08 #8
cr88192 said:

<snip>
>
now, I usually code in notepad,
Oh dear. :-)
which has inflexible 8-space tabs, so
usually I use this.
if the tab space is adjustable, usually I like 4 space tabs.
Tab/space wars are so 1990s, though, aren't they?
2 or 3 spaces is IMO too little.
And IMO 3 is too many. Vive la difference!
1 space is just horrid (may as well not indent at all...).
Agreed.
usually, I put opening and closing braces on their own lines, and closing
braces are indended the same as the opening braces.
Agreed again.

You forgot <stdio.h>
int main(int argc, char *argv[])
{
FILE *fd;
if(argv<2)
You meant argc.
{
printf("usage: %s <filename>\n", argv[0]);
If argc is 0, the behaviour is undefined. If it is >= 1, argv[0] must
represent the program name in some way, but need not be a string
representing the invocation name for the program. It could even be a pid!
return(-1);
This has no portable meaning (and the parentheses are redundantly
superfluous).
}

fd=fopen(argv[1], "rb");
...
return(0);
Again, the parentheses are superfluously redundant.
}

note that EXIT_SUCCESS and EXIT_FAILURE are considered "more correct" for
main return values,
0 is fine - it means success.
but 0 and -1 are more common/traditional.
A -1 return value has no de jure meaning in C (which is, at least, in
keeping with the better kinds of tradition - if we knew why we did them,
they wouldn't be traditions!).

To indicate failure portably, use EXIT_FAILURE.
IMO, both forms:
if(...)
{

and:
if(...) {

are fairly common and acceptable, but most people put the brace on its
own line for functions, and rarely for structs or unions.
The word "most" is arguable. K&R's style is perniciously persistent even
now. And a significant number of Allman adherents /do/ put a struct brace
on its own line.
it is common for commas to be followed by a space ("f(x, y);" but not
"f(x,y);").
True, and wise.
some people precede/follow parens and/or operators with spaces.
True, and a matter of taste, I think. My own taste is for parentheses not
to "command" any whitespace, but for binary operators to be separated from
their operands by a space.
if certain single-letter variable names are used (especially,
i,j,k,s,t,...) it is almost obligatory that they be certain types (i,j,k
are int, s,t are 'char *', ...).
No, not really. Common, yes. Obligatory? Hardly.
>
return is often/usually written as if it were a function call (common in
C, rare in C++).
return /isn't/ a function call, and it seems to me from perusing this group
and from what I've seen of good C code (in well-regarded literature, in
workplaces, and on the Web) that few if any experienced C programmers
treat it like one.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #9
Bill Cunningham wrote:
"Joe Wright" <jo********@comcast.netwrote in message
news:xd******************************@comcast.com. ..
>..given your program as input. What do you think?

The first copy of the file I wrote has had its indentation changed by
the news server or client. It was indented when I posted it and the
indentation was removed. Maybe nntp does that. The progran was originally
indented like this.

if (argc!3) {
fprintf(stderr,"usage error\n");
return -1;
}
This is one of many possible styles. The main thing is to use
intentation to identify each block, and to use a consistent style.

The two commonest are probable

if (x)
{
bodyhere();
}

and
if (x) {
bodyhere();
}

The one you used above is less common in my experience.
Jun 27 '08 #10
On 8 May, 01:51, "Bill Cunningham" <nos...@nspam.comwrote:
* * I have had several complaints by some people who wish to help me
and I wish to get the problem straight. I wrote this small utility
myself and added some indentation and I wonder if it is acceptable.
It does make source easier to read.
that's the idea! :-)
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
*if (argc!=3) {
* fprintf(stderr,"usage error\n");
* return -1;
* }
*double x,y;
*x=strtod(argv[1],NULL);
*y=strtod(argv[2],NULL);
*printf("%.2f\n",y/x);
*return 0;
*}

* * Is this a good example of a properly indended program?

well it's better! In fact it's good enough to prevent
me whining about indentation. Personnally I prefer the
braces to line up, but the above is ok. I definitly
prefer whitespace around binary operators (eg. =)
and I'd use more blank lines. But on usenet saving vertical
space can be a good thing.

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
double x;
double y;

if (argc != 3)
{
fprintf (stderr,"usage error\n");
return -1;
}

x = strtod (argv [1], NULL);
y = strtod (argv [2], NULL);

printf ("%.2f\n", y / x);

return 0;
}

the main thing is to group things together that
logically belong together, leave enough whitespace
(horizontal and vertical) to aid readability and
above all, be consistent.

Oh, and never use tabs!

[mutter, one project had a standard of 4 column indent
and 8 space tabs. So you mixed tabs and spaces. eek!]
--
Nick Keighley

"The Real Programmer wants a "you asked for it, you got it"
text editor--complicated, cryptic, powerful, unforgiving,
dangerous. TECO, to be precise."
Jun 27 '08 #11
i use tabs for eg.

void func1(void)
{
...

Jun 27 '08 #12
pereges wrote:
i use tabs for eg.

void func1(void)
{
...
My tab key is configured to give me four space characters
in my source code editor.
Tab characters don't post very well.

--
pete
Jun 27 '08 #13


Richard Heathfield ha scritto:
cr88192 said:

<snip>
>now, I usually code in notepad,

Oh dear. :-)
Oh dear dear :-)
>which has inflexible 8-space tabs, so
usually I use this.
if the tab space is adjustable, usually I like 4 space tabs.
And IMO 3 is too many. Vive la difference!

You forgot <stdio.h>
You meant argc.
A type , may be...
If argc is 0, the behaviour is undefined. If it is >= 1, argv[0] must
represent the program name in some way, but need not be a string
representing the invocation name for the program. It could even be a pid!
argc must be 1 (the program name ia always passed as argument), by
standard.
But, yes, it is best to check it...
> return(-1);

This has no portable meaning (and the parentheses are redundantly
superfluous).
I'm not agree, it' more readable.
>>
fd=fopen(argv[1], "rb");
...
return(0);

Again, the parentheses are superfluously redundant.
Question of style: for me it is better to place parentesis around the
returned value.
What about an horrible return like this one ????
return a*32 / b + c - d * e / oh_my_good ;
0 is fine - it means success.

Ok, it's a standard value
A -1 return value has no de jure meaning in C (which is, at least, in
keeping with the better kinds of tradition - if we knew why we did them,
they wouldn't be traditions!).

To indicate failure portably, use EXIT_FAILURE.
:-)

brix

Jun 27 '08 #14
brix99luftballons wrote:
argc must be 1 (the program name ia always passed as argument), by
standard.
No. argc may be zero.

N869
5.1.2.2.1 Program startup

[#2] If they are declared, the parameters to the main
function shall obey the following constraints:

-- The value of argc shall be nonnegative.

-- argv[argc] shall be a null pointer.

-- If the value of argc is greater than zero, the array
members argv[0] through argv[argc-1] inclusive shall
contain pointers to strings, which are given
implementation-defined values by the host environment
prior to program startup. The intent is to supply to
the program information determined prior to program
startup from elsewhere in the hosted environment. If
the host environment is not capable of supplying
strings with letters in both uppercase and lowercase,
the implementation shall ensure that the strings are
received in lowercase.

-- If the value of argc is greater than zero, the string
pointed to by argv[0] represents the program name;
argv[0][0] shall be the null character if the program
name is not available from the host environment. If
the value of argc is greater than one, the strings
pointed to by argv[1] through argv[argc-1] represent
the program parameters.

--
pete
Jun 27 '08 #15
brix99luftballons said:
>

Richard Heathfield ha scritto:
<snip>
>If argc is 0, the behaviour is undefined. If it is >= 1, argv[0] must
represent the program name in some way, but need not be a string
representing the invocation name for the program. It could even be a
pid!
argc must be 1 (the program name ia always passed as argument), by
standard.
Wrong.
But, yes, it is best to check it...
Right, so let's do that. See 2.1.2.2 of C89 or 5.1.2.2.1 of C99:

* The value of argc shall be nonnegative.

* argv[argc] shall be a null pointer.

* If the value of argc is greater than zero, the array members
argv[0] through argv[argc-1] inclusive shall contain pointers to
strings, which are given implementation-defined values by the host
environment prior to program startup. The intent is to supply to the
program information determined prior to program startup from elsewhere
in the hosted environment. [...]

It is clear from the above that argc may be 0.
>> return(-1);

This has no portable meaning (and the parentheses are redundantly
superfluous).
I'm not agree, it' more readable.
Please explain why parentheses make the return code more readable. Does
this apply to every expression, or just to the expression that
(optionally) follows a return statement? If so, why is return special?
>> fd=fopen(argv[1], "rb");
...
return(0);

Again, the parentheses are superfluously redundant.
Question of style: for me it is better to place parentesis around the
returned value.
What about an horrible return like this one ????
return a*32 / b + c - d * e / oh_my_good ;
What makes you think that adding parentheses helps? If the reader can't
comprehend a*32 / b + c - d * e / oh_my_good, why are they more likely to
comprehend (a*32 / b + c - d * e / oh_my_good)?

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #16
rio

"Joe Wright" <jo********@comcast.netha scritto nel messaggio
news:xd******************************@comcast.com. ..
Bill Cunningham wrote:
> I have had several complaints by some people who wish to help me and
I wish to get the problem straight. I wrote this small utility myself and
added some indentation and I wonder if it is acceptable. It does make
source easier to read.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc!=3) {
fprintf(stderr,"usage error\n");
return -1;
}
double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
printf("%.2f\n",y/x);
return 0;
}

Is this a good example of a properly indended program?
>Bill
i like below

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{double x, y;

if (argc!=3) { fprintf(stderr,"usage error\n"); return -1;}

x=strtod(argv[1], 0);
y=strtod(argv[2], 0);

return (x!=0.0) ?
(printf("%.2f\n", y/x), 0): (printf("Error: x==0\n"), -1) ;
}


Jun 27 '08 #17

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:HM******************************@bt.com...
cr88192 said:

<snip>
>>
now, I usually code in notepad,

Oh dear. :-)
all in all it is a good and simple editor...

>which has inflexible 8-space tabs, so
usually I use this.
if the tab space is adjustable, usually I like 4 space tabs.

Tab/space wars are so 1990s, though, aren't they?
yeah...
anymore, it is mostly forgotten, but I still usually use 4 or 8...

>2 or 3 spaces is IMO too little.

And IMO 3 is too many. Vive la difference!
yeah, it is a matter of taste I guess. 2 or 3 spaces IMO is painful to read
or skim...
>1 space is just horrid (may as well not indent at all...).

Agreed.
>usually, I put opening and closing braces on their own lines, and closing
braces are indended the same as the opening braces.

Agreed again.

You forgot <stdio.h>
yeah, I inferred this, since my point was more about demonstrating style
than actually working code.
>int main(int argc, char *argv[])
{
FILE *fd;
if(argv<2)

You meant argc.
yes, typing does not always work perfectly...

> {
printf("usage: %s <filename>\n", argv[0]);

If argc is 0, the behaviour is undefined. If it is >= 1, argv[0] must
represent the program name in some way, but need not be a string
representing the invocation name for the program. It could even be a pid!
this is a common practice though, and also I don't personally know of any OS
where argc is not at least 1...

> return(-1);

This has no portable meaning (and the parentheses are redundantly
superfluous).
return(-1);
is the same as:
return -1;

only, the parens are a matter of style and tradition...
> }

fd=fopen(argv[1], "rb");
...
return(0);

Again, the parentheses are superfluously redundant.
>}

note that EXIT_SUCCESS and EXIT_FAILURE are considered "more correct" for
main return values,

0 is fine - it means success.
yeah.
>but 0 and -1 are more common/traditional.

A -1 return value has no de jure meaning in C (which is, at least, in
keeping with the better kinds of tradition - if we knew why we did them,
they wouldn't be traditions!).

To indicate failure portably, use EXIT_FAILURE.
it is more correct, but -1 is a very common value for indicating error as
well.
I think usually any value other than 0 counts as an error.

>IMO, both forms:
if(...)
{

and:
if(...) {

are fairly common and acceptable, but most people put the brace on its
own line for functions, and rarely for structs or unions.

The word "most" is arguable. K&R's style is perniciously persistent even
now. And a significant number of Allman adherents /do/ put a struct brace
on its own line.
I comment based on what I have most often seen, but these conventions are by
no means universal.

>it is common for commas to be followed by a space ("f(x, y);" but not
"f(x,y);").

True, and wise.
>some people precede/follow parens and/or operators with spaces.

True, and a matter of taste, I think. My own taste is for parentheses not
to "command" any whitespace, but for binary operators to be separated from
their operands by a space.
yeah, my case I don't usually use a space either (since to me the paren is
'strong', and putting a space there makes it seem 'weak'...).

my case, whether or not I use spaces around operators is a matter of
situation, where usually they are used for breaking up expressions into
recognizable parts, and sometimes for aligning groups of regularized
expressions.

sometimes terms for polynomial expressions can be broken up like this as
well.

4*a*c - 2*b

>if certain single-letter variable names are used (especially,
i,j,k,s,t,...) it is almost obligatory that they be certain types (i,j,k
are int, s,t are 'char *', ...).

No, not really. Common, yes. Obligatory? Hardly.
sufficiently common patterns are almost obligatory.
if one is going to use different types, they are better off avoiding these
names, for sake of reducing possible confusion.

>>
return is often/usually written as if it were a function call (common in
C, rare in C++).

return /isn't/ a function call, and it seems to me from perusing this
group
and from what I've seen of good C code (in well-regarded literature, in
workplaces, and on the Web) that few if any experienced C programmers
treat it like one.
well, as noted, it is a lot more common in C IME, but it is almost never
done in C++.

however, I have personally seen a lot more code with the parens than without
the parens...

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Jun 27 '08 #18
"Bill Cunningham" <no****@nspam.comwrites:
I have had several complaints by some people who wish to help me and I
wish to get the problem straight. I wrote this small utility myself and
added some indentation and I wonder if it is acceptable. It does make source
easier to read.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc!=3) {
fprintf(stderr,"usage error\n");
return -1;
}
double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
printf("%.2f\n",y/x);
return 0;
}

Is this a good example of a properly indended program?

Bill
You are a very good troll I think! :-)
Jun 27 '08 #19
"cr88192" <cr*****@NOSPAM.hotmail.comwrites:
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:HM******************************@bt.com...
>cr88192 said:

<snip>
>>>
now, I usually code in notepad,

Oh dear. :-)

all in all it is a good and simple editor...

>>which has inflexible 8-space tabs, so
usually I use this.
if the tab space is adjustable, usually I like 4 space tabs.

Tab/space wars are so 1990s, though, aren't they?

yeah...
anymore, it is mostly forgotten, but I still usually use 4 or 8...

>>2 or 3 spaces is IMO too little.

And IMO 3 is too many. Vive la difference!

yeah, it is a matter of taste I guess. 2 or 3 spaces IMO is painful to read
or skim...
>>1 space is just horrid (may as well not indent at all...).

Agreed.
>>usually, I put opening and closing braces on their own lines, and closing
braces are indended the same as the opening braces.

Agreed again.

You forgot <stdio.h>

yeah, I inferred this, since my point was more about demonstrating style
than actually working code.
>>int main(int argc, char *argv[])
{
FILE *fd;
if(argv<2)

You meant argc.

yes, typing does not always work perfectly...

>> {
printf("usage: %s <filename>\n", argv[0]);

If argc is 0, the behaviour is undefined. If it is >= 1, argv[0] must
represent the program name in some way, but need not be a string
representing the invocation name for the program. It could even be a pid!

this is a common practice though, and also I don't personally know of any OS
where argc is not at least 1...

>> return(-1);

This has no portable meaning (and the parentheses are redundantly
superfluous).

return(-1);
is the same as:
return -1;

only, the parens are a matter of style and tradition...
>> }

fd=fopen(argv[1], "rb");
...
return(0);

Again, the parentheses are superfluously redundant.
>>}

note that EXIT_SUCCESS and EXIT_FAILURE are considered "more correct" for
main return values,

0 is fine - it means success.

yeah.
>>but 0 and -1 are more common/traditional.

A -1 return value has no de jure meaning in C (which is, at least, in
keeping with the better kinds of tradition - if we knew why we did them,
they wouldn't be traditions!).

To indicate failure portably, use EXIT_FAILURE.

it is more correct, but -1 is a very common value for indicating error as
well.
I think usually any value other than 0 counts as an error.

>>IMO, both forms:
if(...)
{

and:
if(...) {

are fairly common and acceptable, but most people put the brace on its
own line for functions, and rarely for structs or unions.

The word "most" is arguable. K&R's style is perniciously persistent even
now. And a significant number of Allman adherents /do/ put a struct brace
on its own line.

I comment based on what I have most often seen, but these conventions are by
no means universal.

>>it is common for commas to be followed by a space ("f(x, y);" but not
"f(x,y);").

True, and wise.
>>some people precede/follow parens and/or operators with spaces.

True, and a matter of taste, I think. My own taste is for parentheses not
to "command" any whitespace, but for binary operators to be separated from
their operands by a space.

yeah, my case I don't usually use a space either (since to me the paren is
'strong', and putting a space there makes it seem 'weak'...).

my case, whether or not I use spaces around operators is a matter of
situation, where usually they are used for breaking up expressions into
recognizable parts, and sometimes for aligning groups of regularized
expressions.

sometimes terms for polynomial expressions can be broken up like this as
well.

4*a*c - 2*b

>>if certain single-letter variable names are used (especially,
i,j,k,s,t,...) it is almost obligatory that they be certain types (i,j,k
are int, s,t are 'char *', ...).

No, not really. Common, yes. Obligatory? Hardly.

sufficiently common patterns are almost obligatory.
if one is going to use different types, they are better off avoiding these
names, for sake of reducing possible confusion.

>>>
return is often/usually written as if it were a function call (common in
C, rare in C++).

return /isn't/ a function call, and it seems to me from perusing this
group
and from what I've seen of good C code (in well-regarded literature, in
workplaces, and on the Web) that few if any experienced C programmers
treat it like one.

well, as noted, it is a lot more common in C IME, but it is almost never
done in C++.

however, I have personally seen a lot more code with the parens than without
the parens...

I agree with people who write real apps accessed by many peoples. And
these are:

It is clear and it works.

http://www.linuxjournal.com/article/5780
Jun 27 '08 #20
On May 8, 11:33*am, "cr88192" <cr88...@NOSPAM.hotmail.comwrote:
"Richard Heathfield" <r...@see.sig.invalidwrote in message

news:HM******************************@bt.com...
cr88192 said:
<snip>
now, I usually code in notepad,
Oh dear. :-)

all in all it is a good and simple editor...
(So you can write some very complex software but not an editor better
than notepad?..)

If you like notepad, you might like to try qed.exe, if you can still
find it. (I think an updated version here http://www.movsd.com/qed.htm).
It has some useful indent commands. And I believe still smaller than
notepad.

--
Bartc
Jun 27 '08 #21
Bart <bc@freeuk.comwrites:
On May 8, 11:33Â*am, "cr88192" <cr88...@NOSPAM.hotmail.comwrote:
>"Richard Heathfield" <r...@see.sig.invalidwrote in message

news:HM******************************@bt.com...
cr88192 said:
<snip>
>now, I usually code in notepad,
Oh dear. :-)

all in all it is a good and simple editor...

(So you can write some very complex software but not an editor better
than notepad?..)

If you like notepad, you might like to try qed.exe, if you can still
find it. (I think an updated version here http://www.movsd.com/qed.htm).
It has some useful indent commands. And I believe still smaller than
notepad.
Anyone using notepad to write C should be fired. There are lots of
productivity tools which make programming more sure fire and certainly
easier - not to use at least some of them is silly.
Jun 27 '08 #22
cr88192 wrote:
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:HM******************************@bt.com...
>cr88192 said:
<snip>
>> {
printf("usage: %s <filename>\n", argv[0]);
If argc is 0, the behaviour is undefined. If it is >= 1, argv[0] must
represent the program name in some way, but need not be a string
representing the invocation name for the program. It could even be a pid!

this is a common practice though, and also I don't personally know of any OS
where argc is not at least 1...
<snip>

Any Unix if the exec* call used to invoke the program provides a
parameter list with argv[0]=NULL. In Unix programs other than your shell
are allowed to exec your program, so you cannot guarantee you will get a
program name.
>>but 0 and -1 are more common/traditional.
A -1 return value has no de jure meaning in C (which is, at least, in
keeping with the better kinds of tradition - if we knew why we did them,
they wouldn't be traditions!).

To indicate failure portably, use EXIT_FAILURE.

it is more correct, but -1 is a very common value for indicating error as
well.
I think usually any value other than 0 counts as an error.
<snip>

Not on VMS. IIRC on VMS odd return values indicate success and even
return values indicate failure with the C compiler using some magic to
make returning 0 actually return an odd number to the environment.

There *are* good reasons for using non-C-standard return values from
main, but in my experience they only apply when you have return return
values indicating something more than simple success/failure.
--
Flash Gordon
Jun 27 '08 #23
Flash Gordon said:
cr88192 wrote:
<snip>
>I don't personally know of
any OS where argc is not at least 1...
Always a shaky argument.
>
<snip>

Any Unix if the exec* call used to invoke the program provides a
parameter list with argv[0]=NULL.
Right. Also, I believe, older Mac systems.

<snip>
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #24
Right, so let's do that. See 2.1.2.2 of C89 or 5.1.2.2.1 of C99:

* The value of argc shall be nonnegative.

* argv[argc] shall be a null pointer.

* If the value of argc is greater than zero, the array members
argv[0] through argv[argc-1] inclusive shall contain pointers to
strings, which are given implementation-defined values by the host
environment prior to program startup. The intent is to supply to the
program information determined prior to program startup from elsewhere
in the hosted environment. [...]

It is clear from the above that argc may be 0.
You are rigth... till now i've never found a system that pass a 0 as
argc, but it should exist,
maybe on a very small controllers environment...
>
>>> return(-1);
This has no portable meaning (and the parentheses are redundantly
superfluous).

I'm not agree, it' more readable.

Please explain why parentheses make the return code more readable. Does
this apply to every expression, or just to the expression that
(optionally) follows a return statement? If so, why is return special?
It's just a question of style. I prefer to place parentheses. Return is
not special, so it
is trated in the same way of all other cases where parentheses should be
used also
when they are superfluous.
What makes you think that adding parentheses helps? If the reader can't
comprehend a*32 / b + c - d * e / oh_my_good, why are they more likely to
comprehend (a*32 / b + c - d * e / oh_my_good)?
Yea, but of couse for me is much better to use parentheses:
retun( (((z*100)/y)+((w-r)*k)) / u );

That it is a non-ambiguous form, readable also from people with little
knowledge
of the language and not familiar with the specific operator's precedence.

b.

Jun 27 '08 #25
brix99luftballons said:

<snip>

till now i've never found a system that pass a 0 as
argc, but it should exist,
maybe on a very small controllers environment...
Macs and Unices aren't that small.
>Please explain why parentheses make the return code more readable. Does
this apply to every expression, or just to the expression that
(optionally) follows a return statement? If so, why is return special?
It's just a question of style. I prefer to place parentheses. Return is
not special,
Agreed. So why adorn its expression with parentheses? = takes an expression
as its right operand. Do you write a = (b)? Or how about +, which takes
two expressions as its operands. Do you write: a = (b) + (c) ?

If so, why? And if not, why treat return differently?
so it is trated in the same way of all other cases where parentheses
should be used also when they are superfluous.
If they are superfluous, why use them at all? I can understand why you
might add them in places where the precedence isn't obvious, e.g. in
something like (a << b) + c - but surrounding an expression with
parentheses does nothing to clarify precedence within the expression
itself.
>What makes you think that adding parentheses helps? If the reader can't
comprehend a*32 / b + c - d * e / oh_my_good, why are they more likely
to comprehend (a*32 / b + c - d * e / oh_my_good)?
Yea, but of couse for me is much better to use parentheses:
Why?
retun( (((z*100)/y)+((w-r)*k)) / u );

That it is a non-ambiguous form,
So is the original.
readable also from people with little
knowledge
of the language and not familiar with the specific operator's precedence.
How, precisely, does changing return 0; to return(0); clarify precedence?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #26
Keith Thompson <ks***@mib.orgwrote:
2. Opening and closing braces appear on lines by themselves.
Enclosed lines are indented by one level.
Rarer, but still valid, styles are:
3. Like 2, but the braces are aligned with the enclosed lines.
4. Like 2, but the braces are half-indented (e.g., by 2 columns).
I think this is the GNU-recommended style.
4.
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "usage error\n");
return -1;
}
double x, y;
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
printf("%.2f\n", y / x);
return 0;
}
That's not exactly GNU style, which is like this:

int
main (int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "usage error\n");
return -1;
}
double x, y;
x = strtod (argv[1], NULL);
y = strtod (argv[2], NULL);
printf ("%.2f\n", y / x);
return 0;
}

Andrew.
Jun 27 '08 #27
Richard Heathfield <rj*@see.sig.invalidwrites:
brix99luftballons said:

<snip>

till now i've never found a system that pass a 0 as
>argc, but it should exist,
maybe on a very small controllers environment...

Macs and Unices aren't that small.
Most systems can be made to pass a zero, but Unices (I prefer *nix)
don't do so as a matter of course.

<snip>
>so it is trated in the same way of all other cases where parentheses
should be used also when they are superfluous.

If they are superfluous, why use them at all? I can understand why you
might add them in places where the precedence isn't obvious, e.g. in
something like (a << b) + c - but surrounding an expression with
parentheses does nothing to clarify precedence within the expression
itself.
You are being your usual argumentative self! Can't you see a reason
why return (E); could be seen as a reasonable generalisation? if,
while, switch etc all have syntactic ()s that at least *look*
superfluous (yes, I know they are not so). From that point of view a
return without them look out of place.

The language designers certainly thought so. I, for one, had to
*un-learn* return (E); since it was always thus in B and it was also
required in very early C. To be portable, you wrote return (E); just in
case your code got near an ancient compiler.

Of course those days are long gone and I now prefer a minimal use of
parentheses.

--
Ben.
Jun 27 '08 #28
Ben Bacarisse said:

<snip>
Most systems can be made to pass a zero, but Unices (I prefer *nix)
don't do so as a matter of course.
Granted.

<snip>
You are being your usual argumentative self!
Well, I'm being my usual logical self. :-)
Can't you see a reason
why return (E); could be seen as a reasonable generalisation?
Do you really want to *generalise* the parenthesisation of expressions?

<snip>
Of course those days are long gone and I now prefer a minimal use of
parentheses.
Precisely.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #29

"rio" <a@b.cwrote in message
news:48***********************@reader4.news.tin.it ...
i like below

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{double x, y;

if (argc!=3) { fprintf(stderr,"usage error\n"); return -1;}

x=strtod(argv[1], 0);
y=strtod(argv[2], 0);

return (x!=0.0) ?
(printf("%.2f\n", y/x), 0): (printf("Error: x==0\n"), -1) ;
}
This is the part that goes over my head with my knowledge.

return (x!=0.0) ? /*what's the ? and 0.0 for */

There are things in C, shortcuts that I haven't grasped yet like += and ?: I
do things the long way right now.

Bill
Jun 27 '08 #30

"CBFalconer" <cb********@yahoo.comwrote in message
news:48***************@yahoo.com...
There is a program around called indent (GNU version 2.2.9 here)
which does all this for you. It is configurable. I use the
following configuration for it (really just one long line in
indent.pro):

-kr -l66 -i3 -bad -di16 -lc66 -nce -ncs -cbi0 -bbo -pmt -psl -ts1
-cdw -ppi 3
This is the results of my program using "indent div.c" which is the name
of the source code.

#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char **argv)
{
if (argc != 3)
{
fprintf (stderr, "usage error\n");
return -1;
}
double x, y;
x = strtod (argv[1], NULL);
y = strtod (argv[2], NULL);
printf ("%.2f\n", y / x);
return 0;
}

Believe me I didn't do this myself. Indent did it for me!

Bill
Jun 27 '08 #31
"Bill Cunningham" <no****@nspam.comwrites:
"rio" <a@b.cwrote in message
news:48***********************@reader4.news.tin.it ...
>i like below

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{double x, y;

if (argc!=3) { fprintf(stderr,"usage error\n"); return -1;}

x=strtod(argv[1], 0);
y=strtod(argv[2], 0);

return (x!=0.0) ?
(printf("%.2f\n", y/x), 0): (printf("Error: x==0\n"), -1) ;
}

This is the part that goes over my head with my knowledge.

return (x!=0.0) ? /*what's the ? and 0.0 for */

There are things in C, shortcuts that I haven't grasped yet like += and ?: I
do things the long way right now.
I believe "rio" is pulling your leg. His code is gratuitously
obfuscated.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #32
Nick Keighley wrote:
>
.... snip ...
>
[mutter, one project had a standard of 4 column indent
and 8 space tabs. So you mixed tabs and spaces. eek!]
That is one more reason to use 3 space indentation. Mixing spaces
with tabs is very likely to show up early, and get corrected.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #33
Ben Bacarisse <be********@bsb.me.ukwrites:
Richard Heathfield <rj*@see.sig.invalidwrites:
brix99luftballons said:

<snip>

till now i've never found a system that pass a 0 as
argc, but it should exist,
maybe on a very small controllers environment...
Macs and Unices aren't that small.

Most systems can be made to pass a zero, but Unices (I prefer *nix)
don't do so as a matter of course.
Ever used ldd? At least on linux it runs the executable with argc==0
which is catched by the dynamic linker. So if your program is dynamicly
linked, you'll never get that case but running by mistaken ldd on a
statically linked executable can lead to that.

Yours,

--
Jean-Marc
Jun 27 '08 #34
Bill Cunningham wrote:
"CBFalconer" <cb********@yahoo.comwrote in message
>There is a program around called indent (GNU version 2.2.9 here)
which does all this for you. It is configurable. I use the
following configuration for it (really just one long line in
indent.pro):

-kr -l66 -i3 -bad -di16 -lc66 -nce -ncs -cbi0 -bbo -pmt -psl -ts1 -cdw -ppi 3

This is the results of my program using "indent div.c" which is
the name of the source code.
.... snip ...
>
Believe me I didn't do this myself. Indent did it for me!
You control it with indent.pro. Just make the line I provided
above available in indent.pro in the directory that contains the
source code you are indenting.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #35

"Eligiusz Narutowicz" <el*************@hotmail.comwrote in message
news:fv**********@registered.motzarella.org...
Bart <bc@freeuk.comwrites:
>On May 8, 11:33 am, "cr88192" <cr88...@NOSPAM.hotmail.comwrote:
>>"Richard Heathfield" <r...@see.sig.invalidwrote in message

news:HM******************************@bt.com.. .

cr88192 said:

<snip>

now, I usually code in notepad,

Oh dear. :-)

all in all it is a good and simple editor...

(So you can write some very complex software but not an editor better
than notepad?..)

If you like notepad, you might like to try qed.exe, if you can still
find it. (I think an updated version here http://www.movsd.com/qed.htm).
It has some useful indent commands. And I believe still smaller than
notepad.

Anyone using notepad to write C should be fired. There are lots of
productivity tools which make programming more sure fire and certainly
easier - not to use at least some of them is silly.
I have, personally, never really had that much motivation...

notepad is simple, effective, and edits text files.

I can jump to line numbers, I can search for things, ...

about the only thing it really lacks IMO is a better search-and-replace
feature (so that I don't end up resorting to things like sed, ...). but,
only very rarely is this an issue...
when it comes to C, really, I am not sure what would be added that would
really help all that much...

also note that, often, I have anywhere from 10 to 50 notepad's open at once,
and this was one thing that was a major problem in Vista: it setting a
critically low limit on the number of open windows I could have open before
stuff went weird...

Jun 27 '08 #36

"Eligiusz Narutowicz" <el*************@hotmail.comwrote in message
news:fv**********@registered.motzarella.org...
"cr88192" <cr*****@NOSPAM.hotmail.comwrites:
<snip>
>>
well, as noted, it is a lot more common in C IME, but it is almost never
done in C++.

however, I have personally seen a lot more code with the parens than
without
the parens...


I agree with people who write real apps accessed by many peoples. And
these are:

It is clear and it works.

http://www.linuxjournal.com/article/5780
I will agree with this for the most part, as a general rule the Linux
codebase is fairly readable.

however, I do disagree with it on a few points, namely in that I tend to use
rather different naming conventions (including, on occasion, variants of
hungarian notation, including in function names...).

I do agree that globals should be avoided whenever possible, and especially
not be used between subsystems. however, even there, this is not absolute
(there are some rare cases where I believe this is acceptable).
I tend to encode in the names at least some info about where in the codebase
the code is located, and also because of my modularity practices (this helps
both in avoiding accidental clashes or dependencies, and also in locating
where some particular code is at in the project).

of course, newer languages (such as C++, C#, Java, ...) provide other
mechanisms for this, but it is a tradeoff (shorter easier names, of which
one is no longer certain where exactly in the codebase this function is
located, albeit "jump to" features and similar are common in modern IDE's).

it works...

Jun 27 '08 #37
On 8 May, 22:37, CBFalconer <cbfalco...@yahoo.comwrote:
Nick Keighleywrote:

... snip ...
[mutter, one project had a standard of 4 column indent
and 8 space tabs. So you mixed tabs and spaces. eek!]

That is one more reason to use 3 space indentation. *Mixing spaces
with tabs is very likely to show up early, and get corrected.
you misunderstand. It wasn't an error to mix tabs and spaces
it was compulsary. If you didn't indent with mixed tabs and
spaces you couldn't check your source in!

(actually the checkin system ran your program through
indent and indent was set to this standard)
--
Nick Keighley

Jun 27 '08 #38

"rio" <a@b.cwrote in message
news:48***********************@reader4.news.tin.it ...
>
"Joe Wright" <jo********@comcast.netha scritto nel messaggio
news:xd******************************@comcast.com. ..
>Bill Cunningham wrote:
>> I have had several complaints by some people who wish to help me and
I wish to get the problem straight. I wrote this small utility myself
and
added some indentation and I wonder if it is acceptable. It does make
source easier to read.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc!=3) {
fprintf(stderr,"usage error\n");
return -1;
}
double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
printf("%.2f\n",y/x);
return 0;
}

Is this a good example of a properly indended program?
>>Bill

i like below

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{double x, y;

if (argc!=3) { fprintf(stderr,"usage error\n"); return -1;}

x=strtod(argv[1], 0);
y=strtod(argv[2], 0);

return (x!=0.0) ?
(printf("%.2f\n", y/x), 0): (printf("Error: x==0\n"), -1) ;
}

ICK...

these are not exactly the way I would personally use the trinary and comma
operators...

of course, I do have something vaguely similar, but it is more of a
"compact" style:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
double x, y;
if (argc!=3) { fprintf(stderr,"usage error\n"); return(-1); }
x=strtod(argv[1], 0); y=strtod(argv[2], 0);
if(x==0.0) { printf("Error: x==0\n"); return(-1); }
printf("%.2f\n", y/x), 0); return(0); }

usually, this is done for code that:
I usually wont really be reading or editing much;
my compulsion for reducing vertical space exceeded my need for clarity.

some elements of this style may sometimes be used in ordinary code though.

>

Jun 27 '08 #39
rio
"Bill Cunningham" <no****@nspam.comha scritto nel messaggio
news:LzJUj.24523$qW.3229@trnddc06...
>
"rio" <a@b.cwrote in message
news:48***********************@reader4.news.tin.it ...
>i like below

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{double x, y;

if (argc!=3) { fprintf(stderr,"usage error\n"); return -1;}

x=strtod(argv[1], 0);
y=strtod(argv[2], 0);

return (x!=0.0) ?
(printf("%.2f\n", y/x), 0): (printf("Error: x==0\n"), -1) ;
}

This is the part that goes over my head with my knowledge.

return (x!=0.0) ? /*what's the ? and 0.0 for */
if "x" is 0.0, y/x should be here a seg-fault.
For don't allow this the program says there is an input error because x==0,
and the program return -1 the error code.
There are things in C, shortcuts that I haven't grasped yet like += and ?:
I do things the long way right now.
this is what i like
Bill


Jun 27 '08 #40
rio

"cr88192" <cr*****@NOSPAM.hotmail.comha scritto nel messaggio
news:87**************************@saipan.com...
>
"rio" <a@b.cwrote in message
news:48***********************@reader4.news.tin.it ...
>>
"Joe Wright" <jo********@comcast.netha scritto nel messaggio
news:xd******************************@comcast.com ...
>>Bill Cunningham wrote:
I have had several complaints by some people who wish to help me
and
I wish to get the problem straight. I wrote this small utility myself
and
added some indentation and I wonder if it is acceptable. It does make
source easier to read.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
if (argc!=3) {
fprintf(stderr,"usage error\n");
return -1;
}
double x,y;
x=strtod(argv[1],NULL);
y=strtod(argv[2],NULL);
printf("%.2f\n",y/x);
return 0;
}

Is this a good example of a properly indended program?
>>>Bill

i like below

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{double x, y;

if (argc!=3) { fprintf(stderr,"usage error\n"); return -1;}

x=strtod(argv[1], 0);
y=strtod(argv[2], 0);

return (x!=0.0) ?
(printf("%.2f\n", y/x), 0): (printf("Error: x==0\n"), -1) ;
}


ICK...

these are not exactly the way I would personally use the trinary and comma
operators...

of course, I do have something vaguely similar, but it is more of a
"compact" style:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
double x, y;
if (argc!=3) { fprintf(stderr,"usage error\n"); return(-1); }
x=strtod(argv[1], 0); y=strtod(argv[2], 0);
if(x==0.0) { printf("Error: x==0\n"); return(-1); }
printf("%.2f\n", y/x), 0); return(0); }
that your way is not ok i see because you
forget { for if

Jun 27 '08 #41
I have used pspad editor in the past but i think qed is much better
and less complicated.
Jun 27 '08 #42
rio
"cr88192" <cr*****@NOSPAM.hotmail.comha scritto nel messaggio
news:87**************************@saipan.com...
"rio" <a@b.cwrote in message
news:48***********************@reader4.news.tin.it ...
ICK...

these are not exactly the way I would personally use the trinary and comma
operators...
of course, I do have something vaguely similar, but it is more of a
"compact" style:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
double x, y;
if (argc!=3) { fprintf(stderr,"usage error\n"); return(-1); }
x=strtod(argv[1], 0); y=strtod(argv[2], 0);
if(x==0.0) { printf("Error: x==0\n"); return(-1); }
printf("%.2f\n", y/x), 0); return(0); }
i like it; it seems i have understood
i try something like this:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{double x, y;
/**********************/
if (argc!=3) { fprintf(stderr,"usage error\n");
return -1;}
x=strtod(argv[1], 0);
y=strtod(argv[2], 0);
return (x!=0.0) ? (printf("%.2f\n", y/x), 0):
(printf("Error: x==0\n"), -1);
}
usually, this is done for code that:
I usually wont really be reading or editing much;
my compulsion for reducing vertical space exceeded my need for clarity.

some elements of this style may sometimes be used in ordinary code though.
yes what you say is ok for me.
Thank you

Jun 27 '08 #43
On Thu, 08 May 2008 14:36:30 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
If [parens] are superfluous, why use them at all? I can understand why you
might add them in places where the precedence isn't obvious, e.g. in
something like (a << b) + c - but surrounding an expression with
parentheses does nothing to clarify precedence within the expression
itself.
That's not an example of unobvious, but of wrong or at least
different. (a << b) | c is an example of clarifying/reinforcing.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Jun 27 '08 #44

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Magnus Lie Hetland | last post by:
Not many members on the Atox mailing list yet, so I'll venture a request here... In Atox 0.2, I've added support for indentation tokens (somewhat like the Python indentation scheme, but a bit...
147
by: Sateesh | last post by:
Hi, I am a beginner in Python, and am wondering what is it about the indentation in Python, without which python scripts do not work properly. Why can't the indentation not so strict so as to give...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
7
by: diffuser78 | last post by:
I am a newbie to Python. I am mainly using Eric as the IDE for coding. Also, using VIM and gedit sometimes. I had this wierd problem of indentation. My code was 100% right but it wont run...
9
by: John Salerno | last post by:
How do you make a single string span multiple lines, but also allow yourself to indent the second (third, etc.) lines so that it lines up where you want it, without causing the newlines and tabs or...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
4
by: bearophileHUGS | last post by:
This is the best praise of semantic indentation I have read so far, by Chris Okasaki: http://okasaki.blogspot.com/2008/02/in-praise-of-mandatory-indentation-for.html A quotation: I have...
1
by: Eric S. Johansson | last post by:
in trying to make programming in Python more accessible to disabled programmers (specifically mobility impaired speech recognition users), and hitting a bit of a wall. The wall (for today) is...
19
by: Eric S. Johansson | last post by:
Almar Klein wrote: there's nothing like self interest to drive one's initiative. :-) 14 years with speech recognition and counting. I'm so looking to my 15th anniversary of being injured next...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.