Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > indentation

Reply
Thread Tools

indentation

 
 
Eligiusz Narutowicz
Guest
Posts: n/a
 
      05-08-2008
"Bill Cunningham" <(E-Mail Removed)> writes:

> 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!
 
Reply With Quote
 
 
 
 
Eligiusz Narutowicz
Guest
Posts: n/a
 
      05-08-2008
"cr88192" <(E-Mail Removed)> writes:

> "Richard Heathfield" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
 
Reply With Quote
 
 
 
 
Bart
Guest
Posts: n/a
 
      05-08-2008
On May 8, 11:33*am, "cr88192" <(E-Mail Removed)> wrote:
> "Richard Heathfield" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > 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
 
Reply With Quote
 
Eligiusz Narutowicz
Guest
Posts: n/a
 
      05-08-2008
Bart <(E-Mail Removed)> writes:

> On May 8, 11:33*am, "cr88192" <(E-Mail Removed)> wrote:
>> "Richard Heathfield" <(E-Mail Removed)> wrote in message
>>
>> news:(E-Mail Removed)...
>>
>> > 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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-08-2008
cr88192 wrote:
> "Richard Heathfield" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
 
Reply With Quote
 
brix99luftballons
Guest
Posts: n/a
 
      05-08-2008

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

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-08-2008
Richard Heathfield <(E-Mail Removed)> writes:

> 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.
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      05-08-2008

"rio" <(E-Mail Removed)> wrote in message
news:4822d8eb$0$41661$(E-Mail Removed) ...

> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      05-08-2008

"CBFalconer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-08-2008
"Bill Cunningham" <(E-Mail Removed)> writes:
> "rio" <(E-Mail Removed)> wrote in message
> news:4822d8eb$0$41661$(E-Mail Removed) ...
>> 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
remove overall indentation preserving reletive indentation Jesse B. Ruby 2 03-27-2010 07:23 PM
DataList Indentation SJ ASP .Net 2 11-04-2005 12:16 AM
[Fwd: Indentation] jacov Java 0 07-22-2004 03:22 PM
List Indentation Jay HTML 5 04-21-2004 02:49 PM
Unwanted indentation Daan HTML 2 12-09-2003 10:09 PM



Advertisments